Skip to content

Commit 50f4129

Browse files
authored
Merge branch 'master' into patch-1
2 parents b41ea0b + 25f2783 commit 50f4129

File tree

8 files changed

+113
-54
lines changed

8 files changed

+113
-54
lines changed

.github/workflows/build.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: ["*"]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [10.x, 12.x, 14.x]
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Use Node.js ${{ matrix.node-version }}
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
- run: npm install
24+
- run: npm test
25+
- name: Coveralls
26+
uses: coverallsapp/github-action@master
27+
with:
28+
github-token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/publish.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: publish
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
publish-npm:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@v1
13+
with:
14+
node-version: 14
15+
registry-url: https://registry.npmjs.org/
16+
- run: npm install
17+
- run: npm test
18+
- name: Publish beta version to npm
19+
if: "github.event.release.prerelease"
20+
run: npm publish --tag beta
21+
env:
22+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
23+
- name: Publish to npm
24+
if: "!github.event.release.prerelease"
25+
run: npm publish
26+
env:
27+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.travis.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
Custom error messages in JSON-Schema for Ajv validator
44

5-
[![Build Status](https://travis-ci.org/ajv-validator/ajv-errors.svg?branch=master)](https://travis-ci.org/ajv-validator/ajv-errors)
5+
[![build](https://github.com/ajv-validator/ajv-errors/workflows/build/badge.svg)](https://github.com/ajv-validator/ajv-errors/actions?query=workflow%3Abuild)
66
[![npm](https://img.shields.io/npm/v/ajv-errors.svg)](https://www.npmjs.com/package/ajv-errors)
7-
[![npm (beta)](https://img.shields.io/npm/v/ajv-errors/beta)](https://www.npmjs.com/package/ajv-errors/v/2.0.0-beta.0)
8-
[![Coverage Status](https://coveralls.io/repos/github/ajv-validator/ajv-errors/badge.svg?branch=master)](https://coveralls.io/github/ajv-validator/ajv-errors?branch=master)
9-
[![Gitter](https://img.shields.io/gitter/room/ajv-validator/ajv.svg)](https://gitter.im/ajv-validator/ajv)
7+
[![coverage](https://coveralls.io/repos/github/ajv-validator/ajv-errors/badge.svg?branch=master)](https://coveralls.io/github/ajv-validator/ajv-errors?branch=master)
8+
[![gitter](https://img.shields.io/gitter/room/ajv-validator/ajv.svg)](https://gitter.im/ajv-validator/ajv)
109

1110
**Please note**
1211

13-
ajv-errors v2-beta supports [ajv v7-beta](https://github.com/ajv-validator/ajv).
12+
ajv-errors v2 supports [ajv v7](https://github.com/ajv-validator/ajv).
1413

1514
If you are using ajv v6, you should use [ajv-errors v1](https://github.com/ajv-validator/ajv-errors/tree/v1)
1615

@@ -29,14 +28,6 @@ If you are using ajv v6, you should use [ajv-errors v1](https://github.com/ajv-v
2928

3029
## Install
3130

32-
v2-beta:
33-
34-
```
35-
npm install ajv-errors@beta
36-
```
37-
38-
v1:
39-
4031
```
4132
npm install ajv-errors
4233
```
@@ -294,12 +285,11 @@ const schema = {
294285
}
295286
```
296287

297-
#### Referring to Field Names
298-
299-
Field names can be referred to by using the relative `0#` JSON-pointer.
288+
#### Using property names in error messages
300289

301-
Example which prints additional properties that cause validation error:
290+
Property names can be used in error messages with the relative JSON-pointer (e.g. `0#`).
302291

292+
Example:
303293
```javascript
304294
const schema = {
305295
type: "object",
@@ -315,8 +305,6 @@ const schema = {
315305
}
316306
```
317307

318-
319-
320308
## Options
321309

322310
Defaults:

package.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ajv-errors",
3-
"version": "2.0.0-beta.0",
3+
"version": "2.0.0",
44
"description": "Custom error messages in JSON-Schema for Ajv validator",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -11,11 +11,11 @@
1111
"scripts": {
1212
"build": "rm -rf dist && tsc",
1313
"eslint": "eslint \"src/**/*.*s\" \"spec/**/*.*s\"",
14-
"prettier:write": "prettier --write \"./**/*.{md,json,ts,js}\"",
15-
"prettier:check": "prettier --list-different \"./**/*.{md,json,ts,js}\"",
14+
"prettier:write": "prettier --write \"./**/*.{json,ts,js}\"",
15+
"prettier:check": "prettier --list-different \"./**/*.{json,ts,js}\"",
1616
"test-spec": "jest \"spec/*.ts\"",
1717
"test-cov": "jest \"spec/*.ts\" --coverage",
18-
"test": "npm run eslint && npm run build && npm run test-cov",
18+
"test": "npm run prettier:check && npm run eslint && npm run build && npm run test-cov",
1919
"prepublish": "npm run build"
2020
},
2121
"repository": {
@@ -36,21 +36,20 @@
3636
},
3737
"homepage": "https://github.com/epoberezkin/ajv-errors#readme",
3838
"peerDependencies": {
39-
"ajv": "^7.0.0-beta.7"
39+
"ajv": "^7.0.0"
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-beta.7",
47+
"ajv": "^7.0.0",
4848
"eslint": "^7.2.0",
49-
"eslint-config-prettier": "^6.15.0",
50-
"husky": "^4.3.0",
49+
"eslint-config-prettier": "^7.0.0",
50+
"husky": "^5.1.3",
5151
"jest": "^26.6.3",
5252
"lint-staged": "^10.5.1",
53-
"pre-commit": "^1.2.2",
5453
"prettier": "^2.1.2",
5554
"ts-jest": "^26.4.4",
5655
"typescript": "^4.0.5"
@@ -62,6 +61,6 @@
6261
}
6362
},
6463
"lint-staged": {
65-
"*.{md,json,yaml,js,ts}": "prettier --write"
64+
"*.{json,yaml,js,ts}": "prettier --write"
6665
}
6766
}

spec/object.spec.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import ajvErrors from ".."
22
import Ajv, {SchemaObject, ErrorObject, ValidateFunction} from "ajv"
3+
import AjvPack from "ajv/dist/standalone/instance"
34
import assert = require("assert")
45

6+
function _ajv(verbose?: boolean): Ajv {
7+
return ajvErrors(new Ajv({allErrors: true, verbose, code: {source: true}}))
8+
}
9+
510
describe("errorMessage value is an object", () => {
6-
let ajvs: Ajv[]
11+
let ajvs: (Ajv | AjvPack)[]
712

813
beforeEach(() => {
9-
ajvs = [
10-
ajvErrors(new Ajv({allErrors: true})),
11-
ajvErrors(new Ajv({allErrors: true, verbose: true})),
12-
]
14+
ajvs = [_ajv(), _ajv(true), new AjvPack(_ajv()), new AjvPack(_ajv(true))]
1315
})
1416

1517
describe("keywords", () => {
@@ -50,7 +52,10 @@ describe("errorMessage value is an object", () => {
5052
assert.strictEqual(err.dataPath, "")
5153
assert.strictEqual(err.schemaPath, "#/errorMessage")
5254
const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
53-
assert.deepStrictEqual(replacedKeywords.sort(), expectedErr.sort())
55+
assert.deepStrictEqual(
56+
Array.from(replacedKeywords.sort()),
57+
Array.from(expectedErr.sort())
58+
)
5459
} else {
5560
// original error
5661
assert.strictEqual(err.keyword, expectedErr)
@@ -134,7 +139,10 @@ describe("errorMessage value is an object", () => {
134139
assert.strictEqual(err.dataPath, "/foo")
135140
assert.strictEqual(err.schemaPath, "#/properties/foo/errorMessage")
136141
const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
137-
assert.deepStrictEqual(replacedKeywords.sort(), expectedErr.sort())
142+
assert.deepStrictEqual(
143+
Array.from(replacedKeywords.sort()),
144+
Array.from(expectedErr.sort())
145+
)
138146
} else {
139147
// original error
140148
assert.strictEqual(err.keyword, expectedErr)
@@ -281,7 +289,10 @@ describe("errorMessage value is an object", () => {
281289
assert.strictEqual(err.dataPath, "")
282290
assert.strictEqual(err.schemaPath, "#/errorMessage")
283291
const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
284-
assert.deepStrictEqual(replacedKeywords, Object.keys(expectedErr))
292+
assert.deepStrictEqual(
293+
Array.from(replacedKeywords),
294+
Array.from(Object.keys(expectedErr))
295+
)
285296
} else {
286297
// original error
287298
assert.strictEqual(err.keyword, expectedErr)
@@ -570,7 +581,10 @@ describe("errorMessage value is an object", () => {
570581
assert((Array.isArray(data) ? /^\/(0|1)$/ : /^\/(foo|bar)$/).test(err.dataPath))
571582
assert.strictEqual(err.schemaPath, "#/errorMessage")
572583
const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
573-
assert.deepStrictEqual(replacedKeywords.sort(), expectedErr.sort())
584+
assert.deepStrictEqual(
585+
Array.from(replacedKeywords.sort()),
586+
Array.from(expectedErr.sort())
587+
)
574588
} else {
575589
// original error
576590
assert.strictEqual(err.keyword, expectedErr)
@@ -620,7 +634,10 @@ describe("errorMessage value is an object", () => {
620634
: schema.errorMessage[expectedErr[0] === "required" ? "required" : "_"]
621635
assert.strictEqual(err.message, expectedMessage)
622636
const replacedKeywords = err.params.errors.map((e: ErrorObject) => e.keyword)
623-
assert.deepStrictEqual(replacedKeywords.sort(), expectedErr.sort())
637+
assert.deepStrictEqual(
638+
Array.from(replacedKeywords.sort()),
639+
Array.from(expectedErr.sort())
640+
)
624641
} else {
625642
// original error
626643
assert.strictEqual(err.keyword, expectedErr)

spec/string.spec.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import ajvErrors from ".."
22
import Ajv, {ErrorObject} from "ajv"
3+
import AjvPack from "ajv/dist/standalone/instance"
34
import assert = require("assert")
45

6+
function _ajv(verbose?: boolean): Ajv {
7+
return ajvErrors(new Ajv({allErrors: true, verbose, code: {source: true}}))
8+
}
9+
510
describe("errorMessage value is a string", () => {
611
it("should replace all errors with custom error message", () => {
7-
const ajvs = [
8-
ajvErrors(new Ajv({allErrors: true})),
9-
ajvErrors(new Ajv({allErrors: true, verbose: true})),
10-
]
12+
const ajvs = [_ajv(), _ajv(true), new AjvPack(_ajv()), new AjvPack(_ajv(true))]
1113

1214
const schema = {
1315
type: "object",
@@ -40,7 +42,10 @@ describe("errorMessage value is a string", () => {
4042
const replacedKeywords = err.params.errors.map((e: ErrorObject) => {
4143
return e.keyword
4244
})
43-
assert.deepStrictEqual(replacedKeywords.sort(), expectedReplacedKeywords.sort())
45+
assert.deepStrictEqual(
46+
Array.from(replacedKeywords.sort()),
47+
Array.from(expectedReplacedKeywords.sort())
48+
)
4449
}
4550
})
4651
})

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const used: Name = new Name("emUsed")
3030
const KEYWORD_PROPERTY_PARAMS = {
3131
required: "missingProperty",
3232
dependencies: "property",
33+
dependentRequired: "property",
3334
}
3435

3536
export interface ErrorMessageOptions {
@@ -291,7 +292,10 @@ function errorMessage(options: ErrorMessageOptions): CodeKeywordDefinition {
291292
_`${err}.dataPath.indexOf(${dataPath}) === 0`
292293
),
293294
() => {
294-
const childRegex = gen.scopeValue("pattern", {ref: /^\/([^/]*)(?:\/|$)/})
295+
const childRegex = gen.scopeValue("pattern", {
296+
ref: /^\/([^/]*)(?:\/|$)/,
297+
code: _`new RegExp("^\\\/([^/]*)(?:\\\/|$)")`,
298+
})
295299
const matches = gen.const(
296300
"emMatches",
297301
_`${childRegex}.exec(${err}.dataPath.slice(${dataPath}.length))`

0 commit comments

Comments
 (0)