source: trip-planner-front/node_modules/ajv/dist/compile/util.js@ 1ad8e64

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

initial commit

  • Property mode set to 100644
File size: 6.8 KB
RevLine 
[6a3a178]1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.checkStrictMode = exports.getErrorPath = exports.Type = exports.useFunc = exports.setEvaluated = exports.evaluatedPropsToName = exports.mergeEvaluated = exports.eachItem = exports.unescapeJsonPointer = exports.escapeJsonPointer = exports.escapeFragment = exports.unescapeFragment = exports.schemaRefOrVal = exports.schemaHasRulesButRef = exports.schemaHasRules = exports.checkUnknownRules = exports.alwaysValidSchema = exports.toHash = void 0;
4const codegen_1 = require("./codegen");
5const code_1 = require("./codegen/code");
6// TODO refactor to use Set
7function toHash(arr) {
8 const hash = {};
9 for (const item of arr)
10 hash[item] = true;
11 return hash;
12}
13exports.toHash = toHash;
14function alwaysValidSchema(it, schema) {
15 if (typeof schema == "boolean")
16 return schema;
17 if (Object.keys(schema).length === 0)
18 return true;
19 checkUnknownRules(it, schema);
20 return !schemaHasRules(schema, it.self.RULES.all);
21}
22exports.alwaysValidSchema = alwaysValidSchema;
23function checkUnknownRules(it, schema = it.schema) {
24 const { opts, self } = it;
25 if (!opts.strictSchema)
26 return;
27 if (typeof schema === "boolean")
28 return;
29 const rules = self.RULES.keywords;
30 for (const key in schema) {
31 if (!rules[key])
32 checkStrictMode(it, `unknown keyword: "${key}"`);
33 }
34}
35exports.checkUnknownRules = checkUnknownRules;
36function schemaHasRules(schema, rules) {
37 if (typeof schema == "boolean")
38 return !schema;
39 for (const key in schema)
40 if (rules[key])
41 return true;
42 return false;
43}
44exports.schemaHasRules = schemaHasRules;
45function schemaHasRulesButRef(schema, RULES) {
46 if (typeof schema == "boolean")
47 return !schema;
48 for (const key in schema)
49 if (key !== "$ref" && RULES.all[key])
50 return true;
51 return false;
52}
53exports.schemaHasRulesButRef = schemaHasRulesButRef;
54function schemaRefOrVal({ topSchemaRef, schemaPath }, schema, keyword, $data) {
55 if (!$data) {
56 if (typeof schema == "number" || typeof schema == "boolean")
57 return schema;
58 if (typeof schema == "string")
59 return codegen_1._ `${schema}`;
60 }
61 return codegen_1._ `${topSchemaRef}${schemaPath}${codegen_1.getProperty(keyword)}`;
62}
63exports.schemaRefOrVal = schemaRefOrVal;
64function unescapeFragment(str) {
65 return unescapeJsonPointer(decodeURIComponent(str));
66}
67exports.unescapeFragment = unescapeFragment;
68function escapeFragment(str) {
69 return encodeURIComponent(escapeJsonPointer(str));
70}
71exports.escapeFragment = escapeFragment;
72function escapeJsonPointer(str) {
73 if (typeof str == "number")
74 return `${str}`;
75 return str.replace(/~/g, "~0").replace(/\//g, "~1");
76}
77exports.escapeJsonPointer = escapeJsonPointer;
78function unescapeJsonPointer(str) {
79 return str.replace(/~1/g, "/").replace(/~0/g, "~");
80}
81exports.unescapeJsonPointer = unescapeJsonPointer;
82function eachItem(xs, f) {
83 if (Array.isArray(xs)) {
84 for (const x of xs)
85 f(x);
86 }
87 else {
88 f(xs);
89 }
90}
91exports.eachItem = eachItem;
92function makeMergeEvaluated({ mergeNames, mergeToName, mergeValues, resultToName, }) {
93 return (gen, from, to, toName) => {
94 const res = to === undefined
95 ? from
96 : to instanceof codegen_1.Name
97 ? (from instanceof codegen_1.Name ? mergeNames(gen, from, to) : mergeToName(gen, from, to), to)
98 : from instanceof codegen_1.Name
99 ? (mergeToName(gen, to, from), from)
100 : mergeValues(from, to);
101 return toName === codegen_1.Name && !(res instanceof codegen_1.Name) ? resultToName(gen, res) : res;
102 };
103}
104exports.mergeEvaluated = {
105 props: makeMergeEvaluated({
106 mergeNames: (gen, from, to) => gen.if(codegen_1._ `${to} !== true && ${from} !== undefined`, () => {
107 gen.if(codegen_1._ `${from} === true`, () => gen.assign(to, true), () => gen.assign(to, codegen_1._ `${to} || {}`).code(codegen_1._ `Object.assign(${to}, ${from})`));
108 }),
109 mergeToName: (gen, from, to) => gen.if(codegen_1._ `${to} !== true`, () => {
110 if (from === true) {
111 gen.assign(to, true);
112 }
113 else {
114 gen.assign(to, codegen_1._ `${to} || {}`);
115 setEvaluated(gen, to, from);
116 }
117 }),
118 mergeValues: (from, to) => (from === true ? true : { ...from, ...to }),
119 resultToName: evaluatedPropsToName,
120 }),
121 items: makeMergeEvaluated({
122 mergeNames: (gen, from, to) => gen.if(codegen_1._ `${to} !== true && ${from} !== undefined`, () => gen.assign(to, codegen_1._ `${from} === true ? true : ${to} > ${from} ? ${to} : ${from}`)),
123 mergeToName: (gen, from, to) => gen.if(codegen_1._ `${to} !== true`, () => gen.assign(to, from === true ? true : codegen_1._ `${to} > ${from} ? ${to} : ${from}`)),
124 mergeValues: (from, to) => (from === true ? true : Math.max(from, to)),
125 resultToName: (gen, items) => gen.var("items", items),
126 }),
127};
128function evaluatedPropsToName(gen, ps) {
129 if (ps === true)
130 return gen.var("props", true);
131 const props = gen.var("props", codegen_1._ `{}`);
132 if (ps !== undefined)
133 setEvaluated(gen, props, ps);
134 return props;
135}
136exports.evaluatedPropsToName = evaluatedPropsToName;
137function setEvaluated(gen, props, ps) {
138 Object.keys(ps).forEach((p) => gen.assign(codegen_1._ `${props}${codegen_1.getProperty(p)}`, true));
139}
140exports.setEvaluated = setEvaluated;
141const snippets = {};
142function useFunc(gen, f) {
143 return gen.scopeValue("func", {
144 ref: f,
145 code: snippets[f.code] || (snippets[f.code] = new code_1._Code(f.code)),
146 });
147}
148exports.useFunc = useFunc;
149var Type;
150(function (Type) {
151 Type[Type["Num"] = 0] = "Num";
152 Type[Type["Str"] = 1] = "Str";
153})(Type = exports.Type || (exports.Type = {}));
154function getErrorPath(dataProp, dataPropType, jsPropertySyntax) {
155 // let path
156 if (dataProp instanceof codegen_1.Name) {
157 const isNumber = dataPropType === Type.Num;
158 return jsPropertySyntax
159 ? isNumber
160 ? codegen_1._ `"[" + ${dataProp} + "]"`
161 : codegen_1._ `"['" + ${dataProp} + "']"`
162 : isNumber
163 ? codegen_1._ `"/" + ${dataProp}`
164 : codegen_1._ `"/" + ${dataProp}.replace(/~/g, "~0").replace(/\\//g, "~1")`; // TODO maybe use global escapePointer
165 }
166 return jsPropertySyntax ? codegen_1.getProperty(dataProp).toString() : "/" + escapeJsonPointer(dataProp);
167}
168exports.getErrorPath = getErrorPath;
169function checkStrictMode(it, msg, mode = it.opts.strictSchema) {
170 if (!mode)
171 return;
172 msg = `strict mode: ${msg}`;
173 if (mode === true)
174 throw new Error(msg);
175 it.self.logger.warn(msg);
176}
177exports.checkStrictMode = checkStrictMode;
178//# sourceMappingURL=util.js.map
Note: See TracBrowser for help on using the repository browser.