UNPKG

5.74 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.extendErrors = exports.resetErrorsCount = exports.reportExtraError = exports.reportError = exports.keyword$DataError = exports.keywordError = void 0;
4const codegen_1 = require("./codegen");
5const util_1 = require("./util");
6const names_1 = require("./names");
7exports.keywordError = {
8 message: ({ keyword }) => (0, codegen_1.str) `must pass "${keyword}" keyword validation`,
9};
10exports.keyword$DataError = {
11 message: ({ keyword, schemaType }) => schemaType
12 ? (0, codegen_1.str) `"${keyword}" keyword must be ${schemaType} ($data)`
13 : (0, codegen_1.str) `"${keyword}" keyword is invalid ($data)`,
14};
15function reportError(cxt, error = exports.keywordError, errorPaths, overrideAllErrors) {
16 const { it } = cxt;
17 const { gen, compositeRule, allErrors } = it;
18 const errObj = errorObjectCode(cxt, error, errorPaths);
19 if (overrideAllErrors !== null && overrideAllErrors !== void 0 ? overrideAllErrors : (compositeRule || allErrors)) {
20 addError(gen, errObj);
21 }
22 else {
23 returnErrors(it, (0, codegen_1._) `[${errObj}]`);
24 }
25}
26exports.reportError = reportError;
27function reportExtraError(cxt, error = exports.keywordError, errorPaths) {
28 const { it } = cxt;
29 const { gen, compositeRule, allErrors } = it;
30 const errObj = errorObjectCode(cxt, error, errorPaths);
31 addError(gen, errObj);
32 if (!(compositeRule || allErrors)) {
33 returnErrors(it, names_1.default.vErrors);
34 }
35}
36exports.reportExtraError = reportExtraError;
37function resetErrorsCount(gen, errsCount) {
38 gen.assign(names_1.default.errors, errsCount);
39 gen.if((0, codegen_1._) `${names_1.default.vErrors} !== null`, () => gen.if(errsCount, () => gen.assign((0, codegen_1._) `${names_1.default.vErrors}.length`, errsCount), () => gen.assign(names_1.default.vErrors, null)));
40}
41exports.resetErrorsCount = resetErrorsCount;
42function extendErrors({ gen, keyword, schemaValue, data, errsCount, it, }) {
43 /* istanbul ignore if */
44 if (errsCount === undefined)
45 throw new Error("ajv implementation error");
46 const err = gen.name("err");
47 gen.forRange("i", errsCount, names_1.default.errors, (i) => {
48 gen.const(err, (0, codegen_1._) `${names_1.default.vErrors}[${i}]`);
49 gen.if((0, codegen_1._) `${err}.instancePath === undefined`, () => gen.assign((0, codegen_1._) `${err}.instancePath`, (0, codegen_1.strConcat)(names_1.default.instancePath, it.errorPath)));
50 gen.assign((0, codegen_1._) `${err}.schemaPath`, (0, codegen_1.str) `${it.errSchemaPath}/${keyword}`);
51 if (it.opts.verbose) {
52 gen.assign((0, codegen_1._) `${err}.schema`, schemaValue);
53 gen.assign((0, codegen_1._) `${err}.data`, data);
54 }
55 });
56}
57exports.extendErrors = extendErrors;
58function addError(gen, errObj) {
59 const err = gen.const("err", errObj);
60 gen.if((0, codegen_1._) `${names_1.default.vErrors} === null`, () => gen.assign(names_1.default.vErrors, (0, codegen_1._) `[${err}]`), (0, codegen_1._) `${names_1.default.vErrors}.push(${err})`);
61 gen.code((0, codegen_1._) `${names_1.default.errors}++`);
62}
63function returnErrors(it, errs) {
64 const { gen, validateName, schemaEnv } = it;
65 if (schemaEnv.$async) {
66 gen.throw((0, codegen_1._) `new ${it.ValidationError}(${errs})`);
67 }
68 else {
69 gen.assign((0, codegen_1._) `${validateName}.errors`, errs);
70 gen.return(false);
71 }
72}
73const E = {
74 keyword: new codegen_1.Name("keyword"),
75 schemaPath: new codegen_1.Name("schemaPath"),
76 params: new codegen_1.Name("params"),
77 propertyName: new codegen_1.Name("propertyName"),
78 message: new codegen_1.Name("message"),
79 schema: new codegen_1.Name("schema"),
80 parentSchema: new codegen_1.Name("parentSchema"),
81};
82function errorObjectCode(cxt, error, errorPaths) {
83 const { createErrors } = cxt.it;
84 if (createErrors === false)
85 return (0, codegen_1._) `{}`;
86 return errorObject(cxt, error, errorPaths);
87}
88function errorObject(cxt, error, errorPaths = {}) {
89 const { gen, it } = cxt;
90 const keyValues = [
91 errorInstancePath(it, errorPaths),
92 errorSchemaPath(cxt, errorPaths),
93 ];
94 extraErrorProps(cxt, error, keyValues);
95 return gen.object(...keyValues);
96}
97function errorInstancePath({ errorPath }, { instancePath }) {
98 const instPath = instancePath
99 ? (0, codegen_1.str) `${errorPath}${(0, util_1.getErrorPath)(instancePath, util_1.Type.Str)}`
100 : errorPath;
101 return [names_1.default.instancePath, (0, codegen_1.strConcat)(names_1.default.instancePath, instPath)];
102}
103function errorSchemaPath({ keyword, it: { errSchemaPath } }, { schemaPath, parentSchema }) {
104 let schPath = parentSchema ? errSchemaPath : (0, codegen_1.str) `${errSchemaPath}/${keyword}`;
105 if (schemaPath) {
106 schPath = (0, codegen_1.str) `${schPath}${(0, util_1.getErrorPath)(schemaPath, util_1.Type.Str)}`;
107 }
108 return [E.schemaPath, schPath];
109}
110function extraErrorProps(cxt, { params, message }, keyValues) {
111 const { keyword, data, schemaValue, it } = cxt;
112 const { opts, propertyName, topSchemaRef, schemaPath } = it;
113 keyValues.push([E.keyword, keyword], [E.params, typeof params == "function" ? params(cxt) : params || (0, codegen_1._) `{}`]);
114 if (opts.messages) {
115 keyValues.push([E.message, typeof message == "function" ? message(cxt) : message]);
116 }
117 if (opts.verbose) {
118 keyValues.push([E.schema, schemaValue], [E.parentSchema, (0, codegen_1._) `${topSchemaRef}${schemaPath}`], [names_1.default.data, data]);
119 }
120 if (propertyName)
121 keyValues.push([E.propertyName, propertyName]);
122}
123//# sourceMappingURL=errors.js.map
\No newline at end of file