UNPKG

3.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const code_1 = require("../code");
4const codegen_1 = require("../../compile/codegen");
5const util_1 = require("../../compile/util");
6const util_2 = require("../../compile/util");
7const def = {
8 keyword: "patternProperties",
9 type: "object",
10 schemaType: "object",
11 code(cxt) {
12 const { gen, schema, data, parentSchema, it } = cxt;
13 const { opts } = it;
14 const patterns = (0, code_1.allSchemaProperties)(schema);
15 const alwaysValidPatterns = patterns.filter((p) => (0, util_1.alwaysValidSchema)(it, schema[p]));
16 if (patterns.length === 0 ||
17 (alwaysValidPatterns.length === patterns.length &&
18 (!it.opts.unevaluated || it.props === true))) {
19 return;
20 }
21 const checkProperties = opts.strictSchema && !opts.allowMatchingProperties && parentSchema.properties;
22 const valid = gen.name("valid");
23 if (it.props !== true && !(it.props instanceof codegen_1.Name)) {
24 it.props = (0, util_2.evaluatedPropsToName)(gen, it.props);
25 }
26 const { props } = it;
27 validatePatternProperties();
28 function validatePatternProperties() {
29 for (const pat of patterns) {
30 if (checkProperties)
31 checkMatchingProperties(pat);
32 if (it.allErrors) {
33 validateProperties(pat);
34 }
35 else {
36 gen.var(valid, true); // TODO var
37 validateProperties(pat);
38 gen.if(valid);
39 }
40 }
41 }
42 function checkMatchingProperties(pat) {
43 for (const prop in checkProperties) {
44 if (new RegExp(pat).test(prop)) {
45 (0, util_1.checkStrictMode)(it, `property ${prop} matches pattern ${pat} (use allowMatchingProperties)`);
46 }
47 }
48 }
49 function validateProperties(pat) {
50 gen.forIn("key", data, (key) => {
51 gen.if((0, codegen_1._) `${(0, code_1.usePattern)(cxt, pat)}.test(${key})`, () => {
52 const alwaysValid = alwaysValidPatterns.includes(pat);
53 if (!alwaysValid) {
54 cxt.subschema({
55 keyword: "patternProperties",
56 schemaProp: pat,
57 dataProp: key,
58 dataPropType: util_2.Type.Str,
59 }, valid);
60 }
61 if (it.opts.unevaluated && props !== true) {
62 gen.assign((0, codegen_1._) `${props}[${key}]`, true);
63 }
64 else if (!alwaysValid && !it.allErrors) {
65 // can short-circuit if `unevaluatedProperties` is not supported (opts.next === false)
66 // or if all properties were evaluated (props === true)
67 gen.if((0, codegen_1.not)(valid), () => gen.break());
68 }
69 });
70 });
71 }
72 },
73};
74exports.default = def;
75//# sourceMappingURL=patternProperties.js.map
\No newline at end of file