patternProperties.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import type {CodeKeywordDefinition} from "../../types"
  2. import type KeywordCxt from "../../compile/context"
  3. import {schemaProperties, usePattern} from "../code"
  4. import {_, not, Name} from "../../compile/codegen"
  5. import {Type} from "../../compile/subschema"
  6. import {checkStrictMode} from "../../compile/validate"
  7. import {evaluatedPropsToName} from "../../compile/util"
  8. const def: CodeKeywordDefinition = {
  9. keyword: "patternProperties",
  10. type: "object",
  11. schemaType: "object",
  12. code(cxt: KeywordCxt) {
  13. const {gen, schema, data, parentSchema, it} = cxt
  14. const {opts} = it
  15. const patterns = schemaProperties(it, schema)
  16. // TODO mark properties matching patterns with always valid schemas as evaluated
  17. if (patterns.length === 0) return
  18. const checkProperties = opts.strict && !opts.allowMatchingProperties && parentSchema.properties
  19. const valid = gen.name("valid")
  20. if (it.props !== true && !(it.props instanceof Name)) {
  21. it.props = evaluatedPropsToName(gen, it.props)
  22. }
  23. const {props} = it
  24. validatePatternProperties()
  25. function validatePatternProperties(): void {
  26. for (const pat of patterns) {
  27. if (checkProperties) checkMatchingProperties(pat)
  28. if (it.allErrors) {
  29. validateProperties(pat)
  30. } else {
  31. gen.var(valid, true) // TODO var
  32. validateProperties(pat)
  33. gen.if(valid)
  34. }
  35. }
  36. }
  37. function checkMatchingProperties(pat: string): void {
  38. for (const prop in checkProperties) {
  39. if (new RegExp(pat).test(prop)) {
  40. checkStrictMode(
  41. it,
  42. `property ${prop} matches pattern ${pat} (use allowMatchingProperties)`
  43. )
  44. }
  45. }
  46. }
  47. function validateProperties(pat: string): void {
  48. gen.forIn("key", data, (key) => {
  49. gen.if(_`${usePattern(gen, pat)}.test(${key})`, () => {
  50. cxt.subschema(
  51. {
  52. keyword: "patternProperties",
  53. schemaProp: pat,
  54. dataProp: key,
  55. dataPropType: Type.Str,
  56. strictSchema: it.strictSchema,
  57. },
  58. valid
  59. )
  60. if (it.opts.unevaluated && props !== true) {
  61. gen.assign(_`${props}[${key}]`, true)
  62. } else if (!it.allErrors) {
  63. // can short-circuit if `unevaluatedProperties` is not supported (opts.next === false)
  64. // or if all properties were evaluated (props === true)
  65. gen.if(not(valid), () => gen.break())
  66. }
  67. })
  68. })
  69. }
  70. },
  71. }
  72. export default def