source: trip-planner-front/node_modules/ajv/dist/vocabularies/applicator/dependencies.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.1 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.validateSchemaDeps = exports.validatePropertyDeps = exports.error = void 0;
4const codegen_1 = require("../../compile/codegen");
5const util_1 = require("../../compile/util");
6const code_1 = require("../code");
7exports.error = {
8 message: ({ params: { property, depsCount, deps } }) => {
9 const property_ies = depsCount === 1 ? "property" : "properties";
10 return codegen_1.str `must have ${property_ies} ${deps} when property ${property} is present`;
11 },
12 params: ({ params: { property, depsCount, deps, missingProperty } }) => codegen_1._ `{property: ${property},
13 missingProperty: ${missingProperty},
14 depsCount: ${depsCount},
15 deps: ${deps}}`, // TODO change to reference
16};
17const 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};
28function 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}
39function 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 = 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 code_1.checkReportMissingProp(cxt, depProp);
58 }
59 });
60 }
61 else {
62 gen.if(codegen_1._ `${hasProperty} && (${code_1.checkMissingProp(cxt, deps, missing)})`);
63 code_1.reportMissingProp(cxt, missing);
64 gen.else();
65 }
66 }
67}
68exports.validatePropertyDeps = validatePropertyDeps;
69function 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 (util_1.alwaysValidSchema(it, schemaDeps[prop]))
74 continue;
75 gen.if(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) // TODO var
79 );
80 cxt.ok(valid);
81 }
82}
83exports.validateSchemaDeps = validateSchemaDeps;
84exports.default = def;
85//# sourceMappingURL=dependencies.js.map
Note: See TracBrowser for help on using the repository browser.