jtd.ts 4.1 KB

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