Skip to content

Commit c2e78e5

Browse files
authored
feat: use draft-2019-09 (#137)
1 parent 8fae4e2 commit c2e78e5

9 files changed

Lines changed: 1493 additions & 1243 deletions

File tree

.husky/commit-msg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx --no-install commitlint -e $1

.husky/prepare-commit-msg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
case "$2,$3" in
5+
merge,)
6+
ex "+%s/Merge branch '\([^']\+\)'/chore(merge): \1/i" -scwq $1 ;;
7+
*) ;;
8+
esac

__tests__/fixtures/2019-09.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "array",
3+
"contains": {
4+
"type": "number"
5+
},
6+
"minContains": 2
7+
}

__tests__/index.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,12 @@ test('throws on invalid URL', () => {
170170
expect(() => validator.ifError<string>('http-url', 'https://'))
171171
.toThrow(HttpStatusError)
172172
})
173+
174+
test('should be able to use 2019-09 schema keywords', () => {
175+
validator = new Validation(CORRECT_PATH)
176+
177+
expect(() => validator.ifError<number[]>('2019-09', [1]))
178+
.toThrow(HttpStatusError)
179+
expect(validator.ifError<number[]>('2019-09', [1, 2]))
180+
.toStrictEqual([1, 2])
181+
})

package.json

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"compile": "rimraf lib; tsc -b ./tsconfig.build.json",
1515
"prepublishOnly": "yarn compile",
1616
"test": "yarn lint && jest --collectCoverage --runTestsByPath __tests__/**.spec.ts && codecov >/dev/null",
17-
"lint": "eslint --ext .ts -c .eslintrc.js src",
17+
"lint": "eslint --ext .ts -c .eslintrc.js src __tests__",
1818
"semantic-release": "semantic-release"
1919
},
2020
"repository": {
@@ -28,42 +28,36 @@
2828
},
2929
"homepage": "https://github.com/microfleet/validation#readme",
3030
"devDependencies": {
31-
"@makeomatic/deploy": "^10.4.0",
31+
"@makeomatic/deploy": "^11.0.2",
3232
"@types/callsite": "^1.0.31",
3333
"@types/common-errors": "^1.0.2",
3434
"@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",
35+
"@types/glob": "^7.2.0",
36+
"@types/jest": "^27.4.0",
37+
"@types/node": "^17.0.8",
38+
"@typescript-eslint/eslint-plugin": "^5.9.1",
39+
"@typescript-eslint/parser": "^5.9.1",
4040
"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"
41+
"eslint": "^8.6.0",
42+
"eslint-config-makeomatic": "^5.1.0",
43+
"jest": "^27.4.7",
44+
"ts-jest": "^27.1.2",
45+
"typescript": "^4.5.4"
4646
},
4747
"dependencies": {
48-
"ajv": "^8.6.2",
49-
"ajv-formats": "^2.1.0",
50-
"ajv-keywords": "^5.0.0",
48+
"ajv": "^8.8.2",
49+
"ajv-formats": "^2.1.1",
50+
"ajv-keywords": "^5.1.0",
5151
"callsite": "^1.0.0",
5252
"common-errors": "^1.2.0",
53-
"debug": "^4.3.2",
54-
"glob": "^7.1.7"
53+
"debug": "^4.3.3",
54+
"glob": "^7.2.0"
5555
},
5656
"peerDependencies": {
5757
"common-errors": "~1.x.x"
5858
},
5959
"files": [
6060
"lib/",
6161
"src/"
62-
],
63-
"husky": {
64-
"hooks": {
65-
"commit-msg": "commitlint -e $HUSKY_GIT_PARAMS",
66-
"prepare-commit-msg": "./node_modules/@makeomatic/deploy/git-hooks/prepare-commit-msg $HUSKY_GIT_PARAMS"
67-
}
68-
}
62+
]
6963
}

src/HttpStatusError.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { HttpStatusError as HttpCoreError } from 'common-errors'
22

33
export class HttpStatusError extends HttpCoreError {
4-
public errors?: Error[];
5-
public field?: string;
6-
public status?: number;
7-
public status_code?: number;
4+
public errors?: Error[]
5+
public field?: string
6+
public status?: number
7+
public status_code?: number
88

99
/**
1010
* @param statusCode

src/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { URL } from 'url'
2-
import Ajv, { ValidateFunction, Options } from 'ajv'
2+
import Ajv, { ValidateFunction, Options } from 'ajv/dist/2019'
33
import addKeywords from 'ajv-keywords'
44
import addFormats from 'ajv-formats'
5+
import draft7MetaSchema from "ajv/dist/refs/json-schema-draft-07.json"
56
import callsite = require('callsite')
67
import { InvalidOperationError, io, NotFoundError } from 'common-errors'
78
import _debug = require('debug')
89
import fs = require('fs')
910
import { promises as fsAsync } from 'fs'
1011
import glob = require('glob')
1112
import path = require('path')
13+
1214
import { HttpStatusError } from './HttpStatusError'
1315

1416
const debug = _debug('@microfleet/validation')
@@ -49,12 +51,12 @@ export class Validator {
4951
removeAdditional: false,
5052
useDefaults: true,
5153
verbose: true,
52-
};
54+
}
5355

54-
private readonly schemaDir: string | undefined;
55-
private readonly $ajv: Ajv;
56-
private readonly filterOpt: globFilter;
57-
private readonly schemaOptions: Options;
56+
private readonly schemaDir: string | undefined
57+
private readonly $ajv: Ajv
58+
private readonly filterOpt: globFilter
59+
private readonly schemaOptions: Options
5860

5961
/**
6062
* Initializes validator with schemas in the schemaDir with a given filter function
@@ -89,6 +91,7 @@ export class Validator {
8991
return false
9092
}
9193
})
94+
ajvInstance.addMetaSchema(draft7MetaSchema)
9295

9396
addKeywords(ajvInstance)
9497
addFormats(ajvInstance)
@@ -238,7 +241,7 @@ export class Validator {
238241
})
239242
})
240243
} catch (err) {
241-
const error = new io.IOError(`was unable to read ${dir}`, err)
244+
const error = new io.IOError(`was unable to read ${dir}`, err as Error)
242245
throw error
243246
}
244247
}
@@ -252,7 +255,7 @@ export class Validator {
252255

253256
return glob.sync('**', { cwd: dir })
254257
} catch (err) {
255-
const error = new io.IOError(`was unable to read ${dir}`, err)
258+
const error = new io.IOError(`was unable to read ${dir}`, err as Error)
256259
throw error
257260
}
258261
}
@@ -305,7 +308,7 @@ export class Validator {
305308
* @param schema - schema name
306309
* @param data
307310
*/
308-
private $validate<T extends unknown = unknown>(schema: string, data: unknown): ValidationResponse<T> {
311+
private $validate<T = unknown>(schema: string, data: unknown): ValidationResponse<T> {
309312
const validate = this.$ajv.getSchema<T>(schema)
310313

311314
if (!validate) {

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"noUnusedParameters": true,
1212
"noImplicitReturns": true,
1313
"allowSyntheticDefaultImports": true,
14-
"esModuleInterop": true
14+
"esModuleInterop": true,
15+
"resolveJsonModule": true
1516
},
1617
"exclude": [
1718
"**/node_modules/**",

0 commit comments

Comments
 (0)