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
9 for (const item of arr)
13 exports.toHash = toHash;
14 function alwaysValidSchema(it, schema) {
15 if (typeof schema == "boolean")
17 if (Object.keys(schema).length === 0)
19 checkUnknownRules(it, schema);
20 return !schemaHasRules(schema, it.self.RULES.all);
22 exports.alwaysValidSchema = alwaysValidSchema;
23 function checkUnknownRules(it, schema = it.schema) {
24 const { opts, self } = it;
25 if (!opts.strictSchema)
27 if (typeof schema === "boolean")
29 const rules = self.RULES.keywords;
30 for (const key in schema) {
32 checkStrictMode(it, `unknown keyword: "${key}"`);
35 exports.checkUnknownRules = checkUnknownRules;
36 function schemaHasRules(schema, rules) {
37 if (typeof schema == "boolean")
39 for (const key in schema)
44 exports.schemaHasRules = schemaHasRules;
45 function schemaHasRulesButRef(schema, RULES) {
46 if (typeof schema == "boolean")
48 for (const key in schema)
49 if (key !== "$ref" && RULES.all[key])
53 exports.schemaHasRulesButRef = schemaHasRulesButRef;
54 function schemaRefOrVal({ topSchemaRef, schemaPath }, schema, keyword, $data) {
56 if (typeof schema == "number" || typeof schema == "boolean")
58 if (typeof schema == "string")
59 return (0, codegen_1._) `${schema}`;
61 return (0, codegen_1._) `${topSchemaRef}${schemaPath}${(0, codegen_1.getProperty)(keyword)}`;
63 exports.schemaRefOrVal = schemaRefOrVal;
64 function unescapeFragment(str) {
65 return unescapeJsonPointer(decodeURIComponent(str));
67 exports.unescapeFragment = unescapeFragment;
68 function escapeFragment(str) {
69 return encodeURIComponent(escapeJsonPointer(str));
71 exports.escapeFragment = escapeFragment;
72 function escapeJsonPointer(str) {
73 if (typeof str == "number")
75 return str.replace(/~/g, "~0").replace(/\//g, "~1");
77 exports.escapeJsonPointer = escapeJsonPointer;
78 function unescapeJsonPointer(str) {
79 return str.replace(/~1/g, "/").replace(/~0/g, "~");
81 exports.unescapeJsonPointer = unescapeJsonPointer;
82 function eachItem(xs, f) {
83 if (Array.isArray(xs)) {
91 exports.eachItem = eachItem;
92 function makeMergeEvaluated({ mergeNames, mergeToName, mergeValues, resultToName, }) {
93 return (gen, from, to, toName) => {
94 const res = to === undefined
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;
104 exports.mergeEvaluated = {
105 props: makeMergeEvaluated({
106 mergeNames: (gen, from, to) => gen.if((0, codegen_1._) `${to} !== true && ${from} !== undefined`, () => {
107 gen.if((0, codegen_1._) `${from} === true`, () => gen.assign(to, true), () => gen.assign(to, (0, codegen_1._) `${to} || {}`).code((0, codegen_1._) `Object.assign(${to}, ${from})`));
109 mergeToName: (gen, from, to) => gen.if((0, codegen_1._) `${to} !== true`, () => {
111 gen.assign(to, true);
114 gen.assign(to, (0, codegen_1._) `${to} || {}`);
115 setEvaluated(gen, to, from);
118 mergeValues: (from, to) => (from === true ? true : { ...from, ...to }),
119 resultToName: evaluatedPropsToName,
121 items: makeMergeEvaluated({
122 mergeNames: (gen, from, to) => gen.if((0, codegen_1._) `${to} !== true && ${from} !== undefined`, () => gen.assign(to, (0, codegen_1._) `${from} === true ? true : ${to} > ${from} ? ${to} : ${from}`)),
123 mergeToName: (gen, from, to) => gen.if((0, codegen_1._) `${to} !== true`, () => gen.assign(to, from === true ? true : (0, 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),
128 function evaluatedPropsToName(gen, ps) {
130 return gen.var("props", true);
131 const props = gen.var("props", (0, codegen_1._) `{}`);
132 if (ps !== undefined)
133 setEvaluated(gen, props, ps);
136 exports.evaluatedPropsToName = evaluatedPropsToName;
137 function setEvaluated(gen, props, ps) {
138 Object.keys(ps).forEach((p) => gen.assign((0, codegen_1._) `${props}${(0, codegen_1.getProperty)(p)}`, true));
140 exports.setEvaluated = setEvaluated;
142 function useFunc(gen, f) {
143 return gen.scopeValue("func", {
145 code: snippets[f.code] || (snippets[f.code] = new code_1._Code(f.code)),
148 exports.useFunc = useFunc;
151 Type[Type["Num"] = 0] = "Num";
152 Type[Type["Str"] = 1] = "Str";
153 })(Type = exports.Type || (exports.Type = {}));
154 function getErrorPath(dataProp, dataPropType, jsPropertySyntax) {
156 if (dataProp instanceof codegen_1.Name) {
157 const isNumber = dataPropType === Type.Num;
158 return jsPropertySyntax
160 ? (0, codegen_1._) `"[" + ${dataProp} + "]"`
161 : (0, codegen_1._) `"['" + ${dataProp} + "']"`
163 ? (0, codegen_1._) `"/" + ${dataProp}`
164 : (0, codegen_1._) `"/" + ${dataProp}.replace(/~/g, "~0").replace(/\\//g, "~1")`; // TODO maybe use global escapePointer
166 return jsPropertySyntax ? (0, codegen_1.getProperty)(dataProp).toString() : "/" + escapeJsonPointer(dataProp);
168 exports.getErrorPath = getErrorPath;
169 function checkStrictMode(it, msg, mode = it.opts.strictSchema) {
172 msg = `strict mode: ${msg}`;
174 throw new Error(msg);
175 it.self.logger.warn(msg);
177 exports.checkStrictMode = checkStrictMode;
178 //# sourceMappingURL=util.js.map