1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.extendErrors = exports.resetErrorsCount = exports.reportExtraError = exports.reportError = exports.keyword$DataError = exports.keywordError = void 0;
|
---|
4 | const codegen_1 = require("./codegen");
|
---|
5 | const util_1 = require("./util");
|
---|
6 | const names_1 = require("./names");
|
---|
7 | exports.keywordError = {
|
---|
8 | message: ({ keyword }) => codegen_1.str `must pass "${keyword}" keyword validation`,
|
---|
9 | };
|
---|
10 | exports.keyword$DataError = {
|
---|
11 | message: ({ keyword, schemaType }) => schemaType
|
---|
12 | ? codegen_1.str `"${keyword}" keyword must be ${schemaType} ($data)`
|
---|
13 | : codegen_1.str `"${keyword}" keyword is invalid ($data)`,
|
---|
14 | };
|
---|
15 | function 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, codegen_1._ `[${errObj}]`);
|
---|
24 | }
|
---|
25 | }
|
---|
26 | exports.reportError = reportError;
|
---|
27 | function 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 | }
|
---|
36 | exports.reportExtraError = reportExtraError;
|
---|
37 | function resetErrorsCount(gen, errsCount) {
|
---|
38 | gen.assign(names_1.default.errors, errsCount);
|
---|
39 | 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)));
|
---|
40 | }
|
---|
41 | exports.resetErrorsCount = resetErrorsCount;
|
---|
42 | function 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, codegen_1._ `${names_1.default.vErrors}[${i}]`);
|
---|
49 | gen.if(codegen_1._ `${err}.instancePath === undefined`, () => gen.assign(codegen_1._ `${err}.instancePath`, codegen_1.strConcat(names_1.default.instancePath, it.errorPath)));
|
---|
50 | gen.assign(codegen_1._ `${err}.schemaPath`, codegen_1.str `${it.errSchemaPath}/${keyword}`);
|
---|
51 | if (it.opts.verbose) {
|
---|
52 | gen.assign(codegen_1._ `${err}.schema`, schemaValue);
|
---|
53 | gen.assign(codegen_1._ `${err}.data`, data);
|
---|
54 | }
|
---|
55 | });
|
---|
56 | }
|
---|
57 | exports.extendErrors = extendErrors;
|
---|
58 | function addError(gen, errObj) {
|
---|
59 | const err = gen.const("err", errObj);
|
---|
60 | 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})`);
|
---|
61 | gen.code(codegen_1._ `${names_1.default.errors}++`);
|
---|
62 | }
|
---|
63 | function returnErrors(it, errs) {
|
---|
64 | const { gen, validateName, schemaEnv } = it;
|
---|
65 | if (schemaEnv.$async) {
|
---|
66 | gen.throw(codegen_1._ `new ${it.ValidationError}(${errs})`);
|
---|
67 | }
|
---|
68 | else {
|
---|
69 | gen.assign(codegen_1._ `${validateName}.errors`, errs);
|
---|
70 | gen.return(false);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | const 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 | };
|
---|
82 | function errorObjectCode(cxt, error, errorPaths) {
|
---|
83 | const { createErrors } = cxt.it;
|
---|
84 | if (createErrors === false)
|
---|
85 | return codegen_1._ `{}`;
|
---|
86 | return errorObject(cxt, error, errorPaths);
|
---|
87 | }
|
---|
88 | function 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 | }
|
---|
97 | function errorInstancePath({ errorPath }, { instancePath }) {
|
---|
98 | const instPath = instancePath
|
---|
99 | ? codegen_1.str `${errorPath}${util_1.getErrorPath(instancePath, util_1.Type.Str)}`
|
---|
100 | : errorPath;
|
---|
101 | return [names_1.default.instancePath, codegen_1.strConcat(names_1.default.instancePath, instPath)];
|
---|
102 | }
|
---|
103 | function errorSchemaPath({ keyword, it: { errSchemaPath } }, { schemaPath, parentSchema }) {
|
---|
104 | let schPath = parentSchema ? errSchemaPath : codegen_1.str `${errSchemaPath}/${keyword}`;
|
---|
105 | if (schemaPath) {
|
---|
106 | schPath = codegen_1.str `${schPath}${util_1.getErrorPath(schemaPath, util_1.Type.Str)}`;
|
---|
107 | }
|
---|
108 | return [E.schemaPath, schPath];
|
---|
109 | }
|
---|
110 | function 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 || 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, 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 |
---|