dynamicRef.ts 1.8 KB

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