[6a3a178] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | exports.validateUnion = exports.validateArray = exports.usePattern = exports.callValidateCode = exports.schemaProperties = exports.allSchemaProperties = exports.noPropertyInData = exports.propertyInData = exports.isOwnProperty = exports.hasPropFunc = exports.reportMissingProp = exports.checkMissingProp = exports.checkReportMissingProp = void 0;
|
---|
| 4 | const codegen_1 = require("../compile/codegen");
|
---|
| 5 | const util_1 = require("../compile/util");
|
---|
| 6 | const names_1 = require("../compile/names");
|
---|
| 7 | function checkReportMissingProp(cxt, prop) {
|
---|
| 8 | const { gen, data, it } = cxt;
|
---|
| 9 | gen.if(noPropertyInData(gen, data, prop, it.opts.ownProperties), () => {
|
---|
| 10 | cxt.setParams({ missingProperty: codegen_1._ `${prop}` }, true);
|
---|
| 11 | cxt.error();
|
---|
| 12 | });
|
---|
| 13 | }
|
---|
| 14 | exports.checkReportMissingProp = checkReportMissingProp;
|
---|
| 15 | function checkMissingProp({ gen, data, it: { opts } }, properties, missing) {
|
---|
| 16 | return codegen_1.or(...properties.map((prop) => codegen_1.and(noPropertyInData(gen, data, prop, opts.ownProperties), codegen_1._ `${missing} = ${prop}`)));
|
---|
| 17 | }
|
---|
| 18 | exports.checkMissingProp = checkMissingProp;
|
---|
| 19 | function reportMissingProp(cxt, missing) {
|
---|
| 20 | cxt.setParams({ missingProperty: missing }, true);
|
---|
| 21 | cxt.error();
|
---|
| 22 | }
|
---|
| 23 | exports.reportMissingProp = reportMissingProp;
|
---|
| 24 | function hasPropFunc(gen) {
|
---|
| 25 | return gen.scopeValue("func", {
|
---|
| 26 | // eslint-disable-next-line @typescript-eslint/unbound-method
|
---|
| 27 | ref: Object.prototype.hasOwnProperty,
|
---|
| 28 | code: codegen_1._ `Object.prototype.hasOwnProperty`,
|
---|
| 29 | });
|
---|
| 30 | }
|
---|
| 31 | exports.hasPropFunc = hasPropFunc;
|
---|
| 32 | function isOwnProperty(gen, data, property) {
|
---|
| 33 | return codegen_1._ `${hasPropFunc(gen)}.call(${data}, ${property})`;
|
---|
| 34 | }
|
---|
| 35 | exports.isOwnProperty = isOwnProperty;
|
---|
| 36 | function propertyInData(gen, data, property, ownProperties) {
|
---|
| 37 | const cond = codegen_1._ `${data}${codegen_1.getProperty(property)} !== undefined`;
|
---|
| 38 | return ownProperties ? codegen_1._ `${cond} && ${isOwnProperty(gen, data, property)}` : cond;
|
---|
| 39 | }
|
---|
| 40 | exports.propertyInData = propertyInData;
|
---|
| 41 | function noPropertyInData(gen, data, property, ownProperties) {
|
---|
| 42 | const cond = codegen_1._ `${data}${codegen_1.getProperty(property)} === undefined`;
|
---|
| 43 | return ownProperties ? codegen_1.or(cond, codegen_1.not(isOwnProperty(gen, data, property))) : cond;
|
---|
| 44 | }
|
---|
| 45 | exports.noPropertyInData = noPropertyInData;
|
---|
| 46 | function allSchemaProperties(schemaMap) {
|
---|
| 47 | return schemaMap ? Object.keys(schemaMap).filter((p) => p !== "__proto__") : [];
|
---|
| 48 | }
|
---|
| 49 | exports.allSchemaProperties = allSchemaProperties;
|
---|
| 50 | function schemaProperties(it, schemaMap) {
|
---|
| 51 | return allSchemaProperties(schemaMap).filter((p) => !util_1.alwaysValidSchema(it, schemaMap[p]));
|
---|
| 52 | }
|
---|
| 53 | exports.schemaProperties = schemaProperties;
|
---|
| 54 | function callValidateCode({ schemaCode, data, it: { gen, topSchemaRef, schemaPath, errorPath }, it }, func, context, passSchema) {
|
---|
| 55 | const dataAndSchema = passSchema ? codegen_1._ `${schemaCode}, ${data}, ${topSchemaRef}${schemaPath}` : data;
|
---|
| 56 | const valCxt = [
|
---|
| 57 | [names_1.default.instancePath, codegen_1.strConcat(names_1.default.instancePath, errorPath)],
|
---|
| 58 | [names_1.default.parentData, it.parentData],
|
---|
| 59 | [names_1.default.parentDataProperty, it.parentDataProperty],
|
---|
| 60 | [names_1.default.rootData, names_1.default.rootData],
|
---|
| 61 | ];
|
---|
| 62 | if (it.opts.dynamicRef)
|
---|
| 63 | valCxt.push([names_1.default.dynamicAnchors, names_1.default.dynamicAnchors]);
|
---|
| 64 | const args = codegen_1._ `${dataAndSchema}, ${gen.object(...valCxt)}`;
|
---|
| 65 | return context !== codegen_1.nil ? codegen_1._ `${func}.call(${context}, ${args})` : codegen_1._ `${func}(${args})`;
|
---|
| 66 | }
|
---|
| 67 | exports.callValidateCode = callValidateCode;
|
---|
| 68 | function usePattern({ gen, it: { opts } }, pattern) {
|
---|
| 69 | const u = opts.unicodeRegExp ? "u" : "";
|
---|
| 70 | return gen.scopeValue("pattern", {
|
---|
| 71 | key: pattern,
|
---|
| 72 | ref: new RegExp(pattern, u),
|
---|
| 73 | code: codegen_1._ `new RegExp(${pattern}, ${u})`,
|
---|
| 74 | });
|
---|
| 75 | }
|
---|
| 76 | exports.usePattern = usePattern;
|
---|
| 77 | function validateArray(cxt) {
|
---|
| 78 | const { gen, data, keyword, it } = cxt;
|
---|
| 79 | const valid = gen.name("valid");
|
---|
| 80 | if (it.allErrors) {
|
---|
| 81 | const validArr = gen.let("valid", true);
|
---|
| 82 | validateItems(() => gen.assign(validArr, false));
|
---|
| 83 | return validArr;
|
---|
| 84 | }
|
---|
| 85 | gen.var(valid, true);
|
---|
| 86 | validateItems(() => gen.break());
|
---|
| 87 | return valid;
|
---|
| 88 | function validateItems(notValid) {
|
---|
| 89 | const len = gen.const("len", codegen_1._ `${data}.length`);
|
---|
| 90 | gen.forRange("i", 0, len, (i) => {
|
---|
| 91 | cxt.subschema({
|
---|
| 92 | keyword,
|
---|
| 93 | dataProp: i,
|
---|
| 94 | dataPropType: util_1.Type.Num,
|
---|
| 95 | }, valid);
|
---|
| 96 | gen.if(codegen_1.not(valid), notValid);
|
---|
| 97 | });
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | exports.validateArray = validateArray;
|
---|
| 101 | function validateUnion(cxt) {
|
---|
| 102 | const { gen, schema, keyword, it } = cxt;
|
---|
| 103 | /* istanbul ignore if */
|
---|
| 104 | if (!Array.isArray(schema))
|
---|
| 105 | throw new Error("ajv implementation error");
|
---|
| 106 | const alwaysValid = schema.some((sch) => util_1.alwaysValidSchema(it, sch));
|
---|
| 107 | if (alwaysValid && !it.opts.unevaluated)
|
---|
| 108 | return;
|
---|
| 109 | const valid = gen.let("valid", false);
|
---|
| 110 | const schValid = gen.name("_valid");
|
---|
| 111 | gen.block(() => schema.forEach((_sch, i) => {
|
---|
| 112 | const schCxt = cxt.subschema({
|
---|
| 113 | keyword,
|
---|
| 114 | schemaProp: i,
|
---|
| 115 | compositeRule: true,
|
---|
| 116 | }, schValid);
|
---|
| 117 | gen.assign(valid, codegen_1._ `${valid} || ${schValid}`);
|
---|
| 118 | const merged = cxt.mergeValidEvaluated(schCxt, schValid);
|
---|
| 119 | // can short-circuit if `unevaluatedProperties/Items` not supported (opts.unevaluated !== true)
|
---|
| 120 | // or if all properties and items were evaluated (it.props === true && it.items === true)
|
---|
| 121 | if (!merged)
|
---|
| 122 | gen.if(codegen_1.not(valid));
|
---|
| 123 | }));
|
---|
| 124 | cxt.result(valid, () => cxt.reset(), () => cxt.error(true));
|
---|
| 125 | }
|
---|
| 126 | exports.validateUnion = validateUnion;
|
---|
| 127 | //# sourceMappingURL=code.js.map |
---|