hexo/node_modules/ajv/dist/compile/errors.js

121 lines
5.4 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extendErrors = exports.resetErrorsCount = exports.reportExtraError = exports.reportError = exports.keyword$DataError = exports.keywordError = void 0;
const codegen_1 = require("./codegen");
const names_1 = require("./names");
exports.keywordError = {
message: ({ keyword }) => codegen_1.str `should pass "${keyword}" keyword validation`,
};
exports.keyword$DataError = {
message: ({ keyword, schemaType }) => schemaType
? codegen_1.str `"${keyword}" keyword must be ${schemaType} ($data)`
: codegen_1.str `"${keyword}" keyword is invalid ($data)`,
};
function reportError(cxt, error = exports.keywordError, overrideAllErrors) {
const { it } = cxt;
const { gen, compositeRule, allErrors } = it;
const errObj = errorObjectCode(cxt, error);
if (overrideAllErrors !== null && overrideAllErrors !== void 0 ? overrideAllErrors : (compositeRule || allErrors)) {
addError(gen, errObj);
}
else {
returnErrors(it, codegen_1._ `[${errObj}]`);
}
}
exports.reportError = reportError;
function reportExtraError(cxt, error = exports.keywordError) {
const { it } = cxt;
const { gen, compositeRule, allErrors } = it;
const errObj = errorObjectCode(cxt, error);
addError(gen, errObj);
if (!(compositeRule || allErrors)) {
returnErrors(it, names_1.default.vErrors);
}
}
exports.reportExtraError = reportExtraError;
function resetErrorsCount(gen, errsCount) {
gen.assign(names_1.default.errors, errsCount);
gen.if(codegen_1._ `${names_1.default.vErrors} !== null`, () => gen.if(errsCount, () => gen.assign(codegen_1._ `${names_1.default.vErrors}.length`, errsCount), () => gen.assign(names_1.default.vErrors, null)));
}
exports.resetErrorsCount = resetErrorsCount;
function extendErrors({ gen, keyword, schemaValue, data, errsCount, it, }) {
/* istanbul ignore if */
if (errsCount === undefined)
throw new Error("ajv implementation error");
const err = gen.name("err");
gen.forRange("i", errsCount, names_1.default.errors, (i) => {
gen.const(err, codegen_1._ `${names_1.default.vErrors}[${i}]`);
gen.if(codegen_1._ `${err}.dataPath === undefined`, () => gen.assign(codegen_1._ `${err}.dataPath`, codegen_1.strConcat(names_1.default.dataPath, it.errorPath)));
gen.assign(codegen_1._ `${err}.schemaPath`, codegen_1.str `${it.errSchemaPath}/${keyword}`);
if (it.opts.verbose) {
gen.assign(codegen_1._ `${err}.schema`, schemaValue);
gen.assign(codegen_1._ `${err}.data`, data);
}
});
}
exports.extendErrors = extendErrors;
function addError(gen, errObj) {
const err = gen.const("err", errObj);
gen.if(codegen_1._ `${names_1.default.vErrors} === null`, () => gen.assign(names_1.default.vErrors, codegen_1._ `[${err}]`), codegen_1._ `${names_1.default.vErrors}.push(${err})`);
gen.code(codegen_1._ `${names_1.default.errors}++`);
}
function returnErrors(it, errs) {
const { gen, validateName, schemaEnv } = it;
if (schemaEnv.$async) {
gen.throw(codegen_1._ `new ${it.ValidationError}(${errs})`);
}
else {
gen.assign(codegen_1._ `${validateName}.errors`, errs);
gen.return(false);
}
}
const E = {
keyword: new codegen_1.Name("keyword"),
schemaPath: new codegen_1.Name("schemaPath"),
params: new codegen_1.Name("params"),
propertyName: new codegen_1.Name("propertyName"),
message: new codegen_1.Name("message"),
schema: new codegen_1.Name("schema"),
parentSchema: new codegen_1.Name("parentSchema"),
// JTD error properties
instancePath: new codegen_1.Name("instancePath"),
};
function errorObjectCode(cxt, error) {
const { createErrors, opts } = cxt.it;
if (createErrors === false)
return codegen_1._ `{}`;
return (opts.jtd && !opts.ajvErrors ? jtdErrorObject : ajvErrorObject)(cxt, error);
}
function jtdErrorObject(cxt, { message }) {
const { gen, keyword, it } = cxt;
const { errorPath, errSchemaPath, opts } = it;
const keyValues = [
[E.instancePath, codegen_1.strConcat(names_1.default.dataPath, errorPath)],
[E.schemaPath, codegen_1.str `${errSchemaPath}/${keyword}`],
];
if (opts.messages) {
keyValues.push([E.message, typeof message == "function" ? message(cxt) : message]);
}
return gen.object(...keyValues);
}
function ajvErrorObject(cxt, error) {
const { gen, keyword, data, schemaValue, it } = cxt;
const { topSchemaRef, schemaPath, errorPath, errSchemaPath, propertyName, opts } = it;
const { params, message } = error;
const keyValues = [
[E.keyword, keyword],
[names_1.default.dataPath, codegen_1.strConcat(names_1.default.dataPath, errorPath)],
[E.schemaPath, codegen_1.str `${errSchemaPath}/${keyword}`],
[E.params, typeof params == "function" ? params(cxt) : params || codegen_1._ `{}`],
];
if (propertyName)
keyValues.push([E.propertyName, propertyName]);
if (opts.messages) {
keyValues.push([E.message, typeof message == "function" ? message(cxt) : message]);
}
if (opts.verbose) {
keyValues.push([E.schema, schemaValue], [E.parentSchema, codegen_1._ `${topSchemaRef}${schemaPath}`], [names_1.default.data, data]);
}
return gen.object(...keyValues);
}
//# sourceMappingURL=errors.js.map