1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.validateSchemaDeps = exports.validatePropertyDeps = exports.error = void 0;
|
4 | const codegen_1 = require("../../compile/codegen");
|
5 | const util_1 = require("../../compile/util");
|
6 | const code_1 = require("../code");
|
7 | exports.error = {
|
8 | message: ({ params: { property, depsCount, deps } }) => {
|
9 | const property_ies = depsCount === 1 ? "property" : "properties";
|
10 | return (0, codegen_1.str) `must have ${property_ies} ${deps} when property ${property} is present`;
|
11 | },
|
12 | params: ({ params: { property, depsCount, deps, missingProperty } }) => (0, codegen_1._) `{property: ${property},
|
13 | missingProperty: ${missingProperty},
|
14 | depsCount: ${depsCount},
|
15 | deps: ${deps}}`,
|
16 | };
|
17 | const def = {
|
18 | keyword: "dependencies",
|
19 | type: "object",
|
20 | schemaType: "object",
|
21 | error: exports.error,
|
22 | code(cxt) {
|
23 | const [propDeps, schDeps] = splitDependencies(cxt);
|
24 | validatePropertyDeps(cxt, propDeps);
|
25 | validateSchemaDeps(cxt, schDeps);
|
26 | },
|
27 | };
|
28 | function splitDependencies({ schema }) {
|
29 | const propertyDeps = {};
|
30 | const schemaDeps = {};
|
31 | for (const key in schema) {
|
32 | if (key === "__proto__")
|
33 | continue;
|
34 | const deps = Array.isArray(schema[key]) ? propertyDeps : schemaDeps;
|
35 | deps[key] = schema[key];
|
36 | }
|
37 | return [propertyDeps, schemaDeps];
|
38 | }
|
39 | function validatePropertyDeps(cxt, propertyDeps = cxt.schema) {
|
40 | const { gen, data, it } = cxt;
|
41 | if (Object.keys(propertyDeps).length === 0)
|
42 | return;
|
43 | const missing = gen.let("missing");
|
44 | for (const prop in propertyDeps) {
|
45 | const deps = propertyDeps[prop];
|
46 | if (deps.length === 0)
|
47 | continue;
|
48 | const hasProperty = (0, code_1.propertyInData)(gen, data, prop, it.opts.ownProperties);
|
49 | cxt.setParams({
|
50 | property: prop,
|
51 | depsCount: deps.length,
|
52 | deps: deps.join(", "),
|
53 | });
|
54 | if (it.allErrors) {
|
55 | gen.if(hasProperty, () => {
|
56 | for (const depProp of deps) {
|
57 | (0, code_1.checkReportMissingProp)(cxt, depProp);
|
58 | }
|
59 | });
|
60 | }
|
61 | else {
|
62 | gen.if((0, codegen_1._) `${hasProperty} && (${(0, code_1.checkMissingProp)(cxt, deps, missing)})`);
|
63 | (0, code_1.reportMissingProp)(cxt, missing);
|
64 | gen.else();
|
65 | }
|
66 | }
|
67 | }
|
68 | exports.validatePropertyDeps = validatePropertyDeps;
|
69 | function validateSchemaDeps(cxt, schemaDeps = cxt.schema) {
|
70 | const { gen, data, keyword, it } = cxt;
|
71 | const valid = gen.name("valid");
|
72 | for (const prop in schemaDeps) {
|
73 | if ((0, util_1.alwaysValidSchema)(it, schemaDeps[prop]))
|
74 | continue;
|
75 | gen.if((0, code_1.propertyInData)(gen, data, prop, it.opts.ownProperties), () => {
|
76 | const schCxt = cxt.subschema({ keyword, schemaProp: prop }, valid);
|
77 | cxt.mergeValidEvaluated(schCxt, valid);
|
78 | }, () => gen.var(valid, true)
|
79 | );
|
80 | cxt.ok(valid);
|
81 | }
|
82 | }
|
83 | exports.validateSchemaDeps = validateSchemaDeps;
|
84 | exports.default = def;
|
85 |
|
\ | No newline at end of file |