source: imaps-frontend/node_modules/ajv/lib/jtd.ts@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.1 KB
Line 
1import type {AnySchemaObject, SchemaObject, JTDParser} from "./types"
2import type {JTDSchemaType, SomeJTDSchemaType, JTDDataType} from "./types/jtd-schema"
3import AjvCore, {CurrentOptions} from "./core"
4import jtdVocabulary from "./vocabularies/jtd"
5import jtdMetaSchema from "./refs/jtd-schema"
6import compileSerializer from "./compile/jtd/serialize"
7import compileParser from "./compile/jtd/parse"
8import {SchemaEnv} from "./compile"
9
10const META_SCHEMA_ID = "JTD-meta-schema"
11
12type 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
38export 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
95module.exports = exports = Ajv
96module.exports.Ajv = Ajv
97Object.defineProperty(exports, "__esModule", {value: true})
98
99export default Ajv
100
101export {
102 Format,
103 FormatDefinition,
104 AsyncFormatDefinition,
105 KeywordDefinition,
106 KeywordErrorDefinition,
107 CodeKeywordDefinition,
108 MacroKeywordDefinition,
109 FuncKeywordDefinition,
110 Vocabulary,
111 Schema,
112 SchemaObject,
113 AnySchemaObject,
114 AsyncSchema,
115 AnySchema,
116 ValidateFunction,
117 AsyncValidateFunction,
118 ErrorObject,
119 ErrorNoParams,
120 JTDParser,
121} from "./types"
122
123export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core"
124export {SchemaCxt, SchemaObjCxt} from "./compile"
125export {KeywordCxt} from "./compile/validate"
126export {JTDErrorObject} from "./vocabularies/jtd"
127export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"
128
129export {JTDSchemaType, SomeJTDSchemaType, JTDDataType}
130export {JTDOptions}
131export {default as ValidationError} from "./runtime/validation_error"
132export {default as MissingRefError} from "./compile/ref_error"
Note: See TracBrowser for help on using the repository browser.