UNPKG

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