jtd.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. export {
  2. Format,
  3. FormatDefinition,
  4. AsyncFormatDefinition,
  5. KeywordDefinition,
  6. KeywordErrorDefinition,
  7. CodeKeywordDefinition,
  8. MacroKeywordDefinition,
  9. FuncKeywordDefinition,
  10. Vocabulary,
  11. Schema,
  12. SchemaObject,
  13. AnySchemaObject,
  14. AsyncSchema,
  15. AnySchema,
  16. ValidateFunction,
  17. AsyncValidateFunction,
  18. ErrorObject,
  19. ErrorNoParams,
  20. JTDParser,
  21. } from "./types"
  22. export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core"
  23. export {SchemaCxt, SchemaObjCxt} from "./compile"
  24. import KeywordCxt from "./compile/context"
  25. export {KeywordCxt}
  26. // export {DefinedError} from "./vocabularies/errors"
  27. export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"
  28. import type {AnySchemaObject, SchemaObject, JTDParser} from "./types"
  29. import type {JTDSchemaType, JTDDataType} from "./types/jtd-schema"
  30. export {JTDSchemaType, JTDDataType}
  31. import AjvCore, {CurrentOptions} from "./core"
  32. import jtdVocabulary from "./vocabularies/jtd"
  33. import jtdMetaSchema from "./refs/jtd-schema"
  34. import compileSerializer from "./compile/jtd/serialize"
  35. import compileParser from "./compile/jtd/parse"
  36. import {SchemaEnv} from "./compile"
  37. // const META_SUPPORT_DATA = ["/properties"]
  38. const META_SCHEMA_ID = "JTD-meta-schema"
  39. export type JTDOptions = CurrentOptions & {
  40. // strict mode options not supported with JTD:
  41. strictTypes?: never
  42. strictTuples?: never
  43. allowMatchingProperties?: never
  44. allowUnionTypes?: never
  45. validateFormats?: never
  46. // validation and reporting options not supported with JTD:
  47. $data?: never
  48. verbose?: never
  49. $comment?: never
  50. formats?: never
  51. loadSchema?: never
  52. // options to modify validated data:
  53. useDefaults?: never
  54. coerceTypes?: never
  55. // advanced options:
  56. next?: never
  57. unevaluated?: never
  58. dynamicRef?: never
  59. meta?: boolean
  60. defaultMeta?: never
  61. inlineRefs?: boolean
  62. loopRequired?: never
  63. multipleOfPrecision?: never
  64. ajvErrors?: boolean
  65. }
  66. export default class Ajv extends AjvCore {
  67. constructor(opts: JTDOptions = {}) {
  68. super({
  69. ...opts,
  70. jtd: true,
  71. messages: opts.messages ?? false,
  72. })
  73. }
  74. _addVocabularies(): void {
  75. super._addVocabularies()
  76. this.addVocabulary(jtdVocabulary)
  77. }
  78. _addDefaultMetaSchema(): void {
  79. super._addDefaultMetaSchema()
  80. if (!this.opts.meta) return
  81. this.addMetaSchema(jtdMetaSchema, META_SCHEMA_ID, false)
  82. }
  83. defaultMeta(): string | AnySchemaObject | undefined {
  84. return (this.opts.defaultMeta =
  85. super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined))
  86. }
  87. compileSerializer<T = unknown>(schema: SchemaObject): (data: T) => string
  88. // Separated for type inference to work
  89. // eslint-disable-next-line @typescript-eslint/unified-signatures
  90. compileSerializer<T = unknown>(schema: JTDSchemaType<T>): (data: T) => string
  91. compileSerializer<T = unknown>(schema: SchemaObject): (data: T) => string {
  92. const sch = this._addSchema(schema)
  93. return sch.serialize || this._compileSerializer(sch)
  94. }
  95. compileParser<T = unknown>(schema: SchemaObject): JTDParser<T>
  96. // Separated for type inference to work
  97. // eslint-disable-next-line @typescript-eslint/unified-signatures
  98. compileParser<T = unknown>(schema: JTDSchemaType<T>): JTDParser<T>
  99. compileParser<T = unknown>(schema: SchemaObject): JTDParser<T> {
  100. const sch = this._addSchema(schema)
  101. return (sch.parse || this._compileParser(sch)) as JTDParser<T>
  102. }
  103. private _compileSerializer<T>(sch: SchemaEnv): (data: T) => string {
  104. compileSerializer.call(this, sch, (sch.schema as AnySchemaObject).definitions || {})
  105. /* istanbul ignore if */
  106. if (!sch.serialize) throw new Error("ajv implementation error")
  107. return sch.serialize
  108. }
  109. private _compileParser(sch: SchemaEnv): JTDParser {
  110. compileParser.call(this, sch, (sch.schema as AnySchemaObject).definitions || {})
  111. /* istanbul ignore if */
  112. if (!sch.parse) throw new Error("ajv implementation error")
  113. return sch.parse
  114. }
  115. }