id
int64
0
3.78k
code
stringlengths
13
37.9k
declarations
stringlengths
16
64.6k
2,200
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 { _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,201
function addInitialFormats(this: Ajv): void { for (const name in this.opts.formats) { const format = this.opts.formats[name] if (format) this.addFormat(name, format) } }
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,202
function addInitialFormats(this: Ajv): void { for (const name in this.opts.formats) { const format = this.opts.formats[name] if (format) this.addFormat(name, format) } }
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,203
function addInitialFormats(this: Ajv): void { for (const name in this.opts.formats) { const format = this.opts.formats[name] if (format) this.addFormat(name, format) } }
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,204
function addInitialKeywords( this: Ajv, defs: Vocabulary | {[K in string]?: KeywordDefinition} ): void { if (Array.isArray(defs)) { this.addVocabulary(defs) return } this.logger.warn("keywords option as map is deprecated, pass array") for (const keyword in defs) { const def = defs[keyword] as Ke...
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,205
function addInitialKeywords( this: Ajv, defs: Vocabulary | {[K in string]?: KeywordDefinition} ): void { if (Array.isArray(defs)) { this.addVocabulary(defs) return } this.logger.warn("keywords option as map is deprecated, pass array") for (const keyword in defs) { const def = defs[keyword] as Ke...
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,206
function addInitialKeywords( this: Ajv, defs: Vocabulary | {[K in string]?: KeywordDefinition} ): void { if (Array.isArray(defs)) { this.addVocabulary(defs) return } this.logger.warn("keywords option as map is deprecated, pass array") for (const keyword in defs) { const def = defs[keyword] as Ke...
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,207
function getMetaSchemaOptions(this: Ajv): InstanceOptions { const metaOpts = {...this.opts} for (const opt of META_IGNORE_OPTIONS) delete metaOpts[opt] return metaOpts }
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,208
function getMetaSchemaOptions(this: Ajv): InstanceOptions { const metaOpts = {...this.opts} for (const opt of META_IGNORE_OPTIONS) delete metaOpts[opt] return metaOpts }
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,209
function getMetaSchemaOptions(this: Ajv): InstanceOptions { const metaOpts = {...this.opts} for (const opt of META_IGNORE_OPTIONS) delete metaOpts[opt] return metaOpts }
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,210
function checkKeyword(this: Ajv, keyword: string | string[], def?: KeywordDefinition): void { const {RULES} = this eachItem(keyword, (kwd) => { if (RULES.keywords[kwd]) throw new Error(`Keyword ${kwd} is already defined`) if (!KEYWORD_NAME.test(kwd)) throw new Error(`Keyword ${kwd} has invalid name`) }) ...
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,211
function checkKeyword(this: Ajv, keyword: string | string[], def?: KeywordDefinition): void { const {RULES} = this eachItem(keyword, (kwd) => { if (RULES.keywords[kwd]) throw new Error(`Keyword ${kwd} is already defined`) if (!KEYWORD_NAME.test(kwd)) throw new Error(`Keyword ${kwd} has invalid name`) }) ...
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,212
function checkKeyword(this: Ajv, keyword: string | string[], def?: KeywordDefinition): void { const {RULES} = this eachItem(keyword, (kwd) => { if (RULES.keywords[kwd]) throw new Error(`Keyword ${kwd} is already defined`) if (!KEYWORD_NAME.test(kwd)) throw new Error(`Keyword ${kwd} has invalid name`) }) ...
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,213
function checkKeyword(this: Ajv, keyword: string | string[], def?: KeywordDefinition): void { const {RULES} = this eachItem(keyword, (kwd) => { if (RULES.keywords[kwd]) throw new Error(`Keyword ${kwd} is already defined`) if (!KEYWORD_NAME.test(kwd)) throw new Error(`Keyword ${kwd} has invalid name`) }) ...
type KeywordDefinition = | CodeKeywordDefinition | FuncKeywordDefinition | MacroKeywordDefinition
2,214
function addRule( this: Ajv, keyword: string, definition?: AddedKeywordDefinition, dataType?: JSONType ): void { const post = definition?.post if (dataType && post) throw new Error('keyword with "post" flag cannot have "type"') const {RULES} = this let ruleGroup = post ? RULES.post : RULES.rules.find(({...
type AddedKeywordDefinition = KeywordDefinition & { type: JSONType[] schemaType: JSONType[] }
2,215
function addRule( this: Ajv, keyword: string, definition?: AddedKeywordDefinition, dataType?: JSONType ): void { const post = definition?.post if (dataType && post) throw new Error('keyword with "post" flag cannot have "type"') const {RULES} = this let ruleGroup = post ? RULES.post : RULES.rules.find(({...
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,216
function addRule( this: Ajv, keyword: string, definition?: AddedKeywordDefinition, dataType?: JSONType ): void { const post = definition?.post if (dataType && post) throw new Error('keyword with "post" flag cannot have "type"') const {RULES} = this let ruleGroup = post ? RULES.post : RULES.rules.find(({...
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,217
function addRule( this: Ajv, keyword: string, definition?: AddedKeywordDefinition, dataType?: JSONType ): void { const post = definition?.post if (dataType && post) throw new Error('keyword with "post" flag cannot have "type"') const {RULES} = this let ruleGroup = post ? RULES.post : RULES.rules.find(({...
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,218
function addRule( this: Ajv, keyword: string, definition?: AddedKeywordDefinition, dataType?: JSONType ): void { const post = definition?.post if (dataType && post) throw new Error('keyword with "post" flag cannot have "type"') const {RULES} = this let ruleGroup = post ? RULES.post : RULES.rules.find(({...
type JSONType<T extends string, IsPartial extends boolean> = IsPartial extends true ? T | undefined : T
2,219
function addRule( this: Ajv, keyword: string, definition?: AddedKeywordDefinition, dataType?: JSONType ): void { const post = definition?.post if (dataType && post) throw new Error('keyword with "post" flag cannot have "type"') const {RULES} = this let ruleGroup = post ? RULES.post : RULES.rules.find(({...
type JSONType = (typeof _jsonTypes)[number]
2,220
function addBeforeRule(this: Ajv, ruleGroup: RuleGroup, rule: Rule, before: string): void { const i = ruleGroup.rules.findIndex((_rule) => _rule.keyword === before) if (i >= 0) { ruleGroup.rules.splice(i, 0, rule) } else { ruleGroup.rules.push(rule) this.logger.warn(`rule ${before} is not defined`) ...
interface RuleGroup { type?: JSONType rules: Rule[] }
2,221
function addBeforeRule(this: Ajv, ruleGroup: RuleGroup, rule: Rule, before: string): void { const i = ruleGroup.rules.findIndex((_rule) => _rule.keyword === before) if (i >= 0) { ruleGroup.rules.splice(i, 0, rule) } else { ruleGroup.rules.push(rule) this.logger.warn(`rule ${before} is not defined`) ...
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,222
function addBeforeRule(this: Ajv, ruleGroup: RuleGroup, rule: Rule, before: string): void { const i = ruleGroup.rules.findIndex((_rule) => _rule.keyword === before) if (i >= 0) { ruleGroup.rules.splice(i, 0, rule) } else { ruleGroup.rules.push(rule) this.logger.warn(`rule ${before} is not defined`) ...
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,223
function addBeforeRule(this: Ajv, ruleGroup: RuleGroup, rule: Rule, before: string): void { const i = ruleGroup.rules.findIndex((_rule) => _rule.keyword === before) if (i >= 0) { ruleGroup.rules.splice(i, 0, rule) } else { ruleGroup.rules.push(rule) this.logger.warn(`rule ${before} is not defined`) ...
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,224
function addBeforeRule(this: Ajv, ruleGroup: RuleGroup, rule: Rule, before: string): void { const i = ruleGroup.rules.findIndex((_rule) => _rule.keyword === before) if (i >= 0) { ruleGroup.rules.splice(i, 0, rule) } else { ruleGroup.rules.push(rule) this.logger.warn(`rule ${before} is not defined`) ...
interface Rule { keyword: string definition: AddedKeywordDefinition }
2,225
function keywordMetaschema(this: Ajv, def: KeywordDefinition): void { let {metaSchema} = def if (metaSchema === undefined) return if (def.$data && this.opts.$data) metaSchema = schemaOrData(metaSchema) def.validateSchema = this.compile(metaSchema, 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,226
function keywordMetaschema(this: Ajv, def: KeywordDefinition): void { let {metaSchema} = def if (metaSchema === undefined) return if (def.$data && this.opts.$data) metaSchema = schemaOrData(metaSchema) def.validateSchema = this.compile(metaSchema, 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,227
function keywordMetaschema(this: Ajv, def: KeywordDefinition): void { let {metaSchema} = def if (metaSchema === undefined) return if (def.$data && this.opts.$data) metaSchema = schemaOrData(metaSchema) def.validateSchema = this.compile(metaSchema, 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,228
function keywordMetaschema(this: Ajv, def: KeywordDefinition): void { let {metaSchema} = def if (metaSchema === undefined) return if (def.$data && this.opts.$data) metaSchema = schemaOrData(metaSchema) def.validateSchema = this.compile(metaSchema, true) }
type KeywordDefinition = | CodeKeywordDefinition | FuncKeywordDefinition | MacroKeywordDefinition
2,229
function schemaOrData(schema: AnySchema): AnySchemaObject { return {anyOf: [schema, $dataRef]} }
type AnySchema = Schema | AsyncSchema
2,230
constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) }
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,231
compileSerializer<T = unknown>(schema: SchemaObject): (data: T) => string
interface SchemaObject extends _SchemaObject { id?: string $id?: string $schema?: string $async?: false [x: string]: any // TODO }
2,232
compileSerializer<T = unknown>(schema: SchemaObject): (data: T) => string { const sch = this._addSchema(schema) return sch.serialize || this._compileSerializer(sch) }
interface SchemaObject extends _SchemaObject { id?: string $id?: string $schema?: string $async?: false [x: string]: any // TODO }
2,233
compileParser<T = unknown>(schema: SchemaObject): JTDParser<T>
interface SchemaObject extends _SchemaObject { id?: string $id?: string $schema?: string $async?: false [x: string]: any // TODO }
2,234
compileParser<T = unknown>(schema: SchemaObject): JTDParser<T> { const sch = this._addSchema(schema) return (sch.parse || this._compileParser(sch)) as JTDParser<T> }
interface SchemaObject extends _SchemaObject { id?: string $id?: string $schema?: string $async?: false [x: string]: any // TODO }
2,235
private _compileSerializer<T>(sch: SchemaEnv): (data: T) => string { compileSerializer.call(this, sch, (sch.schema as AnySchemaObject).definitions || {}) /* istanbul ignore if */ if (!sch.serialize) throw new Error("ajv implementation error") return sch.serialize }
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,236
private _compileParser(sch: SchemaEnv): JTDParser { compileParser.call(this, sch, (sch.schema as AnySchemaObject).definitions || {}) /* istanbul ignore if */ if (!sch.parse) throw new Error("ajv implementation error") return sch.parse }
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,237
function funcExportCode(source?: SourceCode): string { const usedValues: UsedScopeValues = {} const n = source?.validateName const vCode = validateCode(usedValues, source) if (ajv.opts.code.esm) { // Always do named export as `validate` rather than the variable `n` which is `validateXX` for known ...
interface SourceCode { validateName: ValueScopeName validateCode: string scopeValues: ScopeValueSets evaluated?: Code }
2,238
function validateCode(usedValues: UsedScopeValues, s?: SourceCode): Code { if (!s) throw new Error('moduleCode: function does not have "source" property') if (usedState(s.validateName) === UsedValueState.Completed) return nil setUsedState(s.validateName, UsedValueState.Started) const scopeCode = ajv.sc...
type UsedScopeValues = { [Prefix in string]?: Map<ValueScopeName, UsedValueState | undefined> }
2,239
function validateCode(usedValues: UsedScopeValues, s?: SourceCode): Code { if (!s) throw new Error('moduleCode: function does not have "source" property') if (usedState(s.validateName) === UsedValueState.Completed) return nil setUsedState(s.validateName, UsedValueState.Started) const scopeCode = ajv.sc...
interface SourceCode { validateName: ValueScopeName validateCode: string scopeValues: ScopeValueSets evaluated?: Code }
2,240
function refValidateCode(n: ValueScopeName): Code | undefined { const vRef = n.value?.ref if (n.prefix === "validate" && typeof vRef == "function") { const v = vRef as AnyValidateFunction return validateCode(usedValues, v.source) } else if ((n.prefix === "root" || n.prefix === "wrapper...
class ValueScopeName extends Name { readonly prefix: string value?: NameValue scopePath?: Code constructor(prefix: string, nameStr: string) { super(nameStr) this.prefix = prefix } setValue(value: NameValue, {property, itemIndex}: ScopePath): void { this.value = value this.scopePath = _`.${...
2,241
function usedState(name: ValueScopeName): UsedValueState | undefined { return usedValues[name.prefix]?.get(name) }
class ValueScopeName extends Name { readonly prefix: string value?: NameValue scopePath?: Code constructor(prefix: string, nameStr: string) { super(nameStr) this.prefix = prefix } setValue(value: NameValue, {property, itemIndex}: ScopePath): void { this.value = value this.scopePath = _`.${...
2,242
function setUsedState(name: ValueScopeName, state: UsedValueState): void { const {prefix} = name const names = (usedValues[prefix] = usedValues[prefix] || new Map()) names.set(name, state) }
class ValueScopeName extends Name { readonly prefix: string value?: NameValue scopePath?: Code constructor(prefix: string, nameStr: string) { super(nameStr) this.prefix = prefix } setValue(value: NameValue, {property, itemIndex}: ScopePath): void { this.value = value this.scopePath = _`.${...
2,243
constructor(readonly ajv: 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,244
constructor(readonly ajv: 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,245
constructor(readonly ajv: 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,246
compile<T = unknown>(schema: AnySchema, meta?: boolean): AnyValidateFunction<T> { return this.getStandalone(this.ajv.compile<T>(schema, meta)) }
type AnySchema = Schema | AsyncSchema
2,247
function checkReportMissingProp(cxt: KeywordCxt, prop: string): void { const {gen, data, it} = cxt gen.if(noPropertyInData(gen, data, prop, it.opts.ownProperties), () => { cxt.setParams({missingProperty: _`${prop}`}, true) cxt.error() }) }
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,248
function checkMissingProp( {gen, data, it: {opts}}: KeywordCxt, properties: string[], missing: Name ): Code { return or( ...properties.map((prop) => and(noPropertyInData(gen, data, prop, opts.ownProperties), _`${missing} = ${prop}`) ) ) }
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,249
function checkMissingProp( {gen, data, it: {opts}}: KeywordCxt, properties: string[], missing: Name ): Code { return or( ...properties.map((prop) => and(noPropertyInData(gen, data, prop, opts.ownProperties), _`${missing} = ${prop}`) ) ) }
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,250
function reportMissingProp(cxt: KeywordCxt, missing: Name): void { cxt.setParams({missingProperty: missing}, true) cxt.error() }
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,251
function reportMissingProp(cxt: KeywordCxt, missing: Name): void { cxt.setParams({missingProperty: missing}, true) cxt.error() }
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,252
function hasPropFunc(gen: CodeGen): Name { return gen.scopeValue("func", { // eslint-disable-next-line @typescript-eslint/unbound-method ref: Object.prototype.hasOwnProperty, code: _`Object.prototype.hasOwnProperty`, }) }
class CodeGen { readonly _scope: Scope readonly _extScope: ValueScope readonly _values: ScopeValueSets = {} private readonly _nodes: ParentNode[] private readonly _blockStarts: number[] = [] private readonly _constants: Constants = {} private readonly opts: CGOptions constructor(extScope: ValueScope, o...
2,253
function isOwnProperty(gen: CodeGen, data: Name, property: Name | string): Code { return _`${hasPropFunc(gen)}.call(${data}, ${property})` }
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,254
function isOwnProperty(gen: CodeGen, data: Name, property: Name | string): Code { return _`${hasPropFunc(gen)}.call(${data}, ${property})` }
class CodeGen { readonly _scope: Scope readonly _extScope: ValueScope readonly _values: ScopeValueSets = {} private readonly _nodes: ParentNode[] private readonly _blockStarts: number[] = [] private readonly _constants: Constants = {} private readonly opts: CGOptions constructor(extScope: ValueScope, o...
2,255
function propertyInData( gen: CodeGen, data: Name, property: Name | string, ownProperties?: boolean ): Code { const cond = _`${data}${getProperty(property)} !== undefined` return ownProperties ? _`${cond} && ${isOwnProperty(gen, data, property)}` : cond }
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,256
function propertyInData( gen: CodeGen, data: Name, property: Name | string, ownProperties?: boolean ): Code { const cond = _`${data}${getProperty(property)} !== undefined` return ownProperties ? _`${cond} && ${isOwnProperty(gen, data, property)}` : cond }
class CodeGen { readonly _scope: Scope readonly _extScope: ValueScope readonly _values: ScopeValueSets = {} private readonly _nodes: ParentNode[] private readonly _blockStarts: number[] = [] private readonly _constants: Constants = {} private readonly opts: CGOptions constructor(extScope: ValueScope, o...
2,257
function noPropertyInData( gen: CodeGen, data: Name, property: Name | string, ownProperties?: boolean ): Code { const cond = _`${data}${getProperty(property)} === undefined` return ownProperties ? or(cond, not(isOwnProperty(gen, data, property))) : cond }
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,258
function noPropertyInData( gen: CodeGen, data: Name, property: Name | string, ownProperties?: boolean ): Code { const cond = _`${data}${getProperty(property)} === undefined` return ownProperties ? or(cond, not(isOwnProperty(gen, data, property))) : cond }
class CodeGen { readonly _scope: Scope readonly _extScope: ValueScope readonly _values: ScopeValueSets = {} private readonly _nodes: ParentNode[] private readonly _blockStarts: number[] = [] private readonly _constants: Constants = {} private readonly opts: CGOptions constructor(extScope: ValueScope, o...
2,259
function allSchemaProperties(schemaMap?: SchemaMap): string[] { return schemaMap ? Object.keys(schemaMap).filter((p) => p !== "__proto__") : [] }
type SchemaMap = {[Key in string]?: AnySchema}
2,260
function schemaProperties(it: SchemaCxt, schemaMap: SchemaMap): string[] { return allSchemaProperties(schemaMap).filter( (p) => !alwaysValidSchema(it, schemaMap[p] as AnySchema) ) }
interface SchemaCxt { readonly gen: CodeGen readonly allErrors?: boolean // validation mode - whether to collect all errors or break on error readonly data: Name // Name with reference to the current part of data instance readonly parentData: Name // should be used in keywords modifying data readonly parentDa...
2,261
function schemaProperties(it: SchemaCxt, schemaMap: SchemaMap): string[] { return allSchemaProperties(schemaMap).filter( (p) => !alwaysValidSchema(it, schemaMap[p] as AnySchema) ) }
type SchemaMap = {[Key in string]?: AnySchema}
2,262
function callValidateCode( {schemaCode, data, it: {gen, topSchemaRef, schemaPath, errorPath}, it}: KeywordCxt, func: Code, context: Code, passSchema?: boolean ): Code { const dataAndSchema = passSchema ? _`${schemaCode}, ${data}, ${topSchemaRef}${schemaPath}` : data const valCxt: [Name, Code | number][] = [...
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,263
function callValidateCode( {schemaCode, data, it: {gen, topSchemaRef, schemaPath, errorPath}, it}: KeywordCxt, func: Code, context: Code, passSchema?: boolean ): Code { const dataAndSchema = passSchema ? _`${schemaCode}, ${data}, ${topSchemaRef}${schemaPath}` : data const valCxt: [Name, Code | number][] = [...
type Code = _Code | Name
2,264
function usePattern({gen, it: {opts}}: KeywordCxt, pattern: string): Name { const u = opts.unicodeRegExp ? "u" : "" const {regExp} = opts.code const rx = regExp(pattern, u) return gen.scopeValue("pattern", { key: rx.toString(), ref: rx, code: _`${regExp.code === "new RegExp" ? newRegExp : useFunc(g...
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,265
function validateArray(cxt: KeywordCxt): Name { const {gen, data, keyword, it} = cxt const valid = gen.name("valid") if (it.allErrors) { const validArr = gen.let("valid", true) validateItems(() => gen.assign(validArr, false)) return validArr } gen.var(valid, true) validateItems(() => gen.break()...
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,266
function validateUnion(cxt: KeywordCxt): void { const {gen, schema, keyword, it} = cxt /* istanbul ignore if */ if (!Array.isArray(schema)) throw new Error("ajv implementation error") const alwaysValid = schema.some((sch: AnySchema) => alwaysValidSchema(it, sch)) if (alwaysValid && !it.opts.unevaluated) retur...
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,267
(sch: AnySchema) => alwaysValidSchema(it, sch)
type AnySchema = Schema | AsyncSchema
2,268
(_sch: AnySchema, i: number) => { const schCxt = cxt.subschema( { keyword, schemaProp: i, compositeRule: true, }, schValid ) gen.assign(valid, _`${valid} || ${schValid}`) const merged = cxt.mergeValidEvaluated(schCxt, schValid) // can s...
type AnySchema = Schema | AsyncSchema
2,269
code(cxt: KeywordCxt, ruleType?: string) { const {gen, data, $data, schema, schemaCode, it} = cxt const {opts, errSchemaPath, schemaEnv, self} = it if (!opts.validateFormats) return if ($data) validate$DataFormat() else validateFormat() function validate$DataFormat(): void { const fmts =...
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,270
function getFormat(fmtDef: AddedFormat): [string, FormatValidate, Code] { const code = fmtDef instanceof RegExp ? regexpCode(fmtDef) : opts.code.formats ? _`${opts.code.formats}${getProperty(schema)}` : undefined const fmt = gen.scopeValue("forma...
type AddedFormat = | true | RegExp | FormatValidator<string> | FormatDefinition<string> | FormatDefinition<number> | AsyncFormatDefinition<string> | AsyncFormatDefinition<number>
2,271
code(cxt: KeywordCxt) { const {gen, schema, data, it} = cxt const items = it.items || 0 if (items === true) return const len = gen.const("len", _`${data}.length`) if (schema === false) { cxt.setParams({len: items}) cxt.fail(_`${len} > ${items}`) } else if (typeof schema == "object" &...
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,272
function validateItems(valid: Name, from: Name | number): void { gen.forRange("i", from, len, (i) => { cxt.subschema({keyword: "unevaluatedItems", dataProp: i, dataPropType: Type.Num}, valid) if (!it.allErrors) gen.if(not(valid), () => gen.break()) }) }
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,273
(key: Name) => gen.if(unevaluatedDynamic(props, key), () => unevaluatedPropCode(key))
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,274
(key: Name) => props === undefined ? unevaluatedPropCode(key) : gen.if(unevaluatedStatic(props, key), () => unevaluatedPropCode(key))
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,275
function unevaluatedPropCode(key: Name): void { if (schema === false) { cxt.setParams({unevaluatedProperty: key}) cxt.error() if (!allErrors) gen.break() return } if (!alwaysValidSchema(it, schema)) { const valid = gen.name("valid") cxt.subschema( ...
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,276
function unevaluatedDynamic(evaluatedProps: Name, key: Name): Code { return _`!${evaluatedProps} || !${evaluatedProps}[${key}]` }
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,277
code(cxt: KeywordCxt) { checkMetadata(cxt) const {gen, data, schema, schemaValue, parentSchema, it} = cxt if (schema.length === 0) throw new Error("enum must have non-empty array") if (schema.length !== new Set(schema).size) throw new Error("enum items must be unique") let valid: Code const isSt...
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,278
code(cxt: KeywordCxt) { checkMetadata(cxt) const {gen, data, schema: ref, parentSchema, it} = cxt const { schemaEnv: {root}, } = it const valid = gen.name("valid") if (parentSchema.nullable) { gen.var(valid, _`${data} === null`) gen.if(not(valid), validateJtdRef) } else { ...
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,279
function callValidate(schema: AnySchemaObject): void { const sch = compileSchema.call( it.self, new SchemaEnv({schema, root, schemaPath: `/definitions/${ref}`}) ) const v = getValidate(cxt, sch) const errsCount = gen.const("_errs", N.errors) callRef(cxt, v, sch, sch.$async)...
type AnySchemaObject = SchemaObject | AsyncSchema
2,280
function inlineRefSchema(schema: AnySchemaObject): void { const schName = gen.scopeValue( "schema", it.opts.code.source === true ? {ref: schema, code: stringify(schema)} : {ref: schema} ) cxt.subschema( { schema, dataTypes: [], schemaPath: nil, ...
type AnySchemaObject = SchemaObject | AsyncSchema
2,281
function hasRef(schema: AnySchemaObject): boolean { for (const key in schema) { let sch: AnySchemaObject if (key === "ref" || (typeof (sch = schema[key]) == "object" && hasRef(sch))) return true } return false }
type AnySchemaObject = SchemaObject | AsyncSchema
2,282
function checkNullable( {gen, data, parentSchema}: KeywordCxt, cond: Code = nil ): [Name, Code] { const valid = gen.name("valid") if (parentSchema.nullable) { gen.let(valid, _`${data} === null`) cond = not(valid) } else { gen.let(valid, false) } return [valid, cond] }
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,283
function checkNullable( {gen, data, parentSchema}: KeywordCxt, cond: Code = nil ): [Name, Code] { const valid = gen.name("valid") if (parentSchema.nullable) { gen.let(valid, _`${data} === null`) cond = not(valid) } else { gen.let(valid, false) } return [valid, cond] }
type Code = _Code | Name
2,284
function checkNullableObject(cxt: KeywordCxt, cond: Code): [Name, Code] { const [valid, cond_] = checkNullable(cxt, cond) return [valid, _`${cond_} && typeof ${cxt.data} == "object" && !Array.isArray(${cxt.data})`] }
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,285
function checkNullableObject(cxt: KeywordCxt, cond: Code): [Name, Code] { const [valid, cond_] = checkNullable(cxt, cond) return [valid, _`${cond_} && typeof ${cxt.data} == "object" && !Array.isArray(${cxt.data})`] }
type Code = _Code | Name
2,286
code(cxt: KeywordCxt) { checkMetadata(cxt) const {gen, data, schema, it} = cxt if (alwaysValidSchema(it, schema)) return const [valid] = checkNullable(cxt) gen.if(not(valid), () => gen.if( _`Array.isArray(${data})`, () => gen.assign(valid, validateArray(cxt)), () => cxt...
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,287
code(cxt: KeywordCxt) { checkMetadata(cxt) const {gen, schema, it} = cxt if (alwaysValidSchema(it, schema)) return const valid = gen.name("valid") cxt.subschema({keyword: "metadata", jtdMetadata: true}, valid) cxt.ok(valid) }
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,288
function checkMetadata({it, keyword}: KeywordCxt, metadata?: boolean): void { if (it.jtdMetadata !== metadata) { throw new Error(`JTD: "${keyword}" cannot be used in this schema location`) } }
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,289
code(cxt: KeywordCxt) { checkMetadata(cxt) const {gen, data, schema, parentSchema} = cxt const [valid, cond] = checkNullableObject(cxt, data) gen.if(cond) validateDiscriminator() gen.elseIf(not(valid)) cxt.error() gen.endIf() cxt.ok(valid) function validateDiscriminator(): void...
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,290
function validateMapping(tag: Name): void { gen.if(false) for (const tagValue in parentSchema.mapping) { gen.elseIf(_`${tag} === ${tagValue}`) gen.assign(valid, applyTagSchema(tagValue)) } gen.else() cxt.error( false, {discrError: DiscrError.Mapping, tag}, ...
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,291
code(cxt: KeywordCxt) { checkMetadata(cxt) const {gen, data, schema, it} = cxt const [valid, cond] = checkNullableObject(cxt, data) if (alwaysValidSchema(it, schema)) { gen.if(not(or(cond, valid)), () => cxt.error()) } else { gen.if(cond) gen.assign(valid, validateMap()) gen....
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,292
function typeErrorMessage({parentSchema}: KeywordErrorCxt, t: string): string { return parentSchema?.nullable ? `must be ${t} or null` : `must be ${t}` }
interface KeywordErrorCxt { gen: CodeGen keyword: string data: Name $data?: string | false schema: any // TODO parentSchema?: AnySchemaObject schemaCode: Code | number | boolean schemaValue: Code | number | boolean schemaType?: JSONType[] errsCount?: Name params: KeywordCxtParams it: SchemaCxt }
2,293
function typeErrorParams({parentSchema}: KeywordErrorCxt, t: string): Code { return _`{type: ${t}, nullable: ${!!parentSchema?.nullable}}` }
interface KeywordErrorCxt { gen: CodeGen keyword: string data: Name $data?: string | false schema: any // TODO parentSchema?: AnySchemaObject schemaCode: Code | number | boolean schemaValue: Code | number | boolean schemaType?: JSONType[] errsCount?: Name params: KeywordCxtParams it: SchemaCxt }
2,294
code(cxt: KeywordCxt) { if (cxt.parentSchema.properties) return validateProperties(cxt) }
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,295
function timestampCode(cxt: KeywordCxt): Code { const {gen, data, it} = cxt const {timestamp, allowDate} = it.opts if (timestamp === "date") return _`${data} instanceof Date ` const vts = useFunc(gen, validTimestamp) const allowDateArg = allowDate ? _`, true` : nil const validString = _`typeof ${data} == "s...
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,296
code(cxt: KeywordCxt) { checkMetadata(cxt) const {data, schema, parentSchema, it} = cxt let cond: Code switch (schema) { case "boolean": case "string": cond = _`typeof ${data} == ${schema}` break case "timestamp": { cond = timestampCode(cxt) break ...
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,297
function validateProperties(cxt: KeywordCxt): void { checkMetadata(cxt) const {gen, data, parentSchema, it} = cxt const {additionalProperties, nullable} = parentSchema if (it.jtdDiscriminator && nullable) throw new Error("JTD: nullable inside discriminator mapping") if (commonProperties()) { throw new Err...
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,298
(key: Name) => { const addProp = isAdditional(key, allProps, "properties", it.jtdDiscriminator) const addOptProp = isAdditional(key, allOptProps, "optionalProperties") const extra = addProp === true ? addOptProp : addOptProp === true ? addProp : and(addProp, addOptProp) gen.if(extra, () ...
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,299
function isAdditional( key: Name, props: string[], keyword: string, jtdDiscriminator?: string ): Code | true { let additional: Code | boolean if (props.length > 8) { // TODO maybe an option instead of hard-coded 8? const propsSchema = schemaRefOrVal(it, parentSchema[keyword], keywo...
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...