[6a3a178] | 1 | import type {AnySchemaObject, SchemaObject, JTDParser} from "./types"
|
---|
| 2 | import type {JTDSchemaType, SomeJTDSchemaType, 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"
|
---|
| 9 |
|
---|
| 10 | const META_SCHEMA_ID = "JTD-meta-schema"
|
---|
| 11 |
|
---|
| 12 | type JTDOptions = CurrentOptions & {
|
---|
| 13 | // strict mode options not supported with JTD:
|
---|
| 14 | strict?: never
|
---|
| 15 | allowMatchingProperties?: never
|
---|
| 16 | allowUnionTypes?: never
|
---|
| 17 | validateFormats?: never
|
---|
| 18 | // validation and reporting options not supported with JTD:
|
---|
| 19 | $data?: never
|
---|
| 20 | verbose?: boolean
|
---|
| 21 | $comment?: never
|
---|
| 22 | formats?: never
|
---|
| 23 | loadSchema?: never
|
---|
| 24 | // options to modify validated data:
|
---|
| 25 | useDefaults?: never
|
---|
| 26 | coerceTypes?: never
|
---|
| 27 | // advanced options:
|
---|
| 28 | next?: never
|
---|
| 29 | unevaluated?: never
|
---|
| 30 | dynamicRef?: never
|
---|
| 31 | meta?: boolean
|
---|
| 32 | defaultMeta?: never
|
---|
| 33 | inlineRefs?: boolean
|
---|
| 34 | loopRequired?: never
|
---|
| 35 | multipleOfPrecision?: never
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | class Ajv extends AjvCore {
|
---|
| 39 | constructor(opts: JTDOptions = {}) {
|
---|
| 40 | super({
|
---|
| 41 | ...opts,
|
---|
| 42 | jtd: true,
|
---|
| 43 | })
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | _addVocabularies(): void {
|
---|
| 47 | super._addVocabularies()
|
---|
| 48 | this.addVocabulary(jtdVocabulary)
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | _addDefaultMetaSchema(): void {
|
---|
| 52 | super._addDefaultMetaSchema()
|
---|
| 53 | if (!this.opts.meta) return
|
---|
| 54 | this.addMetaSchema(jtdMetaSchema, META_SCHEMA_ID, false)
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | defaultMeta(): string | AnySchemaObject | undefined {
|
---|
| 58 | return (this.opts.defaultMeta =
|
---|
| 59 | super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined))
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 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)
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 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>
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 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")
|
---|
| 84 | return sch.serialize
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 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")
|
---|
| 91 | return sch.parse
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | module.exports = exports = Ajv
|
---|
| 96 | Object.defineProperty(exports, "__esModule", {value: true})
|
---|
| 97 |
|
---|
| 98 | export default Ajv
|
---|
| 99 |
|
---|
| 100 | export {
|
---|
| 101 | Format,
|
---|
| 102 | FormatDefinition,
|
---|
| 103 | AsyncFormatDefinition,
|
---|
| 104 | KeywordDefinition,
|
---|
| 105 | KeywordErrorDefinition,
|
---|
| 106 | CodeKeywordDefinition,
|
---|
| 107 | MacroKeywordDefinition,
|
---|
| 108 | FuncKeywordDefinition,
|
---|
| 109 | Vocabulary,
|
---|
| 110 | Schema,
|
---|
| 111 | SchemaObject,
|
---|
| 112 | AnySchemaObject,
|
---|
| 113 | AsyncSchema,
|
---|
| 114 | AnySchema,
|
---|
| 115 | ValidateFunction,
|
---|
| 116 | AsyncValidateFunction,
|
---|
| 117 | ErrorObject,
|
---|
| 118 | ErrorNoParams,
|
---|
| 119 | JTDParser,
|
---|
| 120 | } from "./types"
|
---|
| 121 |
|
---|
| 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"
|
---|
| 127 |
|
---|
| 128 | export {JTDSchemaType, SomeJTDSchemaType, JTDDataType}
|
---|
| 129 | export {JTDOptions}
|
---|