1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | const code_1 = require("../code");
|
---|
4 | const codegen_1 = require("../../compile/codegen");
|
---|
5 | const util_1 = require("../../compile/util");
|
---|
6 | const error = {
|
---|
7 | message: ({ params: { missingProperty } }) => codegen_1.str `must have required property '${missingProperty}'`,
|
---|
8 | params: ({ params: { missingProperty } }) => codegen_1._ `{missingProperty: ${missingProperty}}`,
|
---|
9 | };
|
---|
10 | const def = {
|
---|
11 | keyword: "required",
|
---|
12 | type: "object",
|
---|
13 | schemaType: "array",
|
---|
14 | $data: true,
|
---|
15 | error,
|
---|
16 | code(cxt) {
|
---|
17 | const { gen, schema, schemaCode, data, $data, it } = cxt;
|
---|
18 | const { opts } = it;
|
---|
19 | if (!$data && schema.length === 0)
|
---|
20 | return;
|
---|
21 | const useLoop = schema.length >= opts.loopRequired;
|
---|
22 | if (it.allErrors)
|
---|
23 | allErrorsMode();
|
---|
24 | else
|
---|
25 | exitOnErrorMode();
|
---|
26 | if (opts.strictRequired) {
|
---|
27 | const props = cxt.parentSchema.properties;
|
---|
28 | const { definedProperties } = cxt.it;
|
---|
29 | for (const requiredKey of schema) {
|
---|
30 | if ((props === null || props === void 0 ? void 0 : props[requiredKey]) === undefined && !definedProperties.has(requiredKey)) {
|
---|
31 | const schemaPath = it.schemaEnv.baseId + it.errSchemaPath;
|
---|
32 | const msg = `required property "${requiredKey}" is not defined at "${schemaPath}" (strictRequired)`;
|
---|
33 | util_1.checkStrictMode(it, msg, it.opts.strictRequired);
|
---|
34 | }
|
---|
35 | }
|
---|
36 | }
|
---|
37 | function allErrorsMode() {
|
---|
38 | if (useLoop || $data) {
|
---|
39 | cxt.block$data(codegen_1.nil, loopAllRequired);
|
---|
40 | }
|
---|
41 | else {
|
---|
42 | for (const prop of schema) {
|
---|
43 | code_1.checkReportMissingProp(cxt, prop);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 | function exitOnErrorMode() {
|
---|
48 | const missing = gen.let("missing");
|
---|
49 | if (useLoop || $data) {
|
---|
50 | const valid = gen.let("valid", true);
|
---|
51 | cxt.block$data(valid, () => loopUntilMissing(missing, valid));
|
---|
52 | cxt.ok(valid);
|
---|
53 | }
|
---|
54 | else {
|
---|
55 | gen.if(code_1.checkMissingProp(cxt, schema, missing));
|
---|
56 | code_1.reportMissingProp(cxt, missing);
|
---|
57 | gen.else();
|
---|
58 | }
|
---|
59 | }
|
---|
60 | function loopAllRequired() {
|
---|
61 | gen.forOf("prop", schemaCode, (prop) => {
|
---|
62 | cxt.setParams({ missingProperty: prop });
|
---|
63 | gen.if(code_1.noPropertyInData(gen, data, prop, opts.ownProperties), () => cxt.error());
|
---|
64 | });
|
---|
65 | }
|
---|
66 | function loopUntilMissing(missing, valid) {
|
---|
67 | cxt.setParams({ missingProperty: missing });
|
---|
68 | gen.forOf(missing, schemaCode, () => {
|
---|
69 | gen.assign(valid, code_1.propertyInData(gen, data, missing, opts.ownProperties));
|
---|
70 | gen.if(codegen_1.not(valid), () => {
|
---|
71 | cxt.error();
|
---|
72 | gen.break();
|
---|
73 | });
|
---|
74 | }, codegen_1.nil);
|
---|
75 | }
|
---|
76 | },
|
---|
77 | };
|
---|
78 | exports.default = def;
|
---|
79 | //# sourceMappingURL=required.js.map |
---|