metadata.ts 716 B

123456789101112131415161718192021222324
  1. import {KeywordCxt} from "../../ajv"
  2. import type {CodeKeywordDefinition} from "../../types"
  3. import {alwaysValidSchema} from "../../compile/util"
  4. const def: CodeKeywordDefinition = {
  5. keyword: "metadata",
  6. schemaType: "object",
  7. code(cxt: KeywordCxt) {
  8. checkMetadata(cxt)
  9. const {gen, schema, it} = cxt
  10. if (alwaysValidSchema(it, schema)) return
  11. const valid = gen.name("valid")
  12. cxt.subschema({keyword: "metadata", jtdMetadata: true}, valid)
  13. cxt.ok(valid)
  14. },
  15. }
  16. export function checkMetadata({it, keyword}: KeywordCxt, metadata?: boolean): void {
  17. if (it.jtdMetadata !== metadata) {
  18. throw new Error(`JTD: "${keyword}" cannot be used in this schema location`)
  19. }
  20. }
  21. export default def