You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							178 lines
						
					
					
						
							6.9 KiB
						
					
					
				
			
		
		
	
	
							178 lines
						
					
					
						
							6.9 KiB
						
					
					
				"use strict";
 | 
						|
Object.defineProperty(exports, "__esModule", { value: true });
 | 
						|
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;
 | 
						|
const codegen_1 = require("./codegen");
 | 
						|
const code_1 = require("./codegen/code");
 | 
						|
// 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.strictSchema)
 | 
						|
        return;
 | 
						|
    if (typeof schema === "boolean")
 | 
						|
        return;
 | 
						|
    const rules = self.RULES.keywords;
 | 
						|
    for (const key in schema) {
 | 
						|
        if (!rules[key])
 | 
						|
            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 (0, codegen_1._) `${schema}`;
 | 
						|
    }
 | 
						|
    return (0, codegen_1._) `${topSchemaRef}${schemaPath}${(0, 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((0, codegen_1._) `${to} !== true && ${from} !== undefined`, () => {
 | 
						|
            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})`));
 | 
						|
        }),
 | 
						|
        mergeToName: (gen, from, to) => gen.if((0, codegen_1._) `${to} !== true`, () => {
 | 
						|
            if (from === true) {
 | 
						|
                gen.assign(to, true);
 | 
						|
            }
 | 
						|
            else {
 | 
						|
                gen.assign(to, (0, 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((0, codegen_1._) `${to} !== true && ${from} !== undefined`, () => gen.assign(to, (0, codegen_1._) `${from} === true ? true : ${to} > ${from} ? ${to} : ${from}`)),
 | 
						|
        mergeToName: (gen, from, to) => gen.if((0, codegen_1._) `${to} !== true`, () => gen.assign(to, from === true ? true : (0, 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", (0, 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((0, codegen_1._) `${props}${(0, codegen_1.getProperty)(p)}`, true));
 | 
						|
}
 | 
						|
exports.setEvaluated = setEvaluated;
 | 
						|
const snippets = {};
 | 
						|
function useFunc(gen, f) {
 | 
						|
    return gen.scopeValue("func", {
 | 
						|
        ref: f,
 | 
						|
        code: snippets[f.code] || (snippets[f.code] = new code_1._Code(f.code)),
 | 
						|
    });
 | 
						|
}
 | 
						|
exports.useFunc = useFunc;
 | 
						|
var Type;
 | 
						|
(function (Type) {
 | 
						|
    Type[Type["Num"] = 0] = "Num";
 | 
						|
    Type[Type["Str"] = 1] = "Str";
 | 
						|
})(Type = exports.Type || (exports.Type = {}));
 | 
						|
function getErrorPath(dataProp, dataPropType, jsPropertySyntax) {
 | 
						|
    // let path
 | 
						|
    if (dataProp instanceof codegen_1.Name) {
 | 
						|
        const isNumber = dataPropType === Type.Num;
 | 
						|
        return jsPropertySyntax
 | 
						|
            ? isNumber
 | 
						|
                ? (0, codegen_1._) `"[" + ${dataProp} + "]"`
 | 
						|
                : (0, codegen_1._) `"['" + ${dataProp} + "']"`
 | 
						|
            : isNumber
 | 
						|
                ? (0, codegen_1._) `"/" + ${dataProp}`
 | 
						|
                : (0, codegen_1._) `"/" + ${dataProp}.replace(/~/g, "~0").replace(/\\//g, "~1")`; // TODO maybe use global escapePointer
 | 
						|
    }
 | 
						|
    return jsPropertySyntax ? (0, codegen_1.getProperty)(dataProp).toString() : "/" + escapeJsonPointer(dataProp);
 | 
						|
}
 | 
						|
exports.getErrorPath = getErrorPath;
 | 
						|
function checkStrictMode(it, msg, mode = it.opts.strictSchema) {
 | 
						|
    if (!mode)
 | 
						|
        return;
 | 
						|
    msg = `strict mode: ${msg}`;
 | 
						|
    if (mode === true)
 | 
						|
        throw new Error(msg);
 | 
						|
    it.self.logger.warn(msg);
 | 
						|
}
 | 
						|
exports.checkStrictMode = checkStrictMode;
 | 
						|
//# sourceMappingURL=util.js.map
 |