id
int64
0
3.78k
code
stringlengths
13
37.9k
declarations
stringlengths
16
64.6k
2,100
function invalidSchema(schema: SchemaObject, error: any): void { ajvs.forEach((ajv) => assert.throws(() => ajv.compile(schema), error)) }
interface SchemaObject extends _SchemaObject { id?: string $id?: string $schema?: string $async?: false [x: string]: any // TODO }
2,101
function runTest({instances, draft, tests, skip = [], remotes = {}}: SchemaTest) { for (const ajv of instances) { ajv.opts.code.source = true if (draft === 6) { ajv.addMetaSchema(draft6MetaSchema) ajv.opts.defaultMeta = "http://json-schema.org/draft-06/schema#" } for (const id in remoteRef...
interface SchemaTest { instances: Ajv[] draft: number tests: TestSuite[] skip?: string[] remotes?: Record<string, any> }
2,102
function testTimestamp( opts: JTDOptions, valid: {Date: boolean; datetime: boolean; date: boolean} ) { const ajv = new _AjvJTD(opts) const schema = {type: "timestamp"} const validate = ajv.compile(schema) assert.strictEqual(validate(new Date()), valid.Date) assert.strictEqu...
type JTDOptions = CurrentOptions & { // strict mode options not supported with JTD: strict?: never allowMatchingProperties?: never allowUnionTypes?: never validateFormats?: never // validation and reporting options not supported with JTD: $data?: never verbose?: boolean $comment?: never formats?: ne...
2,103
function badEvenCode(cxt: KeywordCxt) { const op = cxt.schema ? _`===` : _`!===` // invalid on purpose cxt.pass(_`${cxt.data} % 2 ${op} 0`) }
class KeywordCxt implements KeywordErrorCxt { readonly gen: CodeGen readonly allErrors?: boolean readonly keyword: string readonly data: Name // Name referencing the current level of the data instance readonly $data?: string | false schema: any // keyword value in the schema readonly schemaValue: Code | n...
2,104
function passValidationThrowCompile(schema: SchemaObject) { ajv.validateSchema(schema).should.equal(true) should.throw(() => { ajv.compile(schema) }, /value must be/) }
interface SchemaObject extends _SchemaObject { id?: string $id?: string $schema?: string $async?: false [x: string]: any // TODO }
2,105
function afterError(res: TestResult): void { console.log("ajv options:", res.validator.opts) }
interface TestResult { validator: Ajv schema: AnySchema data: unknown valid: boolean expected: boolean errors: ErrorObject[] | null passed: boolean // true if valid == expected }
2,106
function afterEach(res: TestResult): void { // console.log(res.errors); res.valid.should.be.a("boolean") if (res.valid === true) { should.equal(res.errors, null) } else { const errs = res.errors as ErrorObject[] errs.should.be.an("array") for (const err of errs) { err.should.be.an("object"...
interface TestResult { validator: Ajv schema: AnySchema data: unknown valid: boolean expected: boolean errors: ErrorObject[] | null passed: boolean // true if valid == expected }
2,107
function getAjvAllInstances(options: Options, extraOpts: Options = {}): AjvCore[] { return [...getAjvs(_Ajv), ...getAjvs(_Ajv2019)] function getAjvs(Ajv: typeof AjvCore): AjvCore[] { return getAjvInstances(Ajv, options, extraOpts) } }
type Options = CurrentOptions & DeprecatedOptions
2,108
(i: Name) => gen.code(_`console.log(${xs}[${i}])`)
class Name extends _CodeOrName { readonly str: string constructor(s: string) { super() if (!IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier") this.str = s } toString(): string { return this.str } emptyStr(): boolean { return false } get names(): UsedN...
2,109
(x: Name) => gen.code(_`console.log(${x})`)
class Name extends _CodeOrName { readonly str: string constructor(s: string) { super() if (!IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier") this.str = s } toString(): string { return this.str } emptyStr(): boolean { return false } get names(): UsedN...
2,110
(x: Name) => _gen.code(_`console.log(${x})`)
class Name extends _CodeOrName { readonly str: string constructor(s: string) { super() if (!IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier") this.str = s } toString(): string { return this.str } emptyStr(): boolean { return false } get names(): UsedN...
2,111
function getGen(opts?: CodeGenOptions): CodeGen { return new CodeGen(new ValueScope({scope: {}}), opts) }
interface CodeGenOptions { es5?: boolean lines?: boolean ownProperties?: boolean }
2,112
function test(_ajv: Ajv) { const schema = { type: "object", required: {$data: "0/req"}, properties: { req: {}, foo: {}, bar: {}, }, } const validate = _ajv.compile(schema) shouldBeValid(validate, {req: ["foo", ...
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,113
function test(_ajv: Ajv) { const schema = { type: "object", required: {$data: "0/req"}, properties: { req: {}, foo: {}, bar: {}, }, } const validate = _ajv.compile(schema) shouldBeValid(validate, {req: ["foo", ...
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,114
function test(_ajv: Ajv) { const schema = { type: "object", required: {$data: "0/req"}, properties: { req: {}, foo: {}, bar: {}, }, } const validate = _ajv.compile(schema) shouldBeValid(validate, {req: ["foo", ...
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,115
function test(_ajv: Ajv): void { const schema = { type: "object", required: ["a"], properties: { a: {type: "string"}, }, } const validate = _ajv.compile(schema) shouldBeValid(validate, {a: "abc"}) shouldBeInvalid(validate, {}) ...
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,116
function test(_ajv: Ajv): void { const schema = { type: "object", required: ["a"], properties: { a: {type: "string"}, }, } const validate = _ajv.compile(schema) shouldBeValid(validate, {a: "abc"}) shouldBeInvalid(validate, {}) ...
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,117
function test(_ajv: Ajv): void { const schema = { type: "object", required: ["a"], properties: { a: {type: "string"}, }, } const validate = _ajv.compile(schema) shouldBeValid(validate, {a: "abc"}) shouldBeInvalid(validate, {}) ...
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,118
function test(_ajv: Ajv, numErrors: number) { const validate = _ajv.compile(schema) shouldBeValid(validate, data) shouldBeInvalid(validate, invalidData, numErrors) shouldBeError( validate.errors?.[0], "pattern", "#/propertyNames/pattern", "", ...
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,119
function test(_ajv: Ajv, numErrors: number) { const validate = _ajv.compile(schema) shouldBeValid(validate, data) shouldBeInvalid(validate, invalidData, numErrors) shouldBeError( validate.errors?.[0], "pattern", "#/propertyNames/pattern", "", ...
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,120
function test(_ajv: Ajv, numErrors: number) { const validate = _ajv.compile(schema) shouldBeValid(validate, data) shouldBeInvalid(validate, invalidData, numErrors) shouldBeError( validate.errors?.[0], "pattern", "#/propertyNames/pattern", "", ...
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,121
function test(_ajv: Ajv, numErrors?: number) { const schema = { type: ["array", "object"], minItems: 2, minProperties: 2, minimum: 5, // this keyword would log/throw in strictTypes mode } const validate = _ajv.compile(schema) shouldBeValid(validat...
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,122
function test(_ajv: Ajv, numErrors?: number) { const schema = { type: ["array", "object"], minItems: 2, minProperties: 2, minimum: 5, // this keyword would log/throw in strictTypes mode } const validate = _ajv.compile(schema) shouldBeValid(validat...
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,123
function test(_ajv: Ajv, numErrors?: number) { const schema = { type: ["array", "object"], minItems: 2, minProperties: 2, minimum: 5, // this keyword would log/throw in strictTypes mode } const validate = _ajv.compile(schema) shouldBeValid(validat...
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,124
function prepareTest(_ajv: Ajv, schema) { validate = _ajv.compile(schema) numErrors = _ajv.opts.allErrors ? 2 : 1 }
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,125
function prepareTest(_ajv: Ajv, schema) { validate = _ajv.compile(schema) numErrors = _ajv.opts.allErrors ? 2 : 1 }
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,126
function prepareTest(_ajv: Ajv, schema) { validate = _ajv.compile(schema) numErrors = _ajv.opts.allErrors ? 2 : 1 }
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,127
function _getAjvInstances(opts: Options, useOpts: Options): AjvCore[] { const optNames = Object.keys(opts) if (optNames.length) { opts = Object.assign({}, opts) const useOpts1 = Object.assign({}, useOpts) const optName = optNames[0] useOpts1[optName] = opts[optName] delete opts[opt...
type Options = CurrentOptions & DeprecatedOptions
2,128
(e1: JTDError, e2: JTDError) => e1.schemaPath.localeCompare(e2.schemaPath) || e1.instancePath.localeCompare(e2.instancePath)
interface JTDError { instancePath: string schemaPath: string }
2,129
function _it({only, skip}: JSONParseTest): TestFunc { return skip ? it.skip : only ? it.only : it }
interface JSONParseTest { name: string valid: boolean | null json: string data?: unknown only?: boolean skip?: boolean }
2,130
function shouldParse(parse: JTDParser, str: string, res: unknown): void { assert.deepStrictEqual(parse(str), res) assert.strictEqual(parse.message, undefined) assert.strictEqual(parse.position, undefined) }
interface JTDParser<T = unknown> { (json: string): T | undefined message?: string position?: number }
2,131
function shouldFail(parse: JTDParser, str: string): void { assert.strictEqual(parse(str), undefined) assert.strictEqual(typeof parse.message, "string") assert.strictEqual(typeof parse.position, "number") }
interface JTDParser<T = unknown> { (json: string): T | undefined message?: string position?: number }
2,132
function addAsyncFormatsAndKeywords(ajv: Ajv) { ajv.addFormat("date", /^\d\d\d\d-[0-1]\d-[0-3]\d$/) ajv.addFormat("english_word", { async: true, validate: checkWordOnServer, }) ajv.addKeyword({ keyword: "idExists", async: true, type: "number", validate: checkIdExists, errors: false...
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,133
function addAsyncFormatsAndKeywords(ajv: Ajv) { ajv.addFormat("date", /^\d\d\d\d-[0-1]\d-[0-3]\d$/) ajv.addFormat("english_word", { async: true, validate: checkWordOnServer, }) ajv.addKeyword({ keyword: "idExists", async: true, type: "number", validate: checkIdExists, errors: false...
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,134
function addAsyncFormatsAndKeywords(ajv: Ajv) { ajv.addFormat("date", /^\d\d\d\d-[0-1]\d-[0-3]\d$/) ajv.addFormat("english_word", { async: true, validate: checkWordOnServer, }) ajv.addKeyword({ keyword: "idExists", async: true, type: "number", validate: checkIdExists, errors: false...
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,135
function testKeywordErrors(def: KeywordDefinition): void { const ajv = new _Ajv({allErrors: true}) ajv.addKeyword(def) const schema = { type: "object", required: ["foo"], alwaysFails: true, } const validate: any = ajv.compile(schema) validate({foo: 1}).should.equal(false) ...
type KeywordDefinition = | CodeKeywordDefinition | FuncKeywordDefinition | MacroKeywordDefinition
2,136
function test(ajv: Ajv) { const validate = ajv.compile({type: "number"}) should.not.exist(validate.source) }
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,137
function test(ajv: Ajv) { const validate = ajv.compile({type: "number"}) should.not.exist(validate.source) }
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,138
function test(ajv: Ajv) { const validate = ajv.compile({type: "number"}) should.not.exist(validate.source) }
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,139
function test(ajv: Ajv) { ajv.addSchema({$id: "mySchema1", type: "string"}) const validate = ajv.getSchema("mySchema1") assert(typeof validate == "function") validate("foo").should.equal(true) validate(1).should.equal(false) should.throw( () => ajv.compile({id: "mySchema2", ...
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,140
function test(ajv: Ajv) { ajv.addSchema({$id: "mySchema1", type: "string"}) const validate = ajv.getSchema("mySchema1") assert(typeof validate == "function") validate("foo").should.equal(true) validate(1).should.equal(false) should.throw( () => ajv.compile({id: "mySchema2", ...
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,141
function test(ajv: Ajv) { ajv.addSchema({$id: "mySchema1", type: "string"}) const validate = ajv.getSchema("mySchema1") assert(typeof validate == "function") validate("foo").should.equal(true) validate(1).should.equal(false) should.throw( () => ajv.compile({id: "mySchema2", ...
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,142
function testKeyword(ajv: Ajv) { const validate = ajv.compile({ type: ["string", "number"], identifier: true, }) validate("Abc1").should.equal(true) validate("foo bar").should.equal(false) validate("123").should.equal(false) validate(123).should.equal(true) }
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,143
function testKeyword(ajv: Ajv) { const validate = ajv.compile({ type: ["string", "number"], identifier: true, }) validate("Abc1").should.equal(true) validate("foo bar").should.equal(false) validate("123").should.equal(false) validate(123).should.equal(true) }
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,144
function testKeyword(ajv: Ajv) { const validate = ajv.compile({ type: ["string", "number"], identifier: true, }) validate("Abc1").should.equal(true) validate("foo bar").should.equal(false) validate("123").should.equal(false) validate(123).should.equal(true) }
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,145
function testUnicode(ajv: Ajv) { let validateWithUnicode = ajvUnicode.compile({type: "string", minLength: 2}) let validate = ajv.compile({type: "string", minLength: 2}) validateWithUnicode("😀").should.equal(false) validate("😀").should.equal(true) validateWithUnicode = ajvUnic...
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,146
function testUnicode(ajv: Ajv) { let validateWithUnicode = ajvUnicode.compile({type: "string", minLength: 2}) let validate = ajv.compile({type: "string", minLength: 2}) validateWithUnicode("😀").should.equal(false) validate("😀").should.equal(true) validateWithUnicode = ajvUnic...
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,147
function testUnicode(ajv: Ajv) { let validateWithUnicode = ajvUnicode.compile({type: "string", minLength: 2}) let validate = ajv.compile({type: "string", minLength: 2}) validateWithUnicode("😀").should.equal(false) validate("😀").should.equal(true) validateWithUnicode = ajvUnic...
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,148
function test(opts: Options, shouldExtendRef: boolean) { const ajv = new _Ajv(opts) const schema = { definitions: { int: {type: "integer"}, }, type: "number", $ref: "#/definitions/int", minimum: 10, } let validate = ajv.compile(schema) val...
type Options = CurrentOptions & DeprecatedOptions
2,149
function testWarning(opts: Options = {}, msgPattern?: RegExp) { let oldConsole try { oldConsole = console.warn let consoleMsg console.warn = function (...args: any[]) { consoleMsg = Array.prototype.join.call(args, " ") } const ajv = new _Ajv(opts) ...
type Options = CurrentOptions & DeprecatedOptions
2,150
constructor(opts: Options = {}) { super({ ...opts, dynamicRef: true, next: true, unevaluated: true, }) }
type Options = CurrentOptions & DeprecatedOptions
2,151
(ajv: Ajv, options?: Opts): Ajv
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,152
(ajv: Ajv, options?: Opts): Ajv
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,153
(ajv: Ajv, options?: Opts): Ajv
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,154
(schema: AnySchema) => unknown
type AnySchema = Schema | AsyncSchema
2,155
function requiredOptions(o: Options): RequiredInstanceOptions { const s = o.strict const _optz = o.code?.optimize const optimize = _optz === true || _optz === undefined ? 1 : _optz || 0 const regExp = o.code?.regExp ?? defaultRegExp const uriResolver = o.uriResolver ?? DefaultUriResolver return { strict...
type Options = CurrentOptions & DeprecatedOptions
2,156
constructor(opts: Options = {}) { opts = this.opts = {...opts, ...requiredOptions(opts)} const {es5, lines} = this.opts.code this.scope = new ValueScope({scope: {}, prefixes: EXT_SCOPE_NAMES, es5, lines}) this.logger = getLogger(opts.logger) const formatOpt = opts.validateFormats opts.validateF...
type Options = CurrentOptions & DeprecatedOptions
2,157
validate<T>(schema: AsyncSchema, data: unknown | T): Promise<T>
interface AsyncSchema extends _SchemaObject { $async: true }
2,158
compile<T = unknown>(schema: AsyncSchema, _meta?: boolean): AsyncValidateFunction<T>
interface AsyncSchema extends _SchemaObject { $async: true }
2,159
compile<T = unknown>(schema: AnySchema, _meta?: boolean): AnyValidateFunction<T>
type AnySchema = Schema | AsyncSchema
2,160
compile<T = unknown>(schema: AnySchema, _meta?: boolean): AnyValidateFunction<T> { const sch = this._addSchema(schema, _meta) return (sch.validate || this._compileSchemaEnv(sch)) as AnyValidateFunction<T> }
type AnySchema = Schema | AsyncSchema
2,161
compileAsync<T = unknown>(schema: AsyncSchema, meta?: boolean): Promise<AsyncValidateFunction<T>>
interface AsyncSchema extends _SchemaObject { $async: true }
2,162
compileAsync<T = unknown>( schema: AnySchemaObject, meta?: boolean ): Promise<AnyValidateFunction<T>>
type AnySchemaObject = SchemaObject | AsyncSchema
2,163
compileAsync<T = unknown>( schema: AnySchemaObject, meta?: boolean ): Promise<AnyValidateFunction<T>> { if (typeof this.opts.loadSchema != "function") { throw new Error("options.loadSchema should be a function") } const {loadSchema} = this.opts return runCompileAsync.call(this, schema, m...
type AnySchemaObject = SchemaObject | AsyncSchema
2,164
async function runCompileAsync( this: Ajv, _schema: AnySchemaObject, _meta?: boolean ): Promise<AnyValidateFunction> { await loadMetaSchema.call(this, _schema.$schema) const sch = this._addSchema(_schema, _meta) return sch.validate || _compileAsync.call(this, sch) }
type AnySchemaObject = SchemaObject | AsyncSchema
2,165
async function runCompileAsync( this: Ajv, _schema: AnySchemaObject, _meta?: boolean ): Promise<AnyValidateFunction> { await loadMetaSchema.call(this, _schema.$schema) const sch = this._addSchema(_schema, _meta) return sch.validate || _compileAsync.call(this, sch) }
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,166
async function runCompileAsync( this: Ajv, _schema: AnySchemaObject, _meta?: boolean ): Promise<AnyValidateFunction> { await loadMetaSchema.call(this, _schema.$schema) const sch = this._addSchema(_schema, _meta) return sch.validate || _compileAsync.call(this, sch) }
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,167
async function runCompileAsync( this: Ajv, _schema: AnySchemaObject, _meta?: boolean ): Promise<AnyValidateFunction> { await loadMetaSchema.call(this, _schema.$schema) const sch = this._addSchema(_schema, _meta) return sch.validate || _compileAsync.call(this, sch) }
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,168
async function loadMetaSchema(this: Ajv, $ref?: string): Promise<void> { if ($ref && !this.getSchema($ref)) { await runCompileAsync.call(this, {$ref}, true) } }
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,169
async function loadMetaSchema(this: Ajv, $ref?: string): Promise<void> { if ($ref && !this.getSchema($ref)) { await runCompileAsync.call(this, {$ref}, true) } }
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,170
async function loadMetaSchema(this: Ajv, $ref?: string): Promise<void> { if ($ref && !this.getSchema($ref)) { await runCompileAsync.call(this, {$ref}, true) } }
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,171
async function _compileAsync(this: Ajv, sch: SchemaEnv): Promise<AnyValidateFunction> { try { return this._compileSchemaEnv(sch) } catch (e) { if (!(e instanceof MissingRefError)) throw e checkLoaded.call(this, e) await loadMissingSchema.call(this, e.missingSchema) re...
class SchemaEnv implements SchemaEnvArgs { readonly schema: AnySchema readonly schemaId?: "$id" | "id" readonly root: SchemaEnv baseId: string // TODO possibly, it should be readonly schemaPath?: string localRefs?: LocalRefs readonly meta?: boolean readonly $async?: boolean // true if the current schema...
2,172
async function _compileAsync(this: Ajv, sch: SchemaEnv): Promise<AnyValidateFunction> { try { return this._compileSchemaEnv(sch) } catch (e) { if (!(e instanceof MissingRefError)) throw e checkLoaded.call(this, e) await loadMissingSchema.call(this, e.missingSchema) re...
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,173
async function _compileAsync(this: Ajv, sch: SchemaEnv): Promise<AnyValidateFunction> { try { return this._compileSchemaEnv(sch) } catch (e) { if (!(e instanceof MissingRefError)) throw e checkLoaded.call(this, e) await loadMissingSchema.call(this, e.missingSchema) re...
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,174
async function _compileAsync(this: Ajv, sch: SchemaEnv): Promise<AnyValidateFunction> { try { return this._compileSchemaEnv(sch) } catch (e) { if (!(e instanceof MissingRefError)) throw e checkLoaded.call(this, e) await loadMissingSchema.call(this, e.missingSchema) re...
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,175
function checkLoaded(this: Ajv, {missingSchema: ref, missingRef}: MissingRefError): void { if (this.refs[ref]) { throw new Error(`AnySchema ${ref} is loaded but ${missingRef} cannot be resolved`) } }
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,176
function checkLoaded(this: Ajv, {missingSchema: ref, missingRef}: MissingRefError): void { if (this.refs[ref]) { throw new Error(`AnySchema ${ref} is loaded but ${missingRef} cannot be resolved`) } }
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,177
function checkLoaded(this: Ajv, {missingSchema: ref, missingRef}: MissingRefError): void { if (this.refs[ref]) { throw new Error(`AnySchema ${ref} is loaded but ${missingRef} cannot be resolved`) } }
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,178
function checkLoaded(this: Ajv, {missingSchema: ref, missingRef}: MissingRefError): void { if (this.refs[ref]) { throw new Error(`AnySchema ${ref} is loaded but ${missingRef} cannot be resolved`) } }
class MissingRefError extends Error { readonly missingRef: string readonly missingSchema: string constructor(resolver: UriResolver, baseId: string, ref: string, msg?: string) { super(msg || `can't resolve reference ${ref} from id ${baseId}`) this.missingRef = resolveUrl(resolver, baseId, ref) this.mi...
2,179
async function loadMissingSchema(this: Ajv, ref: string): Promise<void> { const _schema = await _loadSchema.call(this, ref) if (!this.refs[ref]) await loadMetaSchema.call(this, _schema.$schema) if (!this.refs[ref]) this.addSchema(_schema, ref, meta) }
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,180
async function loadMissingSchema(this: Ajv, ref: string): Promise<void> { const _schema = await _loadSchema.call(this, ref) if (!this.refs[ref]) await loadMetaSchema.call(this, _schema.$schema) if (!this.refs[ref]) this.addSchema(_schema, ref, meta) }
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,181
async function loadMissingSchema(this: Ajv, ref: string): Promise<void> { const _schema = await _loadSchema.call(this, ref) if (!this.refs[ref]) await loadMetaSchema.call(this, _schema.$schema) if (!this.refs[ref]) this.addSchema(_schema, ref, meta) }
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,182
async function _loadSchema(this: Ajv, ref: string): Promise<AnySchemaObject> { const p = this._loading[ref] if (p) return p try { return await (this._loading[ref] = loadSchema(ref)) } finally { delete this._loading[ref] } }
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,183
async function _loadSchema(this: Ajv, ref: string): Promise<AnySchemaObject> { const p = this._loading[ref] if (p) return p try { return await (this._loading[ref] = loadSchema(ref)) } finally { delete this._loading[ref] } }
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,184
async function _loadSchema(this: Ajv, ref: string): Promise<AnySchemaObject> { const p = this._loading[ref] if (p) return p try { return await (this._loading[ref] = loadSchema(ref)) } finally { delete this._loading[ref] } }
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,185
addMetaSchema( schema: AnySchemaObject, key?: string, // schema key _validateSchema = this.opts.validateSchema // false to skip schema validation, can be used to override validateSchema option for meta-schema ): Ajv { this.addSchema(schema, key, true, _validateSchema) return this }
type AnySchemaObject = SchemaObject | AsyncSchema
2,186
validateSchema(schema: AnySchema, throwOrLogError?: boolean): boolean | Promise<unknown> { if (typeof schema == "boolean") return true let $schema: string | AnySchemaObject | undefined $schema = schema.$schema if ($schema !== undefined && typeof $schema != "string") { throw new Error("$schema must...
type AnySchema = Schema | AsyncSchema
2,187
addVocabulary(definitions: Vocabulary): Ajv { for (const def of definitions) this.addKeyword(def) return this }
type Vocabulary = (KeywordDefinition | string)[]
2,188
$dataMetaSchema(metaSchema: AnySchemaObject, keywordsJsonPointers: string[]): AnySchemaObject { const rules = this.RULES.all metaSchema = JSON.parse(JSON.stringify(metaSchema)) for (const jsonPointer of keywordsJsonPointers) { const segments = jsonPointer.split("/").slice(1) // first segment is an emp...
type AnySchemaObject = SchemaObject | AsyncSchema
2,189
_addSchema( schema: AnySchema, meta?: boolean, baseId?: string, validateSchema = this.opts.validateSchema, addSchema = this.opts.addUsedSchema ): SchemaEnv { let id: string | undefined const {schemaId} = this.opts if (typeof schema == "object") { id = schema[schemaId] } else ...
type AnySchema = Schema | AsyncSchema
2,190
private _compileSchemaEnv(sch: SchemaEnv): AnyValidateFunction { if (sch.meta) this._compileMetaSchema(sch) else compileSchema.call(this, sch) /* istanbul ignore if */ if (!sch.validate) throw new Error("ajv implementation error") return sch.validate }
class SchemaEnv implements SchemaEnvArgs { readonly schema: AnySchema readonly schemaId?: "$id" | "id" readonly root: SchemaEnv baseId: string // TODO possibly, it should be readonly schemaPath?: string localRefs?: LocalRefs readonly meta?: boolean readonly $async?: boolean // true if the current schema...
2,191
private _compileMetaSchema(sch: SchemaEnv): void { const currentOpts = this.opts this.opts = this._metaOpts try { compileSchema.call(this, sch) } finally { this.opts = currentOpts } }
class SchemaEnv implements SchemaEnvArgs { readonly schema: AnySchema readonly schemaId?: "$id" | "id" readonly root: SchemaEnv baseId: string // TODO possibly, it should be readonly schemaPath?: string localRefs?: LocalRefs readonly meta?: boolean readonly $async?: boolean // true if the current schema...
2,192
function checkOptions( this: Ajv, checkOpts: OptionsInfo<RemovedOptions | DeprecatedOptions>, options: Options & RemovedOptions, msg: string, log: "warn" | "error" = "error" ): void { for (const key in checkOpts) { const opt = key as keyof typeof checkOpts if (opt in options) this.logger[log](`${msg...
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,193
function checkOptions( this: Ajv, checkOpts: OptionsInfo<RemovedOptions | DeprecatedOptions>, options: Options & RemovedOptions, msg: string, log: "warn" | "error" = "error" ): void { for (const key in checkOpts) { const opt = key as keyof typeof checkOpts if (opt in options) this.logger[log](`${msg...
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,194
function checkOptions( this: Ajv, checkOpts: OptionsInfo<RemovedOptions | DeprecatedOptions>, options: Options & RemovedOptions, msg: string, log: "warn" | "error" = "error" ): void { for (const key in checkOpts) { const opt = key as keyof typeof checkOpts if (opt in options) this.logger[log](`${msg...
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,195
function getSchEnv(this: Ajv, keyRef: string): SchemaEnv | string | undefined { keyRef = normalizeId(keyRef) // TODO tests fail without this line return this.schemas[keyRef] || this.refs[keyRef] }
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,196
function getSchEnv(this: Ajv, keyRef: string): SchemaEnv | string | undefined { keyRef = normalizeId(keyRef) // TODO tests fail without this line return this.schemas[keyRef] || this.refs[keyRef] }
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,197
function getSchEnv(this: Ajv, keyRef: string): SchemaEnv | string | undefined { keyRef = normalizeId(keyRef) // TODO tests fail without this line return this.schemas[keyRef] || this.refs[keyRef] }
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,198
function addInitialSchemas(this: Ajv): void { const optsSchemas = this.opts.schemas if (!optsSchemas) return if (Array.isArray(optsSchemas)) this.addSchema(optsSchemas) else for (const key in optsSchemas) this.addSchema(optsSchemas[key] as AnySchema, key) }
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,199
function addInitialSchemas(this: Ajv): void { const optsSchemas = this.opts.schemas if (!optsSchemas) return if (Array.isArray(optsSchemas)) this.addSchema(optsSchemas) else for (const key in optsSchemas) this.addSchema(optsSchemas[key] as AnySchema, key) }
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...