1 | import {KeywordCxt} from "../../ajv"
|
2 | import type {CodeKeywordDefinition} from "../../types"
|
3 | import {alwaysValidSchema} from "../../compile/util"
|
4 |
|
5 | const 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 |
|
18 | export 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 |
|
24 | export default def
|