@donmahallem/turbo-validate-request 0.2.16 → 0.3.2

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 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
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
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
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
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
- * @param key property of the request to check the schema against
35
- * @param schema the jsonschema
36
- * @param ajvInstance optional ajv to be used
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: Mon Nov 29 2021 21:26:20 GMT+0100 (Central European Standard Time)
55
+ // BUILD: Fri Dec 24 2021 23:39:19 GMT+0100 (Central European Standard Time)
52
56
 
53
57
  //# sourceMappingURL=index.js.map
@@ -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;;;AAOO,MAAM,sBAAsB,GAC/B,CAAC,KAAmB;IAChB,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;;ACpBL;;;AAYA;;;;;;MAMa,eAAe,GACxB,CAAsC,GAAc,EAChD,MAAyB,EACzB,cAAmB,IAAIC,uBAAG,EAAE;IAC5B,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,CAAiB,CAAC,CAAC,CAAC;YACxD,OAAO;SACV;QACD,IAAI,EAAE,CAAC;KACV,CAAC;AACN;;;;;"}
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
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
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
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
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
- * @param key property of the request to check the schema against
27
- * @param schema the jsonschema
28
- * @param ajvInstance optional ajv to be used
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: Mon Nov 29 2021 21:26:20 GMT+0100 (Central European Standard Time)
47
+ // BUILD: Fri Dec 24 2021 23:39:19 GMT+0100 (Central European Standard Time)
44
48
 
45
49
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/convert-validation-error.ts","../../src/validate-request.ts"],"sourcesContent":[null,null],"names":[],"mappings":";;;AAAA;;;AAOO,MAAM,sBAAsB,GAC/B,CAAC,KAAmB;IAChB,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;;ACpBL;;;AAYA;;;;;;MAMa,eAAe,GACxB,CAAsC,GAAc,EAChD,MAAyB,EACzB,cAAmB,IAAI,GAAG,EAAE;IAC5B,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,CAAiB,CAAC,CAAC,CAAC;YACxD,OAAO;SACV;QACD,IAAI,EAAE,CAAC;KACV,CAAC;AACN;;;;;"}
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,6 +1,3 @@
1
- /*!
2
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
3
- */
4
1
  import { RequestError } from '@donmahallem/turbo';
5
2
  import { DefinedError } from 'ajv';
6
3
  export declare const convertValidationError: (error: DefinedError) => RequestError;
@@ -1 +1 @@
1
- {"version":3,"file":"convert-validation-error.d.ts","sourceRoot":"","sources":["../../src/convert-validation-error.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,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,YAazD,CAAC"}
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,5 +1,2 @@
1
- /*!
2
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
3
- */
4
1
  import 'mocha';
5
2
  //# sourceMappingURL=convert-validation-error.spec.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"convert-validation-error.spec.d.ts","sourceRoot":"","sources":["../../src/convert-validation-error.spec.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,OAAO,OAAO,CAAC"}
1
+ {"version":3,"file":"convert-validation-error.spec.d.ts","sourceRoot":"","sources":["../../src/convert-validation-error.spec.ts"],"names":[],"mappings":"AAQA,OAAO,OAAO,CAAC"}
@@ -1,5 +1,2 @@
1
- /*!
2
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
3
- */
4
- export { validateRequest, } from './validate-request';
1
+ export { validateRequest } from './validate-request';
5
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACH,eAAe,GAClB,MAAM,oBAAoB,CAAC"}
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
- * @param key property of the request to check the schema against
11
- * @param schema the jsonschema
12
- * @param ajvInstance optional ajv to be used
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":"AAAA;;GAEG;AAEH,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,CAAE,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,SAAS,EAC/C,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,EACzB,WAAW,CAAC,EAAE,GAAG,KAAK,cAAc,CAAC;AACzC;;;;;GAKG;AACH,eAAO,MAAM,eAAe,EAAE,eAazB,CAAC"}
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,5 +1,2 @@
1
- /*!
2
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
3
- */
4
1
  import 'mocha';
5
2
  //# sourceMappingURL=validate-request.spec.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"validate-request.spec.d.ts","sourceRoot":"","sources":["../../src/validate-request.spec.ts"],"names":[],"mappings":"AAAA;;GAEG;AAMH,OAAO,OAAO,CAAC"}
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.2.16",
3
+ "version": "0.3.2",
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": "tslint -c tslint.json -p tsconfig.json src/**/*.ts",
20
- "lint:fix": "tslint --fix -c tslint.json -p tsconfig.json src/**/*.ts",
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"
@@ -37,7 +39,7 @@
37
39
  "url": "https://github.com/donmahallem/js-libs.git"
38
40
  },
39
41
  "engines": {
40
- "node": "^12 || ^14 || ^16 || ^17"
42
+ "node": ">=14"
41
43
  },
42
44
  "files": [
43
45
  "dist/*",
@@ -51,7 +53,7 @@
51
53
  "@types/express": "4.17.13",
52
54
  "@types/supertest": "2.0.11",
53
55
  "ajv": "8.8.2",
54
- "express": "4.17.1",
56
+ "express": "4.17.2",
55
57
  "supertest": "6.1.6"
56
58
  },
57
59
  "peerDependencies": {
@@ -59,7 +61,7 @@
59
61
  "express": "^4.17.1"
60
62
  },
61
63
  "dependencies": {
62
- "@donmahallem/turbo": "^2.2.7"
64
+ "@donmahallem/turbo": "^2.3.2"
63
65
  },
64
- "gitHead": "c340f6a4a21874dbdac89729e5e2598f626fa07c"
66
+ "gitHead": "b63ac10a47594d6aed8e9135767446aed967faad"
65
67
  }
@@ -1,21 +1,21 @@
1
- /*!
2
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
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
- (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
- };
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
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
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';
@@ -1,5 +1,6 @@
1
- /*!
2
- * Source https://github.com/donmahallem/js-libs Package: turbo-validate-request
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 = < T = object>(key: CheckKeys,
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
- * @param key property of the request to check the schema against
16
- * @param schema the jsonschema
17
- * @param ajvInstance optional ajv to be used
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
- <T, _partial extends boolean = false>(key: CheckKeys,
21
- schema: JSONSchemaType<T>,
22
- ajvInstance: Ajv = new Ajv()): RequestHandler => {
23
- const validateFunction: ValidateFunction = ajvInstance.compile(schema);
24
- return (req: Request, res: Response, next: NextFunction): void => {
25
- if (!validateFunction(req[key])) {
26
- const errors: DefinedError[] = validateFunction.errors as DefinedError[];
27
- next(convertValidationError(errors[0] as DefinedError));
28
- return;
29
- }
30
- next();
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
+ };