| 1 | import type {CodeKeywordDefinition} from "../../types" |
| 2 | import type {KeywordCxt} from "../../compile/validate" |
| 3 | import {_, getProperty, Code, Name} from "../../compile/codegen" |
| 4 | import N from "../../compile/names" |
| 5 | import {callRef} from "../core/ref" |
| 6 | |
| 7 | const def: CodeKeywordDefinition = { |
| 8 | keyword: "$dynamicRef", |
| 9 | schemaType: "string", |
| 10 | code: (cxt) => dynamicRef(cxt, cxt.schema), |
| 11 | } |
| 12 | |
| 13 | export function dynamicRef(cxt: KeywordCxt, ref: string): void { |
| 14 | const {gen, keyword, it} = cxt |
| 15 | if (ref[0] !== "#") throw new Error(`"${keyword}" only supports hash fragment reference`) |
| 16 | const anchor = ref.slice(1) |
| 17 | if (it.allErrors) { |
| 18 | _dynamicRef() |
| 19 | } else { |
| 20 | const valid = gen.let("valid", false) |
| 21 | _dynamicRef(valid) |
| 22 | cxt.ok(valid) |
| 23 | } |
| 24 | |
| 25 | function _dynamicRef(valid?: Name): void { |
| 26 | // TODO the assumption here is that `recursiveRef: #` always points to the root |
| 27 | // of the schema object, which is not correct, because there may be $id that |
| 28 | // makes # point to it, and the target schema may not contain dynamic/recursiveAnchor. |
| 29 | // Because of that 2 tests in recursiveRef.json fail. |
| 30 | // This is a similar problem to #815 (`$id` doesn't alter resolution scope for `{ "$ref": "#" }`). |
| 31 | // (This problem is not tested in JSON-Schema-Test-Suite) |
| 32 | if (it.schemaEnv.root.dynamicAnchors[anchor]) { |
| 33 | const v = gen.let("_v", _`${N.dynamicAnchors}${getProperty(anchor)}`) |
| 34 | gen.if(v, _callRef(v, valid), _callRef(it.validateName, valid)) |
| 35 | } else { |
| 36 | _callRef(it.validateName, valid)() |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | function _callRef(validate: Code, valid?: Name): () => void { |
| 41 | return valid |
| 42 | ? () => |
| 43 | gen.block(() => { |
| 44 | callRef(cxt, validate) |
| 45 | gen.let(valid, true) |
| 46 | }) |
| 47 | : () => callRef(cxt, validate) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | export default def |