1 import type {AnySchemaObject, SchemaObject, JTDParser} from "./types"
2 import type {JTDSchemaType, JTDDataType} from "./types/jtd-schema"
3 import AjvCore, {CurrentOptions} from "./core"
4 import jtdVocabulary from "./vocabularies/jtd"
5 import jtdMetaSchema from "./refs/jtd-schema"
6 import compileSerializer from "./compile/jtd/serialize"
7 import compileParser from "./compile/jtd/parse"
8 import {SchemaEnv} from "./compile"
10 const META_SCHEMA_ID = "JTD-meta-schema"
12 type JTDOptions = CurrentOptions & {
13 // strict mode options not supported with JTD:
15 allowMatchingProperties?: never
16 allowUnionTypes?: never
17 validateFormats?: never
18 // validation and reporting options not supported with JTD:
24 // options to modify validated data:
35 multipleOfPrecision?: never
38 class Ajv extends AjvCore {
39 constructor(opts: JTDOptions = {}) {
46 _addVocabularies(): void {
47 super._addVocabularies()
48 this.addVocabulary(jtdVocabulary)
51 _addDefaultMetaSchema(): void {
52 super._addDefaultMetaSchema()
53 if (!this.opts.meta) return
54 this.addMetaSchema(jtdMetaSchema, META_SCHEMA_ID, false)
57 defaultMeta(): string | AnySchemaObject | undefined {
58 return (this.opts.defaultMeta =
59 super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined))
62 compileSerializer<T = unknown>(schema: SchemaObject): (data: T) => string
63 // Separated for type inference to work
64 // eslint-disable-next-line @typescript-eslint/unified-signatures
65 compileSerializer<T = unknown>(schema: JTDSchemaType<T>): (data: T) => string
66 compileSerializer<T = unknown>(schema: SchemaObject): (data: T) => string {
67 const sch = this._addSchema(schema)
68 return sch.serialize || this._compileSerializer(sch)
71 compileParser<T = unknown>(schema: SchemaObject): JTDParser<T>
72 // Separated for type inference to work
73 // eslint-disable-next-line @typescript-eslint/unified-signatures
74 compileParser<T = unknown>(schema: JTDSchemaType<T>): JTDParser<T>
75 compileParser<T = unknown>(schema: SchemaObject): JTDParser<T> {
76 const sch = this._addSchema(schema)
77 return (sch.parse || this._compileParser(sch)) as JTDParser<T>
80 private _compileSerializer<T>(sch: SchemaEnv): (data: T) => string {
81 compileSerializer.call(this, sch, (sch.schema as AnySchemaObject).definitions || {})
82 /* istanbul ignore if */
83 if (!sch.serialize) throw new Error("ajv implementation error")
87 private _compileParser(sch: SchemaEnv): JTDParser {
88 compileParser.call(this, sch, (sch.schema as AnySchemaObject).definitions || {})
89 /* istanbul ignore if */
90 if (!sch.parse) throw new Error("ajv implementation error")
95 module.exports = exports = Ajv
96 Object.defineProperty(exports, "__esModule", {value: true})
103 AsyncFormatDefinition,
105 KeywordErrorDefinition,
106 CodeKeywordDefinition,
107 MacroKeywordDefinition,
108 FuncKeywordDefinition,
116 AsyncValidateFunction,
122 export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core"
123 export {SchemaCxt, SchemaObjCxt} from "./compile"
124 export {KeywordCxt} from "./compile/validate"
125 export {JTDErrorObject} from "./vocabularies/jtd"
126 export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"
128 export {JTDSchemaType, JTDDataType}