1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | const codegen_1 = require("../../compile/codegen");
|
---|
4 | const util_1 = require("../../compile/util");
|
---|
5 | const error = {
|
---|
6 | message: ({ params: { min, max } }) => max === undefined
|
---|
7 | ? codegen_1.str `must contain at least ${min} valid item(s)`
|
---|
8 | : codegen_1.str `must contain at least ${min} and no more than ${max} valid item(s)`,
|
---|
9 | params: ({ params: { min, max } }) => max === undefined ? codegen_1._ `{minContains: ${min}}` : codegen_1._ `{minContains: ${min}, maxContains: ${max}}`,
|
---|
10 | };
|
---|
11 | const def = {
|
---|
12 | keyword: "contains",
|
---|
13 | type: "array",
|
---|
14 | schemaType: ["object", "boolean"],
|
---|
15 | before: "uniqueItems",
|
---|
16 | trackErrors: true,
|
---|
17 | error,
|
---|
18 | code(cxt) {
|
---|
19 | const { gen, schema, parentSchema, data, it } = cxt;
|
---|
20 | let min;
|
---|
21 | let max;
|
---|
22 | const { minContains, maxContains } = parentSchema;
|
---|
23 | if (it.opts.next) {
|
---|
24 | min = minContains === undefined ? 1 : minContains;
|
---|
25 | max = maxContains;
|
---|
26 | }
|
---|
27 | else {
|
---|
28 | min = 1;
|
---|
29 | }
|
---|
30 | const len = gen.const("len", codegen_1._ `${data}.length`);
|
---|
31 | cxt.setParams({ min, max });
|
---|
32 | if (max === undefined && min === 0) {
|
---|
33 | util_1.checkStrictMode(it, `"minContains" == 0 without "maxContains": "contains" keyword ignored`);
|
---|
34 | return;
|
---|
35 | }
|
---|
36 | if (max !== undefined && min > max) {
|
---|
37 | util_1.checkStrictMode(it, `"minContains" > "maxContains" is always invalid`);
|
---|
38 | cxt.fail();
|
---|
39 | return;
|
---|
40 | }
|
---|
41 | if (util_1.alwaysValidSchema(it, schema)) {
|
---|
42 | let cond = codegen_1._ `${len} >= ${min}`;
|
---|
43 | if (max !== undefined)
|
---|
44 | cond = codegen_1._ `${cond} && ${len} <= ${max}`;
|
---|
45 | cxt.pass(cond);
|
---|
46 | return;
|
---|
47 | }
|
---|
48 | it.items = true;
|
---|
49 | const valid = gen.name("valid");
|
---|
50 | if (max === undefined && min === 1) {
|
---|
51 | validateItems(valid, () => gen.if(valid, () => gen.break()));
|
---|
52 | }
|
---|
53 | else {
|
---|
54 | gen.let(valid, false);
|
---|
55 | const schValid = gen.name("_valid");
|
---|
56 | const count = gen.let("count", 0);
|
---|
57 | validateItems(schValid, () => gen.if(schValid, () => checkLimits(count)));
|
---|
58 | }
|
---|
59 | cxt.result(valid, () => cxt.reset());
|
---|
60 | function validateItems(_valid, block) {
|
---|
61 | gen.forRange("i", 0, len, (i) => {
|
---|
62 | cxt.subschema({
|
---|
63 | keyword: "contains",
|
---|
64 | dataProp: i,
|
---|
65 | dataPropType: util_1.Type.Num,
|
---|
66 | compositeRule: true,
|
---|
67 | }, _valid);
|
---|
68 | block();
|
---|
69 | });
|
---|
70 | }
|
---|
71 | function checkLimits(count) {
|
---|
72 | gen.code(codegen_1._ `${count}++`);
|
---|
73 | if (max === undefined) {
|
---|
74 | gen.if(codegen_1._ `${count} >= ${min}`, () => gen.assign(valid, true).break());
|
---|
75 | }
|
---|
76 | else {
|
---|
77 | gen.if(codegen_1._ `${count} > ${max}`, () => gen.assign(valid, false).break());
|
---|
78 | if (min === 1)
|
---|
79 | gen.assign(valid, true);
|
---|
80 | else
|
---|
81 | gen.if(codegen_1._ `${count} >= ${min}`, () => gen.assign(valid, true));
|
---|
82 | }
|
---|
83 | }
|
---|
84 | },
|
---|
85 | };
|
---|
86 | exports.default = def;
|
---|
87 | //# sourceMappingURL=contains.js.map |
---|