(
schema: AnySchemaObject,
meta?: boolean
)
| 408 | meta?: boolean |
| 409 | ): Promise<AnyValidateFunction<T>> |
| 410 | compileAsync<T = unknown>( |
| 411 | schema: AnySchemaObject, |
| 412 | meta?: boolean |
| 413 | ): Promise<AnyValidateFunction<T>> { |
| 414 | if (typeof this.opts.loadSchema != "function") { |
| 415 | throw new Error("options.loadSchema should be a function") |
| 416 | } |
| 417 | const {loadSchema} = this.opts |
| 418 | return runCompileAsync.call(this, schema, meta) |
| 419 | |
| 420 | async function runCompileAsync( |
| 421 | this: Ajv, |
| 422 | _schema: AnySchemaObject, |
| 423 | _meta?: boolean |
| 424 | ): Promise<AnyValidateFunction> { |
| 425 | await loadMetaSchema.call(this, _schema.$schema) |
| 426 | const sch = this._addSchema(_schema, _meta) |
| 427 | return sch.validate || _compileAsync.call(this, sch) |
| 428 | } |
| 429 | |
| 430 | async function loadMetaSchema(this: Ajv, $ref?: string): Promise<void> { |
| 431 | if ($ref && !this.getSchema($ref)) { |
| 432 | await runCompileAsync.call(this, {$ref}, true) |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | async function _compileAsync(this: Ajv, sch: SchemaEnv): Promise<AnyValidateFunction> { |
| 437 | try { |
| 438 | return this._compileSchemaEnv(sch) |
| 439 | } catch (e) { |
| 440 | if (!(e instanceof MissingRefError)) throw e |
| 441 | checkLoaded.call(this, e) |
| 442 | await loadMissingSchema.call(this, e.missingSchema) |
| 443 | return _compileAsync.call(this, sch) |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | function checkLoaded(this: Ajv, {missingSchema: ref, missingRef}: MissingRefError): void { |
| 448 | if (this.refs[ref]) { |
| 449 | throw new Error(`AnySchema ${ref} is loaded but ${missingRef} cannot be resolved`) |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | async function loadMissingSchema(this: Ajv, ref: string): Promise<void> { |
| 454 | const _schema = await _loadSchema.call(this, ref) |
| 455 | if (!this.refs[ref]) await loadMetaSchema.call(this, _schema.$schema) |
| 456 | if (!this.refs[ref]) this.addSchema(_schema, ref, meta) |
| 457 | } |
| 458 | |
| 459 | async function _loadSchema(this: Ajv, ref: string): Promise<AnySchemaObject> { |
| 460 | const p = this._loading[ref] |
| 461 | if (p) return p |
| 462 | try { |
| 463 | return await (this._loading[ref] = loadSchema(ref)) |
| 464 | } finally { |
| 465 | delete this._loading[ref] |
| 466 | } |
| 467 | } |
no outgoing calls