@degenerate/endpoints 0.0.5 → 0.0.6
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/endpoints/src/examples/express-server/app.d.ts.map +1 -1
- package/dist/endpoints/src/examples/express-server/app.js +2 -2
- package/package.json +2 -2
- package/dist/generic/src/_settings/authorization-settings.d.ts +0 -17
- package/dist/generic/src/_settings/authorization-settings.d.ts.map +0 -1
- package/dist/generic/src/_settings/authorization-settings.js +0 -20
- package/dist/generic/src/authorization/index.d.ts +0 -24
- package/dist/generic/src/authorization/index.d.ts.map +0 -1
- package/dist/generic/src/authorization/index.js +0 -61
- package/dist/generic/src/authorization/jwt/keys/create-public-key-pem.d.ts +0 -9
- package/dist/generic/src/authorization/jwt/keys/create-public-key-pem.d.ts.map +0 -1
- package/dist/generic/src/authorization/jwt/keys/create-public-key-pem.js +0 -25
- package/dist/generic/src/authorization/jwt/keys/create-secret-key-pem.d.ts +0 -9
- package/dist/generic/src/authorization/jwt/keys/create-secret-key-pem.d.ts.map +0 -1
- package/dist/generic/src/authorization/jwt/keys/create-secret-key-pem.js +0 -24
- package/dist/generic/src/authorization/jwt/sign/create-authorization-issuer.d.ts +0 -10
- package/dist/generic/src/authorization/jwt/sign/create-authorization-issuer.d.ts.map +0 -1
- package/dist/generic/src/authorization/jwt/sign/create-authorization-issuer.js +0 -26
- package/dist/generic/src/authorization/jwt/sign/create-authorization-subject.d.ts +0 -12
- package/dist/generic/src/authorization/jwt/sign/create-authorization-subject.d.ts.map +0 -1
- package/dist/generic/src/authorization/jwt/sign/create-authorization-subject.js +0 -19
- package/dist/generic/src/authorization/jwt/sign/create-jwt-options.d.ts +0 -17
- package/dist/generic/src/authorization/jwt/sign/create-jwt-options.d.ts.map +0 -1
- package/dist/generic/src/authorization/jwt/sign/create-jwt-options.js +0 -43
- package/dist/generic/src/authorization/jwt/sign/create-jwt-signed-token.d.ts +0 -13
- package/dist/generic/src/authorization/jwt/sign/create-jwt-signed-token.d.ts.map +0 -1
- package/dist/generic/src/authorization/jwt/sign/create-jwt-signed-token.js +0 -22
- package/dist/generic/src/authorization/jwt/transform/transform-token-payload-to-safe-payload.d.ts +0 -10
- package/dist/generic/src/authorization/jwt/transform/transform-token-payload-to-safe-payload.d.ts.map +0 -1
- package/dist/generic/src/authorization/jwt/transform/transform-token-payload-to-safe-payload.js +0 -31
- package/dist/generic/src/authorization/jwt/verify/get-decoded-jwt-header.d.ts +0 -10
- package/dist/generic/src/authorization/jwt/verify/get-decoded-jwt-header.d.ts.map +0 -1
- package/dist/generic/src/authorization/jwt/verify/get-decoded-jwt-header.js +0 -20
- package/dist/generic/src/authorization/jwt/verify/get-decoded-jwt-signed-token-payload.d.ts +0 -9
- package/dist/generic/src/authorization/jwt/verify/get-decoded-jwt-signed-token-payload.d.ts.map +0 -1
- package/dist/generic/src/authorization/jwt/verify/get-decoded-jwt-signed-token-payload.js +0 -17
- package/dist/generic/src/authorization/jwt/verify/verify-jwt-signed-token.d.ts +0 -12
- package/dist/generic/src/authorization/jwt/verify/verify-jwt-signed-token.d.ts.map +0 -1
- package/dist/generic/src/authorization/jwt/verify/verify-jwt-signed-token.js +0 -34
- package/dist/generic/src/authorization/safe/authorization/authorized-authorities.d.ts +0 -42
- package/dist/generic/src/authorization/safe/authorization/authorized-authorities.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/authorization/authorized-authorities.js +0 -65
- package/dist/generic/src/authorization/safe/request/decode-bearer-token.d.ts +0 -11
- package/dist/generic/src/authorization/safe/request/decode-bearer-token.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/request/decode-bearer-token.js +0 -31
- package/dist/generic/src/authorization/safe/request/decode-safe-request.d.ts +0 -11
- package/dist/generic/src/authorization/safe/request/decode-safe-request.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/request/decode-safe-request.js +0 -30
- package/dist/generic/src/authorization/safe/request/determine-issuer-from-request.d.ts +0 -10
- package/dist/generic/src/authorization/safe/request/determine-issuer-from-request.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/request/determine-issuer-from-request.js +0 -29
- package/dist/generic/src/authorization/safe/request/issuer/determine-issuer-from-bearer-token.d.ts +0 -12
- package/dist/generic/src/authorization/safe/request/issuer/determine-issuer-from-bearer-token.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/request/issuer/determine-issuer-from-bearer-token.js +0 -30
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-audience.d.ts +0 -10
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-audience.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-audience.js +0 -24
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-expiration-time.d.ts +0 -8
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-expiration-time.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-expiration-time.js +0 -23
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-issued-at-time.d.ts +0 -9
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-issued-at-time.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-issued-at-time.js +0 -24
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-issuer.d.ts +0 -10
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-issuer.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-issuer.js +0 -25
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-not-before-time.d.ts +0 -8
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-not-before-time.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-not-before-time.js +0 -23
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-subject.d.ts +0 -10
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-subject.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/validation/jwt/payload/verify-authorization-subject.js +0 -22
- package/dist/generic/src/authorization/safe/validation/jwt/verify-token-header.d.ts +0 -9
- package/dist/generic/src/authorization/safe/validation/jwt/verify-token-header.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/validation/jwt/verify-token-header.js +0 -25
- package/dist/generic/src/authorization/safe/validation/jwt/verify-token-payload.d.ts +0 -11
- package/dist/generic/src/authorization/safe/validation/jwt/verify-token-payload.d.ts.map +0 -1
- package/dist/generic/src/authorization/safe/validation/jwt/verify-token-payload.js +0 -30
- package/dist/generic/src/helpers/crypto/cryptography-helpers.d.ts +0 -37
- package/dist/generic/src/helpers/crypto/cryptography-helpers.d.ts.map +0 -1
- package/dist/generic/src/helpers/crypto/cryptography-helpers.js +0 -65
- package/dist/generic/src/helpers/object-validation-helpers.d.ts +0 -66
- package/dist/generic/src/helpers/object-validation-helpers.d.ts.map +0 -1
- package/dist/generic/src/helpers/object-validation-helpers.js +0 -254
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.verifyTokenPayload = void 0;
|
|
4
|
-
const authorization_errors_1 = require("../../../../errors/authorization/authorization-errors");
|
|
5
|
-
const verify_authorization_issuer_1 = require("./payload/verify-authorization-issuer");
|
|
6
|
-
const verify_authorization_subject_1 = require("./payload/verify-authorization-subject");
|
|
7
|
-
const verify_authorization_audience_1 = require("./payload/verify-authorization-audience");
|
|
8
|
-
const verify_authorization_not_before_time_1 = require("./payload/verify-authorization-not-before-time");
|
|
9
|
-
const verify_authorization_expiration_time_1 = require("./payload/verify-authorization-expiration-time");
|
|
10
|
-
const verify_authorization_issued_at_time_1 = require("./payload/verify-authorization-issued-at-time");
|
|
11
|
-
/**
|
|
12
|
-
* This function verifies the payload of the token to have the minimum token information from the decoded token payload.
|
|
13
|
-
*
|
|
14
|
-
* @param payload the payload to verify
|
|
15
|
-
* @returns void (throws an error if the payload is not valid)
|
|
16
|
-
*/
|
|
17
|
-
const verifyTokenPayload = (payload) => {
|
|
18
|
-
if (!payload || typeof payload !== 'object') {
|
|
19
|
-
throw new authorization_errors_1.SafeAuthorizationPayloadNotValidError();
|
|
20
|
-
}
|
|
21
|
-
// make sure the issued at time is a valid
|
|
22
|
-
(0, verify_authorization_issued_at_time_1.verifyAuthorizationIssuedAtTime)(payload.iat);
|
|
23
|
-
(0, verify_authorization_not_before_time_1.verifyAuthorizationNotBeforeTime)(payload.nbf);
|
|
24
|
-
(0, verify_authorization_expiration_time_1.verifyAuthorizationExpirationTime)(payload.exp);
|
|
25
|
-
(0, verify_authorization_subject_1.verifyAuthorizationSubject)(payload.sub);
|
|
26
|
-
(0, verify_authorization_audience_1.verifyAuthorizationAudience)(payload.aud);
|
|
27
|
-
(0, verify_authorization_issuer_1.verifyAuthorizationIssuer)(payload.iss);
|
|
28
|
-
return true;
|
|
29
|
-
};
|
|
30
|
-
exports.verifyTokenPayload = verifyTokenPayload;
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* this function converts a hex string to a base64 string.
|
|
3
|
-
*
|
|
4
|
-
* @param hex the hex string to convert to base64
|
|
5
|
-
* @returns
|
|
6
|
-
*/
|
|
7
|
-
declare const hexToBase64: (hexString: string) => string;
|
|
8
|
-
/**
|
|
9
|
-
* this function converts a base64 string to a hex string.
|
|
10
|
-
*
|
|
11
|
-
* @param base64 the base64 string to convert to hex
|
|
12
|
-
* @returns
|
|
13
|
-
*/
|
|
14
|
-
declare const base64ToHex: (base64String: string) => string;
|
|
15
|
-
/**
|
|
16
|
-
* this function converts a base64 string to a PEM string for a public key.
|
|
17
|
-
*
|
|
18
|
-
* @param base64 the base64 string to convert to PEM format.
|
|
19
|
-
* @returns
|
|
20
|
-
*/
|
|
21
|
-
declare const base64ToPublicPEM: (base64: string) => string;
|
|
22
|
-
/**
|
|
23
|
-
* this function converts a base64 string to a PEM string for an elliptical curve private key.
|
|
24
|
-
*
|
|
25
|
-
* @param base64 the base64 string to convert to PEM format.
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
28
|
-
declare const base64ToPrivateECPEM: (base64: string) => string;
|
|
29
|
-
/**
|
|
30
|
-
* this function converts a base64 string to a PEM string for a private key.
|
|
31
|
-
*
|
|
32
|
-
* @param base64 the base64 string to convert to PEM format.
|
|
33
|
-
* @returns
|
|
34
|
-
*/
|
|
35
|
-
declare const base64ToPrivatePEM: (base64: string) => string;
|
|
36
|
-
export { base64ToHex, base64ToPublicPEM, base64ToPrivateECPEM, base64ToPrivatePEM, hexToBase64, };
|
|
37
|
-
//# sourceMappingURL=cryptography-helpers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cryptography-helpers.d.ts","sourceRoot":"","sources":["../../../../../../generic/src/helpers/crypto/cryptography-helpers.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,QAAA,MAAM,WAAW,cAAe,MAAM,KAAG,MAExC,CAAC;AAEF;;;;;GAKG;AACH,QAAA,MAAM,WAAW,iBAAkB,MAAM,KAAG,MAE3C,CAAC;AAIF;;;;;GAKG;AACH,QAAA,MAAM,iBAAiB,WAAY,MAAM,KAAG,MAK3C,CAAC;AAEF;;;;;GAKG;AACH,QAAA,MAAM,oBAAoB,WAAY,MAAM,KAAG,MAK9C,CAAC;AAEF;;;;;GAKG;AACH,QAAA,MAAM,kBAAkB,WAAY,MAAM,KAAG,MAK5C,CAAC;AAIF,OAAO,EACH,WAAW,EACX,iBAAiB,EACjB,oBAAoB,EACpB,kBAAkB,EAClB,WAAW,GACd,CAAC"}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// This file contains the helper functions for cryptography in the application.
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.hexToBase64 = exports.base64ToPrivatePEM = exports.base64ToPrivateECPEM = exports.base64ToPublicPEM = exports.base64ToHex = void 0;
|
|
5
|
-
//#region hex to base 64 functions
|
|
6
|
-
/**
|
|
7
|
-
* this function converts a hex string to a base64 string.
|
|
8
|
-
*
|
|
9
|
-
* @param hex the hex string to convert to base64
|
|
10
|
-
* @returns
|
|
11
|
-
*/
|
|
12
|
-
const hexToBase64 = (hexString) => {
|
|
13
|
-
return Buffer.from(hexString, 'hex').toString('base64');
|
|
14
|
-
};
|
|
15
|
-
exports.hexToBase64 = hexToBase64;
|
|
16
|
-
/**
|
|
17
|
-
* this function converts a base64 string to a hex string.
|
|
18
|
-
*
|
|
19
|
-
* @param base64 the base64 string to convert to hex
|
|
20
|
-
* @returns
|
|
21
|
-
*/
|
|
22
|
-
const base64ToHex = (base64String) => {
|
|
23
|
-
return Buffer.from(base64String, 'base64').toString('hex');
|
|
24
|
-
};
|
|
25
|
-
exports.base64ToHex = base64ToHex;
|
|
26
|
-
//#region base 64 to PEM functions
|
|
27
|
-
/**
|
|
28
|
-
* this function converts a base64 string to a PEM string for a public key.
|
|
29
|
-
*
|
|
30
|
-
* @param base64 the base64 string to convert to PEM format.
|
|
31
|
-
* @returns
|
|
32
|
-
*/
|
|
33
|
-
const base64ToPublicPEM = (base64) => {
|
|
34
|
-
// split the base64 into 64 character chunks
|
|
35
|
-
const regex = /.{64}/g;
|
|
36
|
-
const formattedBase64 = base64.replace(regex, '$&\n');
|
|
37
|
-
return `-----BEGIN PUBLIC KEY-----\n${formattedBase64}\n-----END PUBLIC KEY-----`;
|
|
38
|
-
};
|
|
39
|
-
exports.base64ToPublicPEM = base64ToPublicPEM;
|
|
40
|
-
/**
|
|
41
|
-
* this function converts a base64 string to a PEM string for an elliptical curve private key.
|
|
42
|
-
*
|
|
43
|
-
* @param base64 the base64 string to convert to PEM format.
|
|
44
|
-
* @returns
|
|
45
|
-
*/
|
|
46
|
-
const base64ToPrivateECPEM = (base64) => {
|
|
47
|
-
// split the base64 into 64 character chunks
|
|
48
|
-
const regex = /.{64}/g;
|
|
49
|
-
const formattedBase64 = base64.replace(regex, '$&\n');
|
|
50
|
-
return `-----BEGIN EC PRIVATE KEY-----\n${formattedBase64}\n-----END EC PRIVATE KEY-----`;
|
|
51
|
-
};
|
|
52
|
-
exports.base64ToPrivateECPEM = base64ToPrivateECPEM;
|
|
53
|
-
/**
|
|
54
|
-
* this function converts a base64 string to a PEM string for a private key.
|
|
55
|
-
*
|
|
56
|
-
* @param base64 the base64 string to convert to PEM format.
|
|
57
|
-
* @returns
|
|
58
|
-
*/
|
|
59
|
-
const base64ToPrivatePEM = (base64) => {
|
|
60
|
-
// split the base64 into 64 character chunks
|
|
61
|
-
const regex = /.{64}/g;
|
|
62
|
-
const formattedBase64 = base64.replace(regex, '$&\n');
|
|
63
|
-
return `-----BEGIN PRIVATE KEY-----\n${formattedBase64}\n-----END PRIVATE KEY-----`;
|
|
64
|
-
};
|
|
65
|
-
exports.base64ToPrivatePEM = base64ToPrivatePEM;
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks that an object is an array and that it has the minimum number of items.
|
|
3
|
-
*
|
|
4
|
-
* @param incomingObject the object to check
|
|
5
|
-
* @param minimumLength the minimum number of objects to match (zero by default)
|
|
6
|
-
* @param maximumLength the maximum number of objects to match will default to the array length if not provided
|
|
7
|
-
*
|
|
8
|
-
* @returns whether the criteria matches.
|
|
9
|
-
*/
|
|
10
|
-
declare const validateArray: (incomingObject: unknown, minimumLength?: number, maximumLength?: number) => boolean;
|
|
11
|
-
/**
|
|
12
|
-
* This validates an incoming shape of a boolean type data.
|
|
13
|
-
*
|
|
14
|
-
* @param incomingObject an object to validate as a boolean.
|
|
15
|
-
* @param undefinedValid whether the item can be undefined or not (false by default)
|
|
16
|
-
*
|
|
17
|
-
* @returns whether the shape is valid.
|
|
18
|
-
*/
|
|
19
|
-
declare const validateBoolean: (incomingObject: unknown, undefinedValid?: boolean) => boolean;
|
|
20
|
-
/**
|
|
21
|
-
* This validates a date object or a string that can be converted to a date object.
|
|
22
|
-
* @param dateToValidate the date to validate
|
|
23
|
-
* @param undefinedValid whether the item can be undefined or not (false by default)
|
|
24
|
-
* @param allowFutureDated the date can be in the future (false by default)
|
|
25
|
-
* @param ensureAtLeast18YearsAgo whether the date is at least 18 years ago (true by default)
|
|
26
|
-
*
|
|
27
|
-
* @returns
|
|
28
|
-
*/
|
|
29
|
-
declare const validateDate: (dateToValidate: unknown, undefinedValid?: boolean, allowFutureDated?: boolean, ensureAtLeast18YearsAgo?: boolean) => boolean;
|
|
30
|
-
/**
|
|
31
|
-
* This validates that the incoming Object is a number and that it is in the range (if provided)
|
|
32
|
-
*
|
|
33
|
-
* @param incomingObject object to validate to a number
|
|
34
|
-
* @param min (optional) minimum number
|
|
35
|
-
* @param max (optional) maximum number
|
|
36
|
-
* @returns
|
|
37
|
-
*/
|
|
38
|
-
declare const validateNumber: (incomingObject: unknown, min?: number, max?: number) => boolean;
|
|
39
|
-
/**
|
|
40
|
-
* This validates an incoming shape of a set of files links to the data.
|
|
41
|
-
*
|
|
42
|
-
* @param incomingObject an object to validate as a string.
|
|
43
|
-
* @param hasNoSpaces whether the item should have no spaces in it (false by default)
|
|
44
|
-
* @param undefinedValid whether the item can be undefined or not (false by default)
|
|
45
|
-
* @param emptyStringValid whether the item can be an empty string or not (false by default)
|
|
46
|
-
*
|
|
47
|
-
* @returns whether the shape is valid.
|
|
48
|
-
*/
|
|
49
|
-
declare const validateString: (incomingObject: unknown, hasNoSpaces?: boolean, undefinedValid?: boolean, emptyStringValid?: boolean) => boolean;
|
|
50
|
-
/**
|
|
51
|
-
* This validates that an incoming object is a valid hex string without spaces.
|
|
52
|
-
*
|
|
53
|
-
* @param incomingObject the object to validate
|
|
54
|
-
* @returns
|
|
55
|
-
*/
|
|
56
|
-
declare const validateStringHex: (incomingObject: unknown) => boolean;
|
|
57
|
-
/**
|
|
58
|
-
* This validates that an incoming object is a valid URL.
|
|
59
|
-
*
|
|
60
|
-
* @param incomingUrl the URL to check
|
|
61
|
-
* @param undefinedValid whether the item can be undefined or not (false by default)
|
|
62
|
-
* @returns wether it is a valid URL or not
|
|
63
|
-
*/
|
|
64
|
-
declare const validateUrl: (incomingUrl: unknown, undefinedValid?: boolean) => boolean;
|
|
65
|
-
export { validateArray, validateBoolean, validateDate, validateNumber, validateString, validateStringHex, validateUrl, };
|
|
66
|
-
//# sourceMappingURL=object-validation-helpers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"object-validation-helpers.d.ts","sourceRoot":"","sources":["../../../../../generic/src/helpers/object-validation-helpers.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,QAAA,MAAM,aAAa,mBAAoB,OAAO,0CAAqC,MAAM,KAAG,OAc3F,CAAC;AAEF;;;;;;;GAOG;AACH,QAAA,MAAM,eAAe,mBAAoB,OAAO,+BAA2B,OAS1E,CAAC;AAEF;;;;;;;;GAQG;AACH,QAAA,MAAM,YAAY,mBAAoB,OAAO,8FACP,OAoCrC,CAAC;AAEF;;;;;;;GAOG;AACH,QAAA,MAAM,cAAc,mBAAoB,OAAO,QAAQ,MAAM,QAAQ,MAAM,KAAG,OAU7E,CAAC;AAEF;;;;;;;;;GASG;AACH,QAAA,MAAM,cAAc,mBAAoB,OAAO,kFAA0E,OAiCxH,CAAC;AAEF;;;;;GAKG;AACH,QAAA,MAAM,iBAAiB,mBAAoB,OAAO,KAAG,OAepD,CAAC;AAEF;;;;;;GAMG;AACH,QAAA,MAAM,WAAW,gBAAiB,OAAO,+BAA2B,OAqBnE,CAAC;AA+CF,OAAO,EACH,aAAa,EACb,eAAe,EACf,YAAY,EACZ,cAAc,EACd,cAAc,EACd,iBAAiB,EACjB,WAAW,GACd,CAAC"}
|
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// This file contains the validation functions for objects.
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.validateUrl = exports.validateStringHex = exports.validateString = exports.validateNumber = exports.validateDate = exports.validateBoolean = exports.validateArray = void 0;
|
|
5
|
-
/**
|
|
6
|
-
* Checks that an object is an array and that it has the minimum number of items.
|
|
7
|
-
*
|
|
8
|
-
* @param incomingObject the object to check
|
|
9
|
-
* @param minimumLength the minimum number of objects to match (zero by default)
|
|
10
|
-
* @param maximumLength the maximum number of objects to match will default to the array length if not provided
|
|
11
|
-
*
|
|
12
|
-
* @returns whether the criteria matches.
|
|
13
|
-
*/
|
|
14
|
-
const validateArray = (incomingObject, minimumLength = 0, maximumLength) => {
|
|
15
|
-
if (incomingObject && Array.isArray(incomingObject) && incomingObject.length >= minimumLength) {
|
|
16
|
-
if (!maximumLength) {
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
if (incomingObject.length <= maximumLength) {
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
exports.validateArray = validateArray;
|
|
33
|
-
/**
|
|
34
|
-
* This validates an incoming shape of a boolean type data.
|
|
35
|
-
*
|
|
36
|
-
* @param incomingObject an object to validate as a boolean.
|
|
37
|
-
* @param undefinedValid whether the item can be undefined or not (false by default)
|
|
38
|
-
*
|
|
39
|
-
* @returns whether the shape is valid.
|
|
40
|
-
*/
|
|
41
|
-
const validateBoolean = (incomingObject, undefinedValid = false) => {
|
|
42
|
-
if (undefinedValid && typeof incomingObject === 'undefined') { // if undefined is valid and it"s undefined
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
if (typeof incomingObject === 'boolean') {
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
exports.validateBoolean = validateBoolean;
|
|
53
|
-
/**
|
|
54
|
-
* This validates a date object or a string that can be converted to a date object.
|
|
55
|
-
* @param dateToValidate the date to validate
|
|
56
|
-
* @param undefinedValid whether the item can be undefined or not (false by default)
|
|
57
|
-
* @param allowFutureDated the date can be in the future (false by default)
|
|
58
|
-
* @param ensureAtLeast18YearsAgo whether the date is at least 18 years ago (true by default)
|
|
59
|
-
*
|
|
60
|
-
* @returns
|
|
61
|
-
*/
|
|
62
|
-
const validateDate = (dateToValidate, undefinedValid = false, allowFutureDated = false, ensureAtLeast18YearsAgo = false) => {
|
|
63
|
-
if (undefinedValid && (typeof dateToValidate === 'undefined' || dateToValidate === '' || dateToValidate === null)) {
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
let date; // a placeholder for the date object
|
|
67
|
-
if (typeof dateToValidate === 'string') {
|
|
68
|
-
date = new Date(dateToValidate); // try to parse the date
|
|
69
|
-
if (isNaN(date.getTime())) { // if it's not a valid date
|
|
70
|
-
return false; // return false
|
|
71
|
-
}
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
if (!(typeof dateToValidate === 'object' && dateToValidate !== null)) {
|
|
76
|
-
return false; // the date isn't an object
|
|
77
|
-
}
|
|
78
|
-
if (Object.prototype.toString.call(dateToValidate) !== '[object Date]') {
|
|
79
|
-
return false; // the date isn't a date object
|
|
80
|
-
}
|
|
81
|
-
// Check if the date is in the future and this is not allowed
|
|
82
|
-
if (!allowFutureDated && dateToValidate > new Date()) {
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
// determine if the date is newer than 18 years in the past
|
|
86
|
-
if (ensureAtLeast18YearsAgo) {
|
|
87
|
-
const today = new Date();
|
|
88
|
-
const eighteenYearsAgo = new Date(today.getFullYear() - 18, today.getMonth(), today.getDate());
|
|
89
|
-
return dateToValidate <= eighteenYearsAgo;
|
|
90
|
-
}
|
|
91
|
-
// otherwise the date is valid.
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
exports.validateDate = validateDate;
|
|
96
|
-
/**
|
|
97
|
-
* This validates that the incoming Object is a number and that it is in the range (if provided)
|
|
98
|
-
*
|
|
99
|
-
* @param incomingObject object to validate to a number
|
|
100
|
-
* @param min (optional) minimum number
|
|
101
|
-
* @param max (optional) maximum number
|
|
102
|
-
* @returns
|
|
103
|
-
*/
|
|
104
|
-
const validateNumber = (incomingObject, min, max) => {
|
|
105
|
-
if (typeof incomingObject === 'number') {
|
|
106
|
-
if (validateMin(incomingObject, min) && validateMax(incomingObject, max)) {
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
return false;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
exports.validateNumber = validateNumber;
|
|
118
|
-
/**
|
|
119
|
-
* This validates an incoming shape of a set of files links to the data.
|
|
120
|
-
*
|
|
121
|
-
* @param incomingObject an object to validate as a string.
|
|
122
|
-
* @param hasNoSpaces whether the item should have no spaces in it (false by default)
|
|
123
|
-
* @param undefinedValid whether the item can be undefined or not (false by default)
|
|
124
|
-
* @param emptyStringValid whether the item can be an empty string or not (false by default)
|
|
125
|
-
*
|
|
126
|
-
* @returns whether the shape is valid.
|
|
127
|
-
*/
|
|
128
|
-
const validateString = (incomingObject, hasNoSpaces = false, undefinedValid = false, emptyStringValid = false) => {
|
|
129
|
-
// if undefined is valid and check if it's undefined and if so, return true
|
|
130
|
-
if (undefinedValid) {
|
|
131
|
-
if (typeof incomingObject === 'undefined') { // if undefined is valid and it"s undefined
|
|
132
|
-
return true;
|
|
133
|
-
}
|
|
134
|
-
if (incomingObject === null) { // include null values.
|
|
135
|
-
return true;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
// Check if the string is empty and if it's valid to be empty
|
|
139
|
-
if (emptyStringValid) {
|
|
140
|
-
if (typeof incomingObject === 'string' && incomingObject.length === 0) {
|
|
141
|
-
return true;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
// if it's a string and it's not empty
|
|
145
|
-
if (typeof incomingObject === 'string') {
|
|
146
|
-
// check that it is a string and that it is not empty
|
|
147
|
-
if (incomingObject.length === 0) {
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
// check if it has no spaces
|
|
151
|
-
if (hasNoSpaces) {
|
|
152
|
-
return incomingObject.indexOf(' ') === -1;
|
|
153
|
-
}
|
|
154
|
-
// otherwise it's a valid string
|
|
155
|
-
return true;
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
return false;
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
exports.validateString = validateString;
|
|
162
|
-
/**
|
|
163
|
-
* This validates that an incoming object is a valid hex string without spaces.
|
|
164
|
-
*
|
|
165
|
-
* @param incomingObject the object to validate
|
|
166
|
-
* @returns
|
|
167
|
-
*/
|
|
168
|
-
const validateStringHex = (incomingObject) => {
|
|
169
|
-
// make sure its a string
|
|
170
|
-
const isStringWithoutSpaces = validateString(incomingObject, true, false, false);
|
|
171
|
-
// make sure its a valid hex string
|
|
172
|
-
if (!isStringWithoutSpaces) {
|
|
173
|
-
return false;
|
|
174
|
-
}
|
|
175
|
-
// check that it is a hex string
|
|
176
|
-
const hexStringRegexp = /^[0-9a-fA-F]+$/;
|
|
177
|
-
if (!hexStringRegexp.test(incomingObject)) {
|
|
178
|
-
return false;
|
|
179
|
-
}
|
|
180
|
-
return true;
|
|
181
|
-
};
|
|
182
|
-
exports.validateStringHex = validateStringHex;
|
|
183
|
-
/**
|
|
184
|
-
* This validates that an incoming object is a valid URL.
|
|
185
|
-
*
|
|
186
|
-
* @param incomingUrl the URL to check
|
|
187
|
-
* @param undefinedValid whether the item can be undefined or not (false by default)
|
|
188
|
-
* @returns wether it is a valid URL or not
|
|
189
|
-
*/
|
|
190
|
-
const validateUrl = (incomingUrl, undefinedValid = false) => {
|
|
191
|
-
if (undefinedValid && (typeof incomingUrl === 'undefined' || incomingUrl === '')) { // if undefined is valid and it"s undefined
|
|
192
|
-
return true;
|
|
193
|
-
}
|
|
194
|
-
if (typeof incomingUrl !== 'string') {
|
|
195
|
-
return false;
|
|
196
|
-
}
|
|
197
|
-
if (incomingUrl.indexOf(' ') !== -1) {
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
//Is it recognized as a valid URL?
|
|
201
|
-
let givenURL;
|
|
202
|
-
try {
|
|
203
|
-
givenURL = new URL(incomingUrl);
|
|
204
|
-
//Is the domain name a valid one with TLD and using HTTP or HTTPS?
|
|
205
|
-
const validDomainRegexp = /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/im;
|
|
206
|
-
return validDomainRegexp.test(givenURL.host) && (givenURL.protocol === 'http:' || givenURL.protocol === 'https:');
|
|
207
|
-
}
|
|
208
|
-
catch (error) {
|
|
209
|
-
return false;
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
exports.validateUrl = validateUrl;
|
|
213
|
-
//#endregion Public Functions
|
|
214
|
-
//#region Internal Functions
|
|
215
|
-
/**
|
|
216
|
-
* validates that an incoming number is greater than the minimum if it was provided.
|
|
217
|
-
*
|
|
218
|
-
* @param incomingNumber the number to check
|
|
219
|
-
* @param min the minimum value.
|
|
220
|
-
* @returns
|
|
221
|
-
*/
|
|
222
|
-
const validateMin = (incomingNumber, min) => {
|
|
223
|
-
if (typeof min === 'number') {
|
|
224
|
-
if (incomingNumber >= min) {
|
|
225
|
-
return true;
|
|
226
|
-
}
|
|
227
|
-
else {
|
|
228
|
-
return false;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
else {
|
|
232
|
-
return true;
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
/**
|
|
236
|
-
* validates that an incoming number is less than or equal to the mamimum if it was provided.
|
|
237
|
-
*
|
|
238
|
-
* @param incomingNumber the number to check
|
|
239
|
-
* @param max the maximum value.
|
|
240
|
-
* @returns
|
|
241
|
-
*/
|
|
242
|
-
const validateMax = (incomingNumber, max) => {
|
|
243
|
-
if (typeof max === 'number') {
|
|
244
|
-
if (incomingNumber <= max) {
|
|
245
|
-
return true;
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
return false;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
return true;
|
|
253
|
-
}
|
|
254
|
-
};
|