1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.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;
|
---|
4 | const codegen_1 = require("./codegen");
|
---|
5 | const code_1 = require("./codegen/code");
|
---|
6 | // TODO refactor to use Set
|
---|
7 | function toHash(arr) {
|
---|
8 | const hash = {};
|
---|
9 | for (const item of arr)
|
---|
10 | hash[item] = true;
|
---|
11 | return hash;
|
---|
12 | }
|
---|
13 | exports.toHash = toHash;
|
---|
14 | function 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 | }
|
---|
22 | exports.alwaysValidSchema = alwaysValidSchema;
|
---|
23 | function 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 | }
|
---|
35 | exports.checkUnknownRules = checkUnknownRules;
|
---|
36 | function 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 | }
|
---|
44 | exports.schemaHasRules = schemaHasRules;
|
---|
45 | function 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 | }
|
---|
53 | exports.schemaHasRulesButRef = schemaHasRulesButRef;
|
---|
54 | function 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 | }
|
---|
63 | exports.schemaRefOrVal = schemaRefOrVal;
|
---|
64 | function unescapeFragment(str) {
|
---|
65 | return unescapeJsonPointer(decodeURIComponent(str));
|
---|
66 | }
|
---|
67 | exports.unescapeFragment = unescapeFragment;
|
---|
68 | function escapeFragment(str) {
|
---|
69 | return encodeURIComponent(escapeJsonPointer(str));
|
---|
70 | }
|
---|
71 | exports.escapeFragment = escapeFragment;
|
---|
72 | function escapeJsonPointer(str) {
|
---|
73 | if (typeof str == "number")
|
---|
74 | return `${str}`;
|
---|
75 | return str.replace(/~/g, "~0").replace(/\//g, "~1");
|
---|
76 | }
|
---|
77 | exports.escapeJsonPointer = escapeJsonPointer;
|
---|
78 | function unescapeJsonPointer(str) {
|
---|
79 | return str.replace(/~1/g, "/").replace(/~0/g, "~");
|
---|
80 | }
|
---|
81 | exports.unescapeJsonPointer = unescapeJsonPointer;
|
---|
82 | function 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 | }
|
---|
91 | exports.eachItem = eachItem;
|
---|
92 | function 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 | }
|
---|
104 | exports.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 | };
|
---|
128 | function 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 | }
|
---|
136 | exports.evaluatedPropsToName = evaluatedPropsToName;
|
---|
137 | function setEvaluated(gen, props, ps) {
|
---|
138 | Object.keys(ps).forEach((p) => gen.assign(codegen_1._ `${props}${codegen_1.getProperty(p)}`, true));
|
---|
139 | }
|
---|
140 | exports.setEvaluated = setEvaluated;
|
---|
141 | const snippets = {};
|
---|
142 | function 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 | }
|
---|
148 | exports.useFunc = useFunc;
|
---|
149 | var Type;
|
---|
150 | (function (Type) {
|
---|
151 | Type[Type["Num"] = 0] = "Num";
|
---|
152 | Type[Type["Str"] = 1] = "Str";
|
---|
153 | })(Type = exports.Type || (exports.Type = {}));
|
---|
154 | function 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 | }
|
---|
168 | exports.getErrorPath = getErrorPath;
|
---|
169 | function 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 | }
|
---|
177 | exports.checkStrictMode = checkStrictMode;
|
---|
178 | //# sourceMappingURL=util.js.map |
---|