123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- 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;
- const codegen_1 = require("./codegen");
- const validate_1 = require("./validate");
- // TODO refactor to use Set
- function toHash(arr) {
- const hash = {};
- for (const item of arr)
- hash[item] = true;
- return hash;
- }
- exports.toHash = toHash;
- function alwaysValidSchema(it, schema) {
- if (typeof schema == "boolean")
- return schema;
- if (Object.keys(schema).length === 0)
- return true;
- checkUnknownRules(it, schema);
- return !schemaHasRules(schema, it.self.RULES.all);
- }
- exports.alwaysValidSchema = alwaysValidSchema;
- function checkUnknownRules(it, schema = it.schema) {
- const { opts, self } = it;
- if (!opts.strict)
- return;
- if (typeof schema === "boolean")
- return;
- const rules = self.RULES.keywords;
- for (const key in schema) {
- if (!rules[key])
- validate_1.checkStrictMode(it, `unknown keyword: "${key}"`);
- }
- }
- exports.checkUnknownRules = checkUnknownRules;
- function schemaHasRules(schema, rules) {
- if (typeof schema == "boolean")
- return !schema;
- for (const key in schema)
- if (rules[key])
- return true;
- return false;
- }
- exports.schemaHasRules = schemaHasRules;
- function schemaHasRulesButRef(schema, RULES) {
- if (typeof schema == "boolean")
- return !schema;
- for (const key in schema)
- if (key !== "$ref" && RULES.all[key])
- return true;
- return false;
- }
- exports.schemaHasRulesButRef = schemaHasRulesButRef;
- function schemaRefOrVal({ topSchemaRef, schemaPath }, schema, keyword, $data) {
- if (!$data) {
- if (typeof schema == "number" || typeof schema == "boolean")
- return schema;
- if (typeof schema == "string")
- return codegen_1._ `${schema}`;
- }
- return codegen_1._ `${topSchemaRef}${schemaPath}${codegen_1.getProperty(keyword)}`;
- }
- exports.schemaRefOrVal = schemaRefOrVal;
- function unescapeFragment(str) {
- return unescapeJsonPointer(decodeURIComponent(str));
- }
- exports.unescapeFragment = unescapeFragment;
- function escapeFragment(str) {
- return encodeURIComponent(escapeJsonPointer(str));
- }
- exports.escapeFragment = escapeFragment;
- function escapeJsonPointer(str) {
- if (typeof str == "number")
- return `${str}`;
- return str.replace(/~/g, "~0").replace(/\//g, "~1");
- }
- exports.escapeJsonPointer = escapeJsonPointer;
- function unescapeJsonPointer(str) {
- return str.replace(/~1/g, "/").replace(/~0/g, "~");
- }
- exports.unescapeJsonPointer = unescapeJsonPointer;
- function eachItem(xs, f) {
- if (Array.isArray(xs)) {
- for (const x of xs)
- f(x);
- }
- else {
- f(xs);
- }
- }
- exports.eachItem = eachItem;
- function makeMergeEvaluated({ mergeNames, mergeToName, mergeValues, resultToName, }) {
- return (gen, from, to, toName) => {
- const res = to === undefined
- ? from
- : to instanceof codegen_1.Name
- ? (from instanceof codegen_1.Name ? mergeNames(gen, from, to) : mergeToName(gen, from, to), to)
- : from instanceof codegen_1.Name
- ? (mergeToName(gen, to, from), from)
- : mergeValues(from, to);
- return toName === codegen_1.Name && !(res instanceof codegen_1.Name) ? resultToName(gen, res) : res;
- };
- }
- exports.mergeEvaluated = {
- props: makeMergeEvaluated({
- mergeNames: (gen, from, to) => gen.if(codegen_1._ `${to} !== true && ${from} !== undefined`, () => {
- gen.if(codegen_1._ `${from} === true`, () => gen.assign(to, true), () => gen.code(codegen_1._ `Object.assign(${to}, ${from})`));
- }),
- mergeToName: (gen, from, to) => gen.if(codegen_1._ `${to} !== true`, () => {
- if (from === true) {
- gen.assign(to, true);
- }
- else {
- gen.assign(to, codegen_1._ `${to} || {}`);
- setEvaluated(gen, to, from);
- }
- }),
- mergeValues: (from, to) => (from === true ? true : { ...from, ...to }),
- resultToName: evaluatedPropsToName,
- }),
- items: makeMergeEvaluated({
- mergeNames: (gen, from, to) => gen.if(codegen_1._ `${to} !== true && ${from} !== undefined`, () => gen.assign(to, codegen_1._ `${from} === true ? true : ${to} > ${from} ? ${to} : ${from}`)),
- mergeToName: (gen, from, to) => gen.if(codegen_1._ `${to} !== true`, () => gen.assign(to, from === true ? true : codegen_1._ `${to} > ${from} ? ${to} : ${from}`)),
- mergeValues: (from, to) => (from === true ? true : Math.max(from, to)),
- resultToName: (gen, items) => gen.var("items", items),
- }),
- };
- function evaluatedPropsToName(gen, ps) {
- if (ps === true)
- return gen.var("props", true);
- const props = gen.var("props", codegen_1._ `{}`);
- if (ps !== undefined)
- setEvaluated(gen, props, ps);
- return props;
- }
- exports.evaluatedPropsToName = evaluatedPropsToName;
- function setEvaluated(gen, props, ps) {
- Object.keys(ps).forEach((p) => gen.assign(codegen_1._ `${props}${codegen_1.getProperty(p)}`, true));
- }
- exports.setEvaluated = setEvaluated;
- //# sourceMappingURL=util.js.map
|