@donmahallem/turbo-validate-request 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +12 -8
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +12 -8
- package/dist/esm/index.js.map +1 -1
- package/dist/types/convert-validation-error.d.ts +0 -3
- package/dist/types/convert-validation-error.d.ts.map +1 -1
- package/dist/types/convert-validation-error.spec.d.ts +0 -3
- package/dist/types/convert-validation-error.spec.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/validate-request.d.ts +5 -6
- package/dist/types/validate-request.d.ts.map +1 -1
- package/dist/types/validate-request.spec.d.ts +0 -3
- package/dist/types/validate-request.spec.d.ts.map +1 -1
- package/package.json +7 -5
- package/src/convert-validation-error.ts +16 -16
- package/src/index.ts +4 -5
- package/src/validate-request.ts +23 -21
package/dist/cjs/index.js
CHANGED
|
@@ -9,8 +9,9 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
9
9
|
|
|
10
10
|
const Ajv__default = /*#__PURE__*/_interopDefaultLegacy(Ajv);
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
*
|
|
12
|
+
/*
|
|
13
|
+
* Package @donmahallem/turbo-validate-request
|
|
14
|
+
* Source https://donmahallem.github.io/js-libs/
|
|
14
15
|
*/
|
|
15
16
|
const convertValidationError = (error) => {
|
|
16
17
|
const errorPath = error.instancePath === '' ? 'root' : error.instancePath;
|
|
@@ -26,14 +27,17 @@ const convertValidationError = (error) => {
|
|
|
26
27
|
}
|
|
27
28
|
};
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
*
|
|
30
|
+
/*
|
|
31
|
+
* Package @donmahallem/turbo-validate-request
|
|
32
|
+
* Source https://donmahallem.github.io/js-libs/
|
|
31
33
|
*/
|
|
32
34
|
/**
|
|
33
35
|
* Checks a request against the given schema
|
|
34
|
-
*
|
|
35
|
-
* @param
|
|
36
|
-
* @param
|
|
36
|
+
*
|
|
37
|
+
* @param {string} key property of the request to check the schema against
|
|
38
|
+
* @param {JSONSchemaType} schema the jsonschema
|
|
39
|
+
* @param {Ajv} ajvInstance optional ajv to be used
|
|
40
|
+
* @returns {RequestHandler} express middleware
|
|
37
41
|
*/
|
|
38
42
|
const validateRequest = (key, schema, ajvInstance = new Ajv__default["default"]()) => {
|
|
39
43
|
const validateFunction = ajvInstance.compile(schema);
|
|
@@ -48,6 +52,6 @@ const validateRequest = (key, schema, ajvInstance = new Ajv__default["default"](
|
|
|
48
52
|
};
|
|
49
53
|
|
|
50
54
|
exports.validateRequest = validateRequest;
|
|
51
|
-
// BUILD:
|
|
55
|
+
// BUILD: Fri Dec 10 2021 15:22:24 GMT+0100 (Central European Standard Time)
|
|
52
56
|
|
|
53
57
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/convert-validation-error.ts","../../src/validate-request.ts"],"sourcesContent":[null,null],"names":["RequestError","Ajv"],"mappings":";;;;;;;;;;;AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/convert-validation-error.ts","../../src/validate-request.ts"],"sourcesContent":[null,null],"names":["RequestError","Ajv"],"mappings":";;;;;;;;;;;AAAA;;;;AAQO,MAAM,sBAAsB,GAA0C,CAAC,KAAmB;IAC7F,MAAM,SAAS,GAAW,KAAK,CAAC,YAAY,KAAK,EAAE,GAAG,MAAM,GAAG,KAAK,CAAC,YAAY,CAAC;IAClF,QAAQ,KAAK,CAAC,OAAO;QACjB,KAAK,UAAU;YACX,OAAO,IAAIA,kBAAY,CAAC,oBAAoB,KAAK,CAAC,MAAM,CAAC,eAAe,QAAQ,SAAS,GAAG,EAAE,GAAG,CAAC,CAAC;QACvG,KAAK,SAAS;YACV,OAAO,IAAIA,kBAAY,CAAC,oCAAoC,SAAS,GAAG,EAAE,GAAG,CAAC,CAAC;QACnF,KAAK,MAAM;YACP,OAAO,IAAIA,kBAAY,CAAC,oBAAoB,SAAS,eAAe,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC;QAClG;YACI,OAAO,IAAIA,kBAAY,CAAC,YAAY,SAAS,GAAG,EAAE,GAAG,CAAC,CAAC;KAC9D;AACL,CAAC;;ACpBD;;;;AAWA;;;;;;;;MAQa,eAAe,GAAoB,CAC5C,GAAc,EACd,MAAyB,EACzB,cAAmB,IAAIC,uBAAG,EAAE;IAE5B,MAAM,gBAAgB,GAAqB,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACvE,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB;QACnD,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE;YAC7B,MAAM,MAAM,GAAmB,gBAAgB,CAAC,MAAwB,CAAC;YACzE,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACxC,OAAO;SACV;QACD,IAAI,EAAE,CAAC;KACV,CAAC;AACN;;;;;"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import Ajv from 'ajv';
|
|
2
2
|
import { RequestError } from '@donmahallem/turbo';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
*
|
|
4
|
+
/*
|
|
5
|
+
* Package @donmahallem/turbo-validate-request
|
|
6
|
+
* Source https://donmahallem.github.io/js-libs/
|
|
6
7
|
*/
|
|
7
8
|
const convertValidationError = (error) => {
|
|
8
9
|
const errorPath = error.instancePath === '' ? 'root' : error.instancePath;
|
|
@@ -18,14 +19,17 @@ const convertValidationError = (error) => {
|
|
|
18
19
|
}
|
|
19
20
|
};
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
*
|
|
22
|
+
/*
|
|
23
|
+
* Package @donmahallem/turbo-validate-request
|
|
24
|
+
* Source https://donmahallem.github.io/js-libs/
|
|
23
25
|
*/
|
|
24
26
|
/**
|
|
25
27
|
* Checks a request against the given schema
|
|
26
|
-
*
|
|
27
|
-
* @param
|
|
28
|
-
* @param
|
|
28
|
+
*
|
|
29
|
+
* @param {string} key property of the request to check the schema against
|
|
30
|
+
* @param {JSONSchemaType} schema the jsonschema
|
|
31
|
+
* @param {Ajv} ajvInstance optional ajv to be used
|
|
32
|
+
* @returns {RequestHandler} express middleware
|
|
29
33
|
*/
|
|
30
34
|
const validateRequest = (key, schema, ajvInstance = new Ajv()) => {
|
|
31
35
|
const validateFunction = ajvInstance.compile(schema);
|
|
@@ -40,6 +44,6 @@ const validateRequest = (key, schema, ajvInstance = new Ajv()) => {
|
|
|
40
44
|
};
|
|
41
45
|
|
|
42
46
|
export { validateRequest };
|
|
43
|
-
// BUILD:
|
|
47
|
+
// BUILD: Fri Dec 10 2021 15:22:24 GMT+0100 (Central European Standard Time)
|
|
44
48
|
|
|
45
49
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/convert-validation-error.ts","../../src/validate-request.ts"],"sourcesContent":[null,null],"names":[],"mappings":";;;AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/convert-validation-error.ts","../../src/validate-request.ts"],"sourcesContent":[null,null],"names":[],"mappings":";;;AAAA;;;;AAQO,MAAM,sBAAsB,GAA0C,CAAC,KAAmB;IAC7F,MAAM,SAAS,GAAW,KAAK,CAAC,YAAY,KAAK,EAAE,GAAG,MAAM,GAAG,KAAK,CAAC,YAAY,CAAC;IAClF,QAAQ,KAAK,CAAC,OAAO;QACjB,KAAK,UAAU;YACX,OAAO,IAAI,YAAY,CAAC,oBAAoB,KAAK,CAAC,MAAM,CAAC,eAAe,QAAQ,SAAS,GAAG,EAAE,GAAG,CAAC,CAAC;QACvG,KAAK,SAAS;YACV,OAAO,IAAI,YAAY,CAAC,oCAAoC,SAAS,GAAG,EAAE,GAAG,CAAC,CAAC;QACnF,KAAK,MAAM;YACP,OAAO,IAAI,YAAY,CAAC,oBAAoB,SAAS,eAAe,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC;QAClG;YACI,OAAO,IAAI,YAAY,CAAC,YAAY,SAAS,GAAG,EAAE,GAAG,CAAC,CAAC;KAC9D;AACL,CAAC;;ACpBD;;;;AAWA;;;;;;;;MAQa,eAAe,GAAoB,CAC5C,GAAc,EACd,MAAyB,EACzB,cAAmB,IAAI,GAAG,EAAE;IAE5B,MAAM,gBAAgB,GAAqB,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACvE,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB;QACnD,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE;YAC7B,MAAM,MAAM,GAAmB,gBAAgB,CAAC,MAAwB,CAAC;YACzE,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACxC,OAAO;SACV;QACD,IAAI,EAAE,CAAC;KACV,CAAC;AACN;;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert-validation-error.d.ts","sourceRoot":"","sources":["../../src/convert-validation-error.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"convert-validation-error.d.ts","sourceRoot":"","sources":["../../src/convert-validation-error.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,KAAK,CAAC;AAEnC,eAAO,MAAM,sBAAsB,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,YAY7D,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert-validation-error.spec.d.ts","sourceRoot":"","sources":["../../src/convert-validation-error.spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"convert-validation-error.spec.d.ts","sourceRoot":"","sources":["../../src/convert-validation-error.spec.ts"],"names":[],"mappings":"AAQA,OAAO,OAAO,CAAC"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
|
|
3
|
-
*/
|
|
4
1
|
import Ajv, { JSONSchemaType } from 'ajv';
|
|
5
2
|
import { RequestHandler } from 'express';
|
|
6
3
|
declare type CheckKeys = 'query' | 'params' | 'body';
|
|
7
4
|
declare type MethodSignature = <T = object>(key: CheckKeys, schema: JSONSchemaType<T>, ajvInstance?: Ajv) => RequestHandler;
|
|
8
5
|
/**
|
|
9
6
|
* Checks a request against the given schema
|
|
10
|
-
*
|
|
11
|
-
* @param
|
|
12
|
-
* @param
|
|
7
|
+
*
|
|
8
|
+
* @param {string} key property of the request to check the schema against
|
|
9
|
+
* @param {JSONSchemaType} schema the jsonschema
|
|
10
|
+
* @param {Ajv} ajvInstance optional ajv to be used
|
|
11
|
+
* @returns {RequestHandler} express middleware
|
|
13
12
|
*/
|
|
14
13
|
export declare const validateRequest: MethodSignature;
|
|
15
14
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate-request.d.ts","sourceRoot":"","sources":["../../src/validate-request.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validate-request.d.ts","sourceRoot":"","sources":["../../src/validate-request.ts"],"names":[],"mappings":"AAKA,OAAO,GAAG,EAAE,EAAgB,cAAc,EAAoB,MAAM,KAAK,CAAC;AAC1E,OAAO,EAAyB,cAAc,EAAY,MAAM,SAAS,CAAC;AAG1E,aAAK,SAAS,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,CAAC;AAC7C,aAAK,eAAe,GAAG,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,EAAE,GAAG,KAAK,cAAc,CAAC;AACpH;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,EAAE,eAc7B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate-request.spec.d.ts","sourceRoot":"","sources":["../../src/validate-request.spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validate-request.spec.d.ts","sourceRoot":"","sources":["../../src/validate-request.spec.ts"],"names":[],"mappings":"AASA,OAAO,OAAO,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@donmahallem/turbo-validate-request",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Turbo Helpers for express.js",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -16,8 +16,10 @@
|
|
|
16
16
|
"build:readme": "npx @appnest/readme generate --input ../package_readme_blueprint.md --config readme_config.json",
|
|
17
17
|
"test": "mocha --config ../../.mocharc.yml",
|
|
18
18
|
"test:coverage": "nyc --nycrc-path ../../.nycrc.json npm run test",
|
|
19
|
-
"lint": "
|
|
20
|
-
"
|
|
19
|
+
"lint": "eslint -c ./.eslintrc.js --ext .ts ./src",
|
|
20
|
+
"postlint": "prettier --config ./../../.prettierrc src/**/*.ts --check",
|
|
21
|
+
"lint:fix": "eslint -c ./.eslintrc.js --ext .ts ./src --fix",
|
|
22
|
+
"postlint:fix": "prettier --config ./../../.prettierrc src/**/*.ts --write",
|
|
21
23
|
"docs": "typedoc --options ./typedoc.config.js",
|
|
22
24
|
"prepublishOnly": "npm run test && npm run docs && npm run build",
|
|
23
25
|
"version": "npm run build:readme && git add README.md"
|
|
@@ -59,7 +61,7 @@
|
|
|
59
61
|
"express": "^4.17.1"
|
|
60
62
|
},
|
|
61
63
|
"dependencies": {
|
|
62
|
-
"@donmahallem/turbo": "^2.3.
|
|
64
|
+
"@donmahallem/turbo": "^2.3.1"
|
|
63
65
|
},
|
|
64
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "fa4c1e9bb2becb6793534cd6623437f6f63c8a60"
|
|
65
67
|
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/*
|
|
2
|
+
* Package @donmahallem/turbo-validate-request
|
|
3
|
+
* Source https://donmahallem.github.io/js-libs/
|
|
3
4
|
*/
|
|
4
5
|
|
|
5
6
|
import { RequestError } from '@donmahallem/turbo';
|
|
6
7
|
import { DefinedError } from 'ajv';
|
|
7
8
|
|
|
8
|
-
export const convertValidationError: (error: DefinedError) => RequestError =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
};
|
|
9
|
+
export const convertValidationError: (error: DefinedError) => RequestError = (error: DefinedError): RequestError => {
|
|
10
|
+
const errorPath: string = error.instancePath === '' ? 'root' : error.instancePath;
|
|
11
|
+
switch (error.keyword) {
|
|
12
|
+
case 'required':
|
|
13
|
+
return new RequestError(`Missing property ${error.params.missingProperty} at '${errorPath}'`, 400);
|
|
14
|
+
case 'pattern':
|
|
15
|
+
return new RequestError(`Value doesn't match pattern at: '${errorPath}'`, 400);
|
|
16
|
+
case 'type':
|
|
17
|
+
return new RequestError(`Invalid type at '${errorPath}'. Expected ${error.params.type}`, 400);
|
|
18
|
+
default:
|
|
19
|
+
return new RequestError(`Invalid '${errorPath}'`, 400);
|
|
20
|
+
}
|
|
21
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/*
|
|
2
|
+
* Package @donmahallem/turbo-validate-request
|
|
3
|
+
* Source https://donmahallem.github.io/js-libs/
|
|
3
4
|
*/
|
|
4
5
|
|
|
5
|
-
export {
|
|
6
|
-
validateRequest,
|
|
7
|
-
} from './validate-request';
|
|
6
|
+
export { validateRequest } from './validate-request';
|
package/src/validate-request.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/*
|
|
2
|
+
* Package @donmahallem/turbo-validate-request
|
|
3
|
+
* Source https://donmahallem.github.io/js-libs/
|
|
3
4
|
*/
|
|
4
5
|
|
|
5
6
|
import Ajv, { DefinedError, JSONSchemaType, ValidateFunction } from 'ajv';
|
|
@@ -7,26 +8,27 @@ import { NextFunction, Request, RequestHandler, Response } from 'express';
|
|
|
7
8
|
import { convertValidationError } from './convert-validation-error';
|
|
8
9
|
|
|
9
10
|
type CheckKeys = 'query' | 'params' | 'body';
|
|
10
|
-
type MethodSignature = <
|
|
11
|
-
schema: JSONSchemaType<T>,
|
|
12
|
-
ajvInstance?: Ajv) => RequestHandler;
|
|
11
|
+
type MethodSignature = <T = object>(key: CheckKeys, schema: JSONSchemaType<T>, ajvInstance?: Ajv) => RequestHandler;
|
|
13
12
|
/**
|
|
14
13
|
* Checks a request against the given schema
|
|
15
|
-
*
|
|
16
|
-
* @param
|
|
17
|
-
* @param
|
|
14
|
+
*
|
|
15
|
+
* @param {string} key property of the request to check the schema against
|
|
16
|
+
* @param {JSONSchemaType} schema the jsonschema
|
|
17
|
+
* @param {Ajv} ajvInstance optional ajv to be used
|
|
18
|
+
* @returns {RequestHandler} express middleware
|
|
18
19
|
*/
|
|
19
|
-
export const validateRequest: MethodSignature =
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
export const validateRequest: MethodSignature = <T>(
|
|
21
|
+
key: CheckKeys,
|
|
22
|
+
schema: JSONSchemaType<T>,
|
|
23
|
+
ajvInstance: Ajv = new Ajv()
|
|
24
|
+
): RequestHandler => {
|
|
25
|
+
const validateFunction: ValidateFunction = ajvInstance.compile(schema);
|
|
26
|
+
return (req: Request, res: Response, next: NextFunction): void => {
|
|
27
|
+
if (!validateFunction(req[key])) {
|
|
28
|
+
const errors: DefinedError[] = validateFunction.errors as DefinedError[];
|
|
29
|
+
next(convertValidationError(errors[0]));
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
next();
|
|
32
33
|
};
|
|
34
|
+
};
|