Skip to content

Commit 0415601

Browse files
committed
upgrade ajv 8.0.0, dataPath -> instancePath
1 parent 623c07b commit 0415601

File tree

6 files changed

+43
-43
lines changed

6 files changed

+43
-43
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ Processed errors:
7575
// ...
7676
params: {
7777
errors: [
78-
{keyword: "additionalProperties", dataPath: "" /* , ... */},
79-
{keyword: "type", dataPath: ".foo" /* , ... */},
78+
{keyword: "additionalProperties", instancePath: "" /* , ... */},
79+
{keyword: "type", instancePath: ".foo" /* , ... */},
8080
],
8181
},
8282
},
@@ -114,7 +114,7 @@ Processed errors:
114114
{
115115
// original error
116116
keyword: type,
117-
dataPath: "/foo",
117+
instancePath: "/foo",
118118
// ...
119119
message: "should be integer",
120120
},
@@ -187,7 +187,7 @@ Processed errors:
187187
{
188188
keyword: "errorMessage",
189189
message: "data.foo should be integer >= 2",
190-
dataPath: "/foo",
190+
instancePath: "/foo",
191191
// ...
192192
params: {
193193
errors: [{keyword: "minimum" /* , ... */}],
@@ -196,7 +196,7 @@ Processed errors:
196196
{
197197
keyword: "errorMessage",
198198
message: "data.bar should be string with length >= 2",
199-
dataPath: "/bar",
199+
instancePath: "/bar",
200200
// ...
201201
params: {
202202
errors: [{keyword: "minLength" /* , ... */}],
@@ -244,7 +244,7 @@ Processed errors:
244244
{
245245
keyword: "errorMessage",
246246
message: 'data should be an object with properties "foo" and "bar" only',
247-
dataPath: "",
247+
instancePath: "",
248248
// ...
249249
params: {
250250
errors: [{keyword: "required" /* , ... */}, {keyword: "required" /* , ... */}],
@@ -317,7 +317,7 @@ Defaults:
317317
```
318318

319319
- _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`.
320-
- _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:
320+
- _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:
321321
- `false` (default): create multiple errors, one for each message
322322
- `true`: create single error, messages are concatenated using `"; "`
323323
- non-empty string: this string is used as a separator to concatenate messages

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
},
3737
"homepage": "https://github.com/epoberezkin/ajv-errors#readme",
3838
"peerDependencies": {
39-
"ajv": "^7.0.0"
39+
"ajv": "^8.0.1"
4040
},
4141
"devDependencies": {
4242
"@ajv-validator/config": "^0.3.0",
4343
"@types/jest": "^26.0.15",
4444
"@types/node": "^14.14.7",
4545
"@typescript-eslint/eslint-plugin": "^4.7.0",
4646
"@typescript-eslint/parser": "^4.7.0",
47-
"ajv": "^7.0.0",
47+
"ajv": "^8.0.1",
4848
"eslint": "^7.2.0",
4949
"eslint-config-prettier": "^7.0.0",
5050
"husky": "^5.1.3",

spec/object.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("errorMessage value is an object", () => {
4949
// errorMessage error
5050
assert.strictEqual(err.keyword, "errorMessage")
5151
assert.strictEqual(err.message, schema.errorMessage[err.params.errors[0].keyword])
52-
assert.strictEqual(err.dataPath, "")
52+
assert.strictEqual(err.instancePath, "")
5353
assert.strictEqual(err.schemaPath, "#/errorMessage")
5454
const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
5555
assert.deepStrictEqual(
@@ -136,7 +136,7 @@ describe("errorMessage value is an object", () => {
136136
.replace("${0#}", '"foo"')
137137
.replace("${0}", JSON.stringify(data.foo))
138138
assert.strictEqual(err.message, expectedMessage)
139-
assert.strictEqual(err.dataPath, "/foo")
139+
assert.strictEqual(err.instancePath, "/foo")
140140
assert.strictEqual(err.schemaPath, "#/properties/foo/errorMessage")
141141
const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
142142
assert.deepStrictEqual(
@@ -286,7 +286,7 @@ describe("errorMessage value is an object", () => {
286286
.replace("${/foo}", JSON.stringify(data.foo))
287287
.replace("${/bar}", JSON.stringify(data.bar))
288288
assert.strictEqual(err.message, expectedMessage)
289-
assert.strictEqual(err.dataPath, "")
289+
assert.strictEqual(err.instancePath, "")
290290
assert.strictEqual(err.schemaPath, "#/errorMessage")
291291
const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
292292
assert.deepStrictEqual(
@@ -575,10 +575,10 @@ describe("errorMessage value is an object", () => {
575575
// errorMessage error
576576
assert.strictEqual(err.keyword, "errorMessage")
577577
const child = Array.isArray(data) ? "items" : "properties"
578-
let expectedMessage = schema.errorMessage[child][err.dataPath.slice(1)]
578+
let expectedMessage = schema.errorMessage[child][err.instancePath.slice(1)]
579579
if (interpolate) expectedMessage = interpolate(expectedMessage, data)
580580
assert.strictEqual(err.message, expectedMessage)
581-
assert((Array.isArray(data) ? /^\/(0|1)$/ : /^\/(foo|bar)$/).test(err.dataPath))
581+
assert((Array.isArray(data) ? /^\/(0|1)$/ : /^\/(foo|bar)$/).test(err.instancePath))
582582
assert.strictEqual(err.schemaPath, "#/errorMessage")
583583
const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
584584
assert.deepStrictEqual(
@@ -629,7 +629,7 @@ describe("errorMessage value is an object", () => {
629629
// errorMessage error
630630
assert.strictEqual(err.keyword, "errorMessage")
631631
assert.strictEqual(err.schemaPath, "#/errorMessage")
632-
const expectedMessage = err.dataPath
632+
const expectedMessage = err.instancePath
633633
? schema.errorMessage.properties.foo
634634
: schema.errorMessage[expectedErr[0] === "required" ? "required" : "_"]
635635
assert.strictEqual(err.message, expectedMessage)

spec/options.spec.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ describe("options", () => {
4040
assertErrors(validate, [
4141
{
4242
keyword: "required",
43-
dataPath: "",
43+
instancePath: "",
4444
},
4545
{
4646
keyword: "type",
47-
dataPath: "/foo",
47+
instancePath: "/foo",
4848
emUsed: true,
4949
},
5050
{
5151
keyword: "errorMessage",
5252
message: "should be an object with an integer property baz",
53-
dataPath: "/foo",
53+
instancePath: "/foo",
5454
errors: ["type"],
5555
},
5656
])
@@ -78,17 +78,17 @@ describe("options", () => {
7878
assertErrors(validate, [
7979
{
8080
keyword: "maximum",
81-
dataPath: "",
81+
instancePath: "",
8282
emUsed: true,
8383
},
8484
{
8585
keyword: "multipleOf",
86-
dataPath: "",
86+
instancePath: "",
8787
},
8888
{
8989
keyword: "errorMessage",
9090
message: "should be <= 10",
91-
dataPath: "",
91+
instancePath: "",
9292
errors: ["maximum"],
9393
},
9494
])
@@ -116,24 +116,24 @@ describe("options", () => {
116116
assertErrors(validate, [
117117
{
118118
keyword: "required",
119-
dataPath: "",
119+
instancePath: "",
120120
emUsed: true,
121121
},
122122
{
123123
keyword: "required",
124-
dataPath: "",
124+
instancePath: "",
125125
emUsed: true,
126126
},
127127
{
128128
keyword: "errorMessage",
129129
message: "should have property foo",
130-
dataPath: "",
130+
instancePath: "",
131131
errors: ["required"],
132132
},
133133
{
134134
keyword: "errorMessage",
135135
message: "should have property bar",
136-
dataPath: "",
136+
instancePath: "",
137137
errors: ["required"],
138138
},
139139
])
@@ -162,17 +162,17 @@ describe("options", () => {
162162
assertErrors(validate, [
163163
{
164164
keyword: "type",
165-
dataPath: "/foo",
165+
instancePath: "/foo",
166166
emUsed: true,
167167
},
168168
{
169169
keyword: "type",
170-
dataPath: "/bar",
170+
instancePath: "/bar",
171171
},
172172
{
173173
keyword: "errorMessage",
174174
message: "foo should be a number",
175-
dataPath: "/foo",
175+
instancePath: "/foo",
176176
errors: ["type"],
177177
},
178178
])
@@ -222,7 +222,7 @@ describe("options", () => {
222222
{
223223
keyword: "errorMessage",
224224
message: expectedMessage,
225-
dataPath: "",
225+
instancePath: "",
226226
errors: expectedKeywords,
227227
},
228228
])
@@ -239,7 +239,7 @@ describe("options", () => {
239239
expectedErrors.forEach((expectedErr, i) => {
240240
const err = errors[i] as ErrorObject & {emUsed: boolean}
241241
assert.strictEqual(err.keyword, expectedErr.keyword)
242-
assert.strictEqual(err.dataPath, expectedErr.dataPath)
242+
assert.strictEqual(err.instancePath, expectedErr.instancePath)
243243
assert.strictEqual(err.emUsed, expectedErr.emUsed)
244244
if (expectedErr.keyword === "errorMessage") {
245245
assert.strictEqual(err.params.errors.length, expectedErr.errors?.length)

spec/string.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function _ajv(verbose?: boolean): Ajv {
77
return ajvErrors(new Ajv({allErrors: true, verbose, code: {source: true}}))
88
}
99

10-
describe("errorMessage value is a string", () => {
10+
describe.only("errorMessage value is a string", () => {
1111
it("should replace all errors with custom error message", () => {
1212
const ajvs = [_ajv(), _ajv(true), new AjvPack(_ajv()), new AjvPack(_ajv(true))]
1313

@@ -37,7 +37,7 @@ describe("errorMessage value is a string", () => {
3737
const err = validate.errors[0]
3838
assert.strictEqual(err.keyword, "errorMessage")
3939
assert.strictEqual(err.message, schema.errorMessage)
40-
assert.strictEqual(err.dataPath, "")
40+
assert.strictEqual(err.instancePath, "")
4141
assert.strictEqual(err.schemaPath, "#/errorMessage")
4242
const replacedKeywords = err.params.errors.map((e: ErrorObject) => {
4343
return e.keyword
@@ -90,7 +90,7 @@ describe("errorMessage value is a string", () => {
9090
.replace("${/foo}", JSON.stringify(data.foo))
9191
.replace("${/bar}", JSON.stringify(data.bar))
9292
assert.strictEqual(err.message, expectedMessage)
93-
assert.strictEqual(err.dataPath, "")
93+
assert.strictEqual(err.instancePath, "")
9494
assert.strictEqual(err.schemaPath, "#/errorMessage")
9595
const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
9696
assert.deepStrictEqual(replacedKeywords.sort(), expectedReplacedKeywords.sort())

src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {Plugin, CodeKeywordDefinition, KeywordCxt, ErrorObject, Code} from
22
import Ajv, {_, str, stringify, Name} from "ajv"
33
import {and, or, not, strConcat} from "ajv/dist/compile/codegen"
44
import {safeStringify, _Code} from "ajv/dist/compile/codegen/code"
5-
import {getData} from "ajv/dist/compile/context"
5+
import {getData} from "ajv/dist/compile/validate"
66
import {reportError} from "ajv/dist/compile/errors"
77
import N from "ajv/dist/compile/names"
88

@@ -51,7 +51,7 @@ function errorMessage(options: ErrorMessageOptions): CodeKeywordDefinition {
5151
const {gen, data, schema, schemaValue, it} = cxt
5252
if (it.createErrors === false) return
5353
const sch: ErrorMessageSchema | string = schema
54-
const dataPath = strConcat(N.dataPath, it.errorPath)
54+
const instancePath = strConcat(N.instancePath, it.errorPath)
5555
gen.if(_`${N.errors} > 0`, () => {
5656
if (typeof sch == "object") {
5757
const [kwdPropErrors, kwdErrors] = keywordErrorsConfig(sch)
@@ -228,8 +228,8 @@ function errorMessage(options: ErrorMessageOptions): CodeKeywordDefinition {
228228
params: _`{errors: ${childErrs}[${key}]}`,
229229
})
230230
gen.assign(
231-
_`${N.vErrors}[${N.errors}-1].dataPath`,
232-
_`${dataPath} + "/" + ${key}.replace(/~/g, "~0").replace(/\\//g, "~1")`
231+
_`${N.vErrors}[${N.errors}-1].instancePath`,
232+
_`${instancePath} + "/" + ${key}.replace(/~/g, "~0").replace(/\\//g, "~1")`
233233
)
234234
})
235235
)
@@ -272,7 +272,7 @@ function errorMessage(options: ErrorMessageOptions): CodeKeywordDefinition {
272272
return and(
273273
_`${err}.keyword !== ${keyword}`,
274274
_`!${err}.${used}`,
275-
_`${err}.dataPath === ${dataPath}`,
275+
_`${err}.instancePath === ${instancePath}`,
276276
_`${err}.keyword in ${kwdErrs}`,
277277
// TODO match the end of the string?
278278
_`${err}.schemaPath.indexOf(${it.errSchemaPath}) === 0`,
@@ -289,7 +289,7 @@ function errorMessage(options: ErrorMessageOptions): CodeKeywordDefinition {
289289
and(
290290
_`${err}.keyword !== ${keyword}`,
291291
_`!${err}.${used}`,
292-
_`${err}.dataPath.indexOf(${dataPath}) === 0`
292+
_`${err}.instancePath.indexOf(${instancePath}) === 0`
293293
),
294294
() => {
295295
const childRegex = gen.scopeValue("pattern", {
@@ -298,7 +298,7 @@ function errorMessage(options: ErrorMessageOptions): CodeKeywordDefinition {
298298
})
299299
const matches = gen.const(
300300
"emMatches",
301-
_`${childRegex}.exec(${err}.dataPath.slice(${dataPath}.length))`
301+
_`${childRegex}.exec(${err}.instancePath.slice(${instancePath}.length))`
302302
)
303303
const child = gen.const(
304304
"emChild",
@@ -314,10 +314,10 @@ function errorMessage(options: ErrorMessageOptions): CodeKeywordDefinition {
314314
_`${err}.keyword !== ${keyword}`,
315315
_`!${err}.${used}`,
316316
or(
317-
_`${err}.dataPath === ${dataPath}`,
317+
_`${err}.instancePath === ${instancePath}`,
318318
and(
319-
_`${err}.dataPath.indexOf(${dataPath}) === 0`,
320-
_`${err}.dataPath[${dataPath}.length] === "/"`
319+
_`${err}.instancePath.indexOf(${instancePath}) === 0`,
320+
_`${err}.instancePath[${instancePath}.length] === "/"`
321321
)
322322
),
323323
_`${err}.schemaPath.indexOf(${it.errSchemaPath}) === 0`,

0 commit comments

Comments
 (0)