ajv-errors
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -6,3 +6,3 @@ "use strict"; | ||
const code_1 = require("ajv/dist/compile/codegen/code"); | ||
const context_1 = require("ajv/dist/compile/context"); | ||
const validate_1 = require("ajv/dist/compile/validate"); | ||
const errors_1 = require("ajv/dist/compile/errors"); | ||
@@ -30,3 +30,3 @@ const names_1 = require("ajv/dist/compile/names"); | ||
const sch = schema; | ||
const dataPath = codegen_1.strConcat(names_1.default.dataPath, it.errorPath); | ||
const instancePath = codegen_1.strConcat(names_1.default.instancePath, it.errorPath); | ||
gen.if(ajv_1._ `${names_1.default.errors} > 0`, () => { | ||
@@ -179,3 +179,3 @@ if (typeof sch == "object") { | ||
}); | ||
gen.assign(ajv_1._ `${names_1.default.vErrors}[${names_1.default.errors}-1].dataPath`, ajv_1._ `${dataPath} + "/" + ${key}.replace(/~/g, "~0").replace(/\\//g, "~1")`); | ||
gen.assign(ajv_1._ `${names_1.default.vErrors}[${names_1.default.errors}-1].instancePath`, ajv_1._ `${instancePath} + "/" + ${key}.replace(/~/g, "~0").replace(/\\//g, "~1")`); | ||
})); | ||
@@ -202,3 +202,3 @@ gen.endIf(); | ||
function matchKeywordError(err, kwdErrs) { | ||
return codegen_1.and(ajv_1._ `${err}.keyword !== ${keyword}`, ajv_1._ `!${err}.${used}`, ajv_1._ `${err}.dataPath === ${dataPath}`, ajv_1._ `${err}.keyword in ${kwdErrs}`, | ||
return codegen_1.and(ajv_1._ `${err}.keyword !== ${keyword}`, ajv_1._ `!${err}.${used}`, ajv_1._ `${err}.instancePath === ${instancePath}`, ajv_1._ `${err}.keyword in ${kwdErrs}`, | ||
// TODO match the end of the string? | ||
@@ -208,3 +208,3 @@ ajv_1._ `${err}.schemaPath.indexOf(${it.errSchemaPath}) === 0`, ajv_1._ `/^\\/[^\\/]*$/.test(${err}.schemaPath.slice(${it.errSchemaPath.length}))`); | ||
function ifMatchesChildError(err, childErrs, thenBody) { | ||
gen.if(codegen_1.and(ajv_1._ `${err}.keyword !== ${keyword}`, ajv_1._ `!${err}.${used}`, ajv_1._ `${err}.dataPath.indexOf(${dataPath}) === 0`), () => { | ||
gen.if(codegen_1.and(ajv_1._ `${err}.keyword !== ${keyword}`, ajv_1._ `!${err}.${used}`, ajv_1._ `${err}.instancePath.indexOf(${instancePath}) === 0`), () => { | ||
const childRegex = gen.scopeValue("pattern", { | ||
@@ -214,3 +214,3 @@ ref: /^\/([^/]*)(?:\/|$)/, | ||
}); | ||
const matches = gen.const("emMatches", ajv_1._ `${childRegex}.exec(${err}.dataPath.slice(${dataPath}.length))`); | ||
const matches = gen.const("emMatches", ajv_1._ `${childRegex}.exec(${err}.instancePath.slice(${instancePath}.length))`); | ||
const child = gen.const("emChild", ajv_1._ `${matches} && ${matches}[1].replace(/~1/g, "/").replace(/~0/g, "~")`); | ||
@@ -221,3 +221,3 @@ gen.if(ajv_1._ `${child} !== undefined && ${child} in ${childErrs}`, () => thenBody(child)); | ||
function matchAnyError(err) { | ||
return codegen_1.and(ajv_1._ `${err}.keyword !== ${keyword}`, ajv_1._ `!${err}.${used}`, codegen_1.or(ajv_1._ `${err}.dataPath === ${dataPath}`, codegen_1.and(ajv_1._ `${err}.dataPath.indexOf(${dataPath}) === 0`, ajv_1._ `${err}.dataPath[${dataPath}.length] === "/"`)), ajv_1._ `${err}.schemaPath.indexOf(${it.errSchemaPath}) === 0`, ajv_1._ `${err}.schemaPath[${it.errSchemaPath}.length] === "/"`); | ||
return codegen_1.and(ajv_1._ `${err}.keyword !== ${keyword}`, ajv_1._ `!${err}.${used}`, codegen_1.or(ajv_1._ `${err}.instancePath === ${instancePath}`, codegen_1.and(ajv_1._ `${err}.instancePath.indexOf(${instancePath}) === 0`, ajv_1._ `${err}.instancePath[${instancePath}.length] === "/"`)), ajv_1._ `${err}.schemaPath.indexOf(${it.errSchemaPath}) === 0`, ajv_1._ `${err}.schemaPath[${it.errSchemaPath}.length] === "/"`); | ||
} | ||
@@ -237,3 +237,3 @@ function getTemplatesCode(keys, msgs) { | ||
return new code_1._Code(code_1.safeStringify(msg) | ||
.replace(INTERPOLATION_REPLACE, (_s, ptr) => `" + JSON.stringify(${context_1.getData(ptr, it)}) + "`) | ||
.replace(INTERPOLATION_REPLACE, (_s, ptr) => `" + JSON.stringify(${validate_1.getData(ptr, it)}) + "`) | ||
.replace(EMPTY_STR, "")); | ||
@@ -240,0 +240,0 @@ } |
{ | ||
"name": "ajv-errors", | ||
"version": "2.0.1", | ||
"description": "Custom error messages in JSON-Schema for Ajv validator", | ||
"version": "3.0.0", | ||
"description": "Custom error messages in JSON Schemas for Ajv validator", | ||
"main": "dist/index.js", | ||
@@ -39,3 +39,3 @@ "types": "dist/index.d.ts", | ||
"peerDependencies": { | ||
"ajv": "^7.0.0" | ||
"ajv": "^8.0.1" | ||
}, | ||
@@ -48,3 +48,3 @@ "devDependencies": { | ||
"@typescript-eslint/parser": "^4.7.0", | ||
"ajv": "^7.0.0", | ||
"ajv": "^8.0.1", | ||
"eslint": "^7.2.0", | ||
@@ -51,0 +51,0 @@ "eslint-config-prettier": "^7.0.0", |
@@ -12,3 +12,3 @@ # ajv-errors | ||
ajv-errors v2 supports [ajv v7](https://github.com/ajv-validator/ajv). | ||
ajv-errors v3 supports [ajv v8](https://github.com/ajv-validator/ajv). | ||
@@ -79,4 +79,4 @@ If you are using ajv v6, you should use [ajv-errors v1](https://github.com/ajv-validator/ajv-errors/tree/v1) | ||
errors: [ | ||
{keyword: "additionalProperties", dataPath: "" /* , ... */}, | ||
{keyword: "type", dataPath: ".foo" /* , ... */}, | ||
{keyword: "additionalProperties", instancePath: "" /* , ... */}, | ||
{keyword: "type", instancePath: ".foo" /* , ... */}, | ||
], | ||
@@ -119,3 +119,3 @@ }, | ||
keyword: type, | ||
dataPath: "/foo", | ||
instancePath: "/foo", | ||
// ... | ||
@@ -193,3 +193,3 @@ message: "should be integer", | ||
message: "data.foo should be integer >= 2", | ||
dataPath: "/foo", | ||
instancePath: "/foo", | ||
// ... | ||
@@ -203,3 +203,3 @@ params: { | ||
message: "data.bar should be string with length >= 2", | ||
dataPath: "/bar", | ||
instancePath: "/bar", | ||
// ... | ||
@@ -252,3 +252,3 @@ params: { | ||
message: 'data should be an object with properties "foo" and "bar" only', | ||
dataPath: "", | ||
instancePath: "", | ||
// ... | ||
@@ -326,3 +326,3 @@ params: { | ||
- _keepErrors_: keep original errors. Default is to remove matched errors (they will still be available in `params.errors` property of generated error). If an error was matched and included in the error generated by `errorMessage` keyword it will have property `emUsed: true`. | ||
- _singleError_: create one error for all keywords used in `errorMessage` keyword (error messages defined for properties and items are not merged because they have different dataPaths). Multiple error messages are concatenated. Option values: | ||
- _singleError_: create one error for all keywords used in `errorMessage` keyword (error messages defined for properties and items are not merged because they have different instancePaths). Multiple error messages are concatenated. Option values: | ||
- `false` (default): create multiple errors, one for each message | ||
@@ -329,0 +329,0 @@ - `true`: create single error, messages are concatenated using `"; "` |
@@ -5,3 +5,3 @@ import type {Plugin, CodeKeywordDefinition, KeywordCxt, ErrorObject, Code} from "ajv" | ||
import {safeStringify, _Code} from "ajv/dist/compile/codegen/code" | ||
import {getData} from "ajv/dist/compile/context" | ||
import {getData} from "ajv/dist/compile/validate" | ||
import {reportError} from "ajv/dist/compile/errors" | ||
@@ -55,3 +55,3 @@ import N from "ajv/dist/compile/names" | ||
const sch: ErrorMessageSchema | string = schema | ||
const dataPath = strConcat(N.dataPath, it.errorPath) | ||
const instancePath = strConcat(N.instancePath, it.errorPath) | ||
gen.if(_`${N.errors} > 0`, () => { | ||
@@ -233,4 +233,4 @@ if (typeof sch == "object") { | ||
gen.assign( | ||
_`${N.vErrors}[${N.errors}-1].dataPath`, | ||
_`${dataPath} + "/" + ${key}.replace(/~/g, "~0").replace(/\\//g, "~1")` | ||
_`${N.vErrors}[${N.errors}-1].instancePath`, | ||
_`${instancePath} + "/" + ${key}.replace(/~/g, "~0").replace(/\\//g, "~1")` | ||
) | ||
@@ -278,3 +278,3 @@ }) | ||
_`!${err}.${used}`, | ||
_`${err}.dataPath === ${dataPath}`, | ||
_`${err}.instancePath === ${instancePath}`, | ||
_`${err}.keyword in ${kwdErrs}`, | ||
@@ -296,3 +296,3 @@ // TODO match the end of the string? | ||
_`!${err}.${used}`, | ||
_`${err}.dataPath.indexOf(${dataPath}) === 0` | ||
_`${err}.instancePath.indexOf(${instancePath}) === 0` | ||
), | ||
@@ -306,3 +306,3 @@ () => { | ||
"emMatches", | ||
_`${childRegex}.exec(${err}.dataPath.slice(${dataPath}.length))` | ||
_`${childRegex}.exec(${err}.instancePath.slice(${instancePath}.length))` | ||
) | ||
@@ -323,6 +323,6 @@ const child = gen.const( | ||
or( | ||
_`${err}.dataPath === ${dataPath}`, | ||
_`${err}.instancePath === ${instancePath}`, | ||
and( | ||
_`${err}.dataPath.indexOf(${dataPath}) === 0`, | ||
_`${err}.dataPath[${dataPath}.length] === "/"` | ||
_`${err}.instancePath.indexOf(${instancePath}) === 0`, | ||
_`${err}.instancePath[${instancePath}.length] === "/"` | ||
) | ||
@@ -329,0 +329,0 @@ ), |
Sorry, the diff of this file is not supported yet
53233