Skip to content

Commit 8fae4e2

Browse files
committed
feat: ajv@8
BREAKING CHANGE: some error message wordings changed, updated to newest json schema draft
1 parent f46ff3a commit 8fae4e2

4 files changed

Lines changed: 2771 additions & 4232 deletions

File tree

__tests__/index.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ test('should return validation error on an invalid object', async () => {
113113
expect(JSON.parse(JSON.stringify(error))).toEqual({
114114
errors: [{
115115
field: '/extraneous',
116-
message: 'should NOT have additional properties',
116+
message: 'must NOT have additional properties',
117117
name: 'HttpStatusError',
118118
status: 400,
119119
statusCode: 400,
120120
status_code: 400,
121121
}],
122-
message: 'custom validation failed: data should NOT have additional properties',
122+
message: 'custom validation failed: data must NOT have additional properties',
123123
name: 'HttpStatusError',
124124
status: 417,
125125
statusCode: 417,

package.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,30 @@
2828
},
2929
"homepage": "https://github.com/microfleet/validation#readme",
3030
"devDependencies": {
31-
"@makeomatic/deploy": "^10.3.3",
32-
"@types/callsite": "^1.0.30",
33-
"@types/common-errors": "^1.0.1",
34-
"@types/debug": "^4.1.5",
35-
"@types/glob": "^7.1.3",
36-
"@types/jest": "^26.0.19",
37-
"@types/node": "^14.14.16",
38-
"@typescript-eslint/eslint-plugin": "^4.11.1",
39-
"@typescript-eslint/parser": "^4.11.1",
40-
"codecov": "^3.8.1",
41-
"eslint": "^7.16.0",
42-
"eslint-config-makeomatic": "^5.0.3",
43-
"jest": "^26.6.3",
44-
"ts-jest": "^26.4.4",
45-
"typescript": "^4.1.3"
31+
"@makeomatic/deploy": "^10.4.0",
32+
"@types/callsite": "^1.0.31",
33+
"@types/common-errors": "^1.0.2",
34+
"@types/debug": "^4.1.7",
35+
"@types/glob": "^7.1.4",
36+
"@types/jest": "^27.0.0",
37+
"@types/node": "^16.4.13",
38+
"@typescript-eslint/eslint-plugin": "^4.29.1",
39+
"@typescript-eslint/parser": "^4.29.1",
40+
"codecov": "^3.8.3",
41+
"eslint": "^7.32.0",
42+
"eslint-config-makeomatic": "^5.0.4",
43+
"jest": "^27.0.6",
44+
"ts-jest": "^27.0.4",
45+
"typescript": "^4.3.5"
4646
},
4747
"dependencies": {
48-
"ajv": "^7.0.2",
49-
"ajv-formats": "^1.5.1",
50-
"ajv-keywords": "^4.0.0",
48+
"ajv": "^8.6.2",
49+
"ajv-formats": "^2.1.0",
50+
"ajv-keywords": "^5.0.0",
5151
"callsite": "^1.0.0",
5252
"common-errors": "^1.2.0",
53-
"debug": "^4.3.1",
54-
"glob": "^7.1.6"
53+
"debug": "^4.3.2",
54+
"glob": "^7.1.7"
5555
},
5656
"peerDependencies": {
5757
"common-errors": "~1.x.x"

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import glob = require('glob')
1111
import path = require('path')
1212
import { HttpStatusError } from './HttpStatusError'
1313

14-
const debug = _debug('ms-validation')
14+
const debug = _debug('@microfleet/validation')
1515

1616
export type globFilter = (filename: string) => boolean;
1717
export type ValidationError = InvalidOperationError | NotFoundError | HttpStatusError
@@ -25,7 +25,7 @@ export type ValidationResponse<T> =
2525
*/
2626
const json: globFilter = (filename: string) => path.extname(filename) === '.json'
2727
const slashes = new RegExp(path.sep, 'g')
28-
const safeValidate = (validate: ValidateFunction, doc: unknown): boolean | Error => {
28+
const safeValidate = <T>(validate: ValidateFunction<T>, doc: unknown): boolean | Error => {
2929
try {
3030
validate(doc)
3131
} catch (e) {
@@ -306,7 +306,7 @@ export class Validator {
306306
* @param data
307307
*/
308308
private $validate<T extends unknown = unknown>(schema: string, data: unknown): ValidationResponse<T> {
309-
const validate = this.$ajv.getSchema(schema)
309+
const validate = this.$ajv.getSchema<T>(schema)
310310

311311
if (!validate) {
312312
return { error: new NotFoundError(`validator "${schema}" not found`), doc: data }
@@ -329,9 +329,9 @@ export class Validator {
329329
let field
330330
if (err.keyword !== 'additionalProperties') {
331331
onlyAdditionalProperties = false
332-
field = err.dataPath
332+
field = err.instancePath
333333
} else {
334-
field = `${err.dataPath}/${(err.params ).additionalProperty}`
334+
field = `${err.instancePath}/${err.params.additionalProperty}`
335335
}
336336

337337
error.addError(new HttpStatusError(400, err.message, field))

0 commit comments

Comments
 (0)