@ikonintegration/ikapi 4.0.0-alpha10 → 4.0.0-alpha12
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/package.json +2 -2
- package/src/Validation/IKValidation.js +12 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ikonintegration/ikapi",
|
|
3
|
-
"version": "4.0.0-
|
|
3
|
+
"version": "4.0.0-alpha12",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "main.js",
|
|
@@ -32,6 +32,6 @@
|
|
|
32
32
|
"pg": "^8.11.1",
|
|
33
33
|
"sha1": "^1.1.1",
|
|
34
34
|
"stack-trace": "0.0.10",
|
|
35
|
-
"superstruct": "0.
|
|
35
|
+
"superstruct": "1.0.3"
|
|
36
36
|
}
|
|
37
37
|
}
|
|
@@ -2,29 +2,30 @@ import IKGlobals from './../IKGlobals.js';
|
|
|
2
2
|
import Utils from './../API/IKUtils.js';
|
|
3
3
|
import IKResponse, { IKBadRequestResponse } from './../API/IKResponse.js';
|
|
4
4
|
//https://www.npmjs.com/package/superstruct
|
|
5
|
-
import
|
|
5
|
+
import * as struct from 'superstruct';
|
|
6
6
|
//validation libs
|
|
7
7
|
import isUuid from 'is-uuid';
|
|
8
8
|
import isEmail from 'is-email';
|
|
9
9
|
//
|
|
10
10
|
export default class IKValidation {
|
|
11
11
|
constructor(config) {
|
|
12
|
+
this.customStruct = {};
|
|
12
13
|
this._buildInternalTypes((config && config.additionalTypes ? config.additionalTypes : {}));
|
|
13
14
|
}
|
|
14
15
|
_buildInternalTypes(additionalTypes) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
16
|
+
for (const key in IKValidation.internalTypes()) {
|
|
17
|
+
this.customStruct[key] = struct.define(key, IKValidation.internalTypes()[key]);
|
|
18
|
+
}
|
|
19
|
+
for (const key in additionalTypes) {
|
|
20
|
+
this.customStruct[key] = struct.define(key, additionalTypes[key]);
|
|
21
|
+
}
|
|
21
22
|
}
|
|
22
23
|
async validate(spec, obj, request) {
|
|
23
|
-
let
|
|
24
|
+
let assertion = null;
|
|
24
25
|
let err = null;
|
|
25
26
|
try {
|
|
26
27
|
let parsedRawObj = obj;
|
|
27
|
-
|
|
28
|
+
assertion = struct.assert(parsedRawObj, spec);
|
|
28
29
|
} catch (e) {
|
|
29
30
|
console.debug(e);
|
|
30
31
|
const { path, value, type } = e;
|
|
@@ -38,17 +39,14 @@ export default class IKValidation {
|
|
|
38
39
|
err = IKBadRequestResponse(IKGlobals.ErrorResponseValidationFail + errorMessage, IKGlobals.ErrorCode_InvalidInput);
|
|
39
40
|
}
|
|
40
41
|
//Catch error as response
|
|
41
|
-
if (
|
|
42
|
+
if (err) return err;
|
|
42
43
|
//run normal code out of this try/catch scope
|
|
43
|
-
return await request(
|
|
44
|
+
return await request(obj);
|
|
44
45
|
}
|
|
45
46
|
}
|
|
46
47
|
//
|
|
47
48
|
IKValidation.internalTypes = function() {
|
|
48
49
|
return {
|
|
49
|
-
optionalNumber: value => (value == undefined || !isNaN(parseInt(value))),
|
|
50
|
-
optionalValidString: value => { return (!value || Utils.isValidString(value) == true); },
|
|
51
|
-
validString: value => { return (Utils.isValidString(value) == true); },
|
|
52
50
|
email: value => { return (isEmail(value) && value.length < 256); },
|
|
53
51
|
fullAddress: value => {
|
|
54
52
|
if (!value) return false;
|