Skip to content

Commit

Permalink
feat: ajv@8
Browse files Browse the repository at this point in the history
BREAKING CHANGE: some error message wordings changed, updated to newest json schema draft
  • Loading branch information
AVVS committed Aug 11, 2021
1 parent f46ff3a commit 8fae4e2
Show file tree
Hide file tree
Showing 4 changed files with 2,771 additions and 4,232 deletions.
4 changes: 2 additions & 2 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ test('should return validation error on an invalid object', async () => {
expect(JSON.parse(JSON.stringify(error))).toEqual({
errors: [{
field: '/extraneous',
message: 'should NOT have additional properties',
message: 'must NOT have additional properties',
name: 'HttpStatusError',
status: 400,
statusCode: 400,
status_code: 400,
}],
message: 'custom validation failed: data should NOT have additional properties',
message: 'custom validation failed: data must NOT have additional properties',
name: 'HttpStatusError',
status: 417,
statusCode: 417,
Expand Down
40 changes: 20 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@
},
"homepage": "https://github.com/microfleet/validation#readme",
"devDependencies": {
"@makeomatic/deploy": "^10.3.3",
"@types/callsite": "^1.0.30",
"@types/common-errors": "^1.0.1",
"@types/debug": "^4.1.5",
"@types/glob": "^7.1.3",
"@types/jest": "^26.0.19",
"@types/node": "^14.14.16",
"@typescript-eslint/eslint-plugin": "^4.11.1",
"@typescript-eslint/parser": "^4.11.1",
"codecov": "^3.8.1",
"eslint": "^7.16.0",
"eslint-config-makeomatic": "^5.0.3",
"jest": "^26.6.3",
"ts-jest": "^26.4.4",
"typescript": "^4.1.3"
"@makeomatic/deploy": "^10.4.0",
"@types/callsite": "^1.0.31",
"@types/common-errors": "^1.0.2",
"@types/debug": "^4.1.7",
"@types/glob": "^7.1.4",
"@types/jest": "^27.0.0",
"@types/node": "^16.4.13",
"@typescript-eslint/eslint-plugin": "^4.29.1",
"@typescript-eslint/parser": "^4.29.1",
"codecov": "^3.8.3",
"eslint": "^7.32.0",
"eslint-config-makeomatic": "^5.0.4",
"jest": "^27.0.6",
"ts-jest": "^27.0.4",
"typescript": "^4.3.5"
},
"dependencies": {
"ajv": "^7.0.2",
"ajv-formats": "^1.5.1",
"ajv-keywords": "^4.0.0",
"ajv": "^8.6.2",
"ajv-formats": "^2.1.0",
"ajv-keywords": "^5.0.0",
"callsite": "^1.0.0",
"common-errors": "^1.2.0",
"debug": "^4.3.1",
"glob": "^7.1.6"
"debug": "^4.3.2",
"glob": "^7.1.7"
},
"peerDependencies": {
"common-errors": "~1.x.x"
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import glob = require('glob')
import path = require('path')
import { HttpStatusError } from './HttpStatusError'

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

export type globFilter = (filename: string) => boolean;
export type ValidationError = InvalidOperationError | NotFoundError | HttpStatusError
Expand All @@ -25,7 +25,7 @@ export type ValidationResponse<T> =
*/
const json: globFilter = (filename: string) => path.extname(filename) === '.json'
const slashes = new RegExp(path.sep, 'g')
const safeValidate = (validate: ValidateFunction, doc: unknown): boolean | Error => {
const safeValidate = <T>(validate: ValidateFunction<T>, doc: unknown): boolean | Error => {
try {
validate(doc)
} catch (e) {
Expand Down Expand Up @@ -306,7 +306,7 @@ export class Validator {
* @param data
*/
private $validate<T extends unknown = unknown>(schema: string, data: unknown): ValidationResponse<T> {
const validate = this.$ajv.getSchema(schema)
const validate = this.$ajv.getSchema<T>(schema)

if (!validate) {
return { error: new NotFoundError(`validator "${schema}" not found`), doc: data }
Expand All @@ -329,9 +329,9 @@ export class Validator {
let field
if (err.keyword !== 'additionalProperties') {
onlyAdditionalProperties = false
field = err.dataPath
field = err.instancePath
} else {
field = `${err.dataPath}/${(err.params ).additionalProperty}`
field = `${err.instancePath}/${err.params.additionalProperty}`
}

error.addError(new HttpStatusError(400, err.message, field))
Expand Down
Loading

0 comments on commit 8fae4e2

Please sign in to comment.