@aneuhold/core-ts-db-lib 1.0.8 → 1.0.10
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/lib/documents/common/ApiKey.d.ts +6 -2
- package/lib/documents/common/ApiKey.d.ts.map +1 -1
- package/lib/documents/common/ApiKey.js +11 -0
- package/lib/documents/common/User.d.ts +2 -0
- package/lib/documents/common/User.d.ts.map +1 -1
- package/lib/documents/common/User.js +16 -0
- package/lib/documents/dashboard/UserConfig.d.ts +6 -2
- package/lib/documents/dashboard/UserConfig.d.ts.map +1 -1
- package/lib/documents/dashboard/UserConfig.js +11 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -3
- package/lib/schemas/required-refs/RequiredUserId.d.ts +9 -0
- package/lib/schemas/required-refs/RequiredUserId.d.ts.map +1 -0
- package/lib/schemas/required-refs/RequiredUserId.js +12 -0
- package/lib/schemas/required_refs/RequiredUserId.d.ts +9 -0
- package/lib/schemas/required_refs/RequiredUserId.d.ts.map +1 -0
- package/lib/schemas/required_refs/RequiredUserId.js +12 -0
- package/lib/schemas/type-guards/commonTypeGuards.d.ts +6 -0
- package/lib/schemas/type-guards/commonTypeGuards.d.ts.map +1 -0
- package/lib/schemas/type-guards/commonTypeGuards.js +23 -0
- package/lib/schemas/validators/DocumentValidator.d.ts +6 -0
- package/lib/schemas/validators/DocumentValidator.d.ts.map +1 -0
- package/lib/schemas/validators/DocumentValidator.js +2 -0
- package/lib/schemas/validators/ValidateUtil.d.ts +45 -0
- package/lib/schemas/validators/ValidateUtil.d.ts.map +1 -0
- package/lib/schemas/validators/ValidateUtil.js +137 -0
- package/lib/schemas/validators/ValidateUtil.spec.d.ts +2 -0
- package/lib/schemas/validators/ValidateUtil.spec.d.ts.map +1 -0
- package/lib/schemas/validators/ValidateUtil.spec.js +55 -0
- package/package.json +1 -1
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { ObjectId } from 'bson';
|
|
2
2
|
import BaseDocument from '../BaseDocument';
|
|
3
|
-
import
|
|
3
|
+
import RequiredUserId from '../../schemas/required-refs/RequiredUserId';
|
|
4
|
+
export declare const validateApiKey: (apiKey: ApiKey) => {
|
|
5
|
+
updatedDoc: ApiKey;
|
|
6
|
+
errors: string[];
|
|
7
|
+
};
|
|
4
8
|
/**
|
|
5
9
|
* A document containing an API key for a particular user. This is stored
|
|
6
10
|
* separately from the {@link User} document to enhance security a bit.
|
|
7
11
|
*/
|
|
8
|
-
export default class ApiKey extends BaseDocument implements
|
|
12
|
+
export default class ApiKey extends BaseDocument implements RequiredUserId {
|
|
9
13
|
/**
|
|
10
14
|
* The API key for the user. This is indexed in the DB.
|
|
11
15
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiKey.d.ts","sourceRoot":"","sources":["../../../src/documents/common/ApiKey.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAEhC,OAAO,YAAY,MAAM,iBAAiB,CAAC;AAC3C,OAAO,
|
|
1
|
+
{"version":3,"file":"ApiKey.d.ts","sourceRoot":"","sources":["../../../src/documents/common/ApiKey.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAEhC,OAAO,YAAY,MAAM,iBAAiB,CAAC;AAC3C,OAAO,cAAc,MAAM,4CAA4C,CAAC;AAGxE,eAAO,MAAM,cAAc,WAAY,MAAM;;;CAQ5C,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,MAAO,SAAQ,YAAa,YAAW,cAAc;IACxE;;OAEG;IACH,GAAG,sDAAuB;IAE1B;;OAEG;IACH,MAAM,EAAE,QAAQ,CAAC;IAEjB;;OAEG;gBACS,MAAM,EAAE,QAAQ;CAI7B"}
|
|
@@ -3,8 +3,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validateApiKey = void 0;
|
|
7
|
+
const bson_1 = require("bson");
|
|
6
8
|
const crypto_1 = __importDefault(require("crypto"));
|
|
7
9
|
const BaseDocument_1 = __importDefault(require("../BaseDocument"));
|
|
10
|
+
const ValidateUtil_1 = __importDefault(require("../../schemas/validators/ValidateUtil"));
|
|
11
|
+
const validateApiKey = (apiKey) => {
|
|
12
|
+
const errors = [];
|
|
13
|
+
const exampleApiKey = new ApiKey(new bson_1.ObjectId());
|
|
14
|
+
const validate = new ValidateUtil_1.default(apiKey, errors);
|
|
15
|
+
validate.string('key', exampleApiKey.key);
|
|
16
|
+
return { updatedDoc: apiKey, errors };
|
|
17
|
+
};
|
|
18
|
+
exports.validateApiKey = validateApiKey;
|
|
8
19
|
/**
|
|
9
20
|
* A document containing an API key for a particular user. This is stored
|
|
10
21
|
* separately from the {@link User} document to enhance security a bit.
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { ObjectId } from 'bson';
|
|
2
2
|
import BaseDocument from '../BaseDocument';
|
|
3
|
+
import { DocumentValidator } from '../../schemas/validators/DocumentValidator';
|
|
4
|
+
export declare const validateUser: DocumentValidator<User>;
|
|
3
5
|
/**
|
|
4
6
|
* A standard user of all personal projects. This should be linked to from
|
|
5
7
|
* other documents that need to reference a user, instead of cluttering the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"User.d.ts","sourceRoot":"","sources":["../../../src/documents/common/User.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,YAAY,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"User.d.ts","sourceRoot":"","sources":["../../../src/documents/common/User.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,YAAY,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAG/E,eAAO,MAAM,YAAY,EAAE,iBAAiB,CAAC,IAAI,CAiBhD,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,IAAK,SAAQ,YAAY;IAC5C,GAAG,WAAkB;IAErB,QAAQ,EAAE,MAAM,CAAC;IAEjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,IAAI,EAAE;QACJ,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,CAAM;IAEP,aAAa;;MAEX;IAEF;;OAEG;gBACS,QAAQ,EAAE,MAAM;CAI7B"}
|
|
@@ -3,8 +3,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validateUser = void 0;
|
|
6
7
|
const bson_1 = require("bson");
|
|
7
8
|
const BaseDocument_1 = __importDefault(require("../BaseDocument"));
|
|
9
|
+
const ValidateUtil_1 = __importDefault(require("../../schemas/validators/ValidateUtil"));
|
|
10
|
+
const validateUser = (user) => {
|
|
11
|
+
const errors = [];
|
|
12
|
+
const validate = new ValidateUtil_1.default(user, errors);
|
|
13
|
+
const exampleUser = new User('example');
|
|
14
|
+
validate.string('userName', 'UsernameUnknown');
|
|
15
|
+
validate.optionalString('password');
|
|
16
|
+
validate.optionalString('email');
|
|
17
|
+
validate.object('auth', exampleUser.auth);
|
|
18
|
+
validate.optionalString('auth.googleId');
|
|
19
|
+
validate.object('projectAccess', exampleUser.projectAccess);
|
|
20
|
+
validate.boolean('projectAccess.dashboard', exampleUser.projectAccess.dashboard);
|
|
21
|
+
return { updatedDoc: user, errors };
|
|
22
|
+
};
|
|
23
|
+
exports.validateUser = validateUser;
|
|
8
24
|
/**
|
|
9
25
|
* A standard user of all personal projects. This should be linked to from
|
|
10
26
|
* other documents that need to reference a user, instead of cluttering the
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { ObjectId } from 'bson';
|
|
2
2
|
import BaseDocumentWithType from '../BaseDocumentWithType';
|
|
3
|
-
import
|
|
4
|
-
export
|
|
3
|
+
import RequiredUserId from '../../schemas/required-refs/RequiredUserId';
|
|
4
|
+
export declare const validateDashboardUserConfig: (config: DashboardUserConfig) => {
|
|
5
|
+
updatedDoc: DashboardUserConfig;
|
|
6
|
+
errors: string[];
|
|
7
|
+
};
|
|
8
|
+
export default class DashboardUserConfig extends BaseDocumentWithType implements RequiredUserId {
|
|
5
9
|
static docType: string;
|
|
6
10
|
docType: string;
|
|
7
11
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UserConfig.d.ts","sourceRoot":"","sources":["../../../src/documents/dashboard/UserConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,oBAAoB,MAAM,yBAAyB,CAAC;AAC3D,OAAO,
|
|
1
|
+
{"version":3,"file":"UserConfig.d.ts","sourceRoot":"","sources":["../../../src/documents/dashboard/UserConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,oBAAoB,MAAM,yBAAyB,CAAC;AAC3D,OAAO,cAAc,MAAM,4CAA4C,CAAC;AAGxE,eAAO,MAAM,2BAA2B,WAAY,mBAAmB;;;CAQtE,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,mBACnB,SAAQ,oBACR,YAAW,cAAc;IAEzB,MAAM,CAAC,OAAO,SAAgB;IAE9B,OAAO,SAA+B;IAEtC;;OAEG;IACH,MAAM,EAAE,QAAQ,CAAC;IAEjB;;OAEG;IACH,aAAa,UAAS;gBAEV,OAAO,EAAE,QAAQ;CAI9B"}
|
|
@@ -3,7 +3,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validateDashboardUserConfig = void 0;
|
|
7
|
+
const bson_1 = require("bson");
|
|
6
8
|
const BaseDocumentWithType_1 = __importDefault(require("../BaseDocumentWithType"));
|
|
9
|
+
const ValidateUtil_1 = __importDefault(require("../../schemas/validators/ValidateUtil"));
|
|
10
|
+
const validateDashboardUserConfig = (config) => {
|
|
11
|
+
const errors = [];
|
|
12
|
+
const validate = new ValidateUtil_1.default(config, errors);
|
|
13
|
+
const exampleConfig = new DashboardUserConfig(new bson_1.ObjectId());
|
|
14
|
+
validate.boolean('enableDevMode', exampleConfig.enableDevMode);
|
|
15
|
+
return { updatedDoc: config, errors };
|
|
16
|
+
};
|
|
17
|
+
exports.validateDashboardUserConfig = validateDashboardUserConfig;
|
|
7
18
|
class DashboardUserConfig extends BaseDocumentWithType_1.default {
|
|
8
19
|
static docType = 'userConfig';
|
|
9
20
|
docType = DashboardUserConfig.docType;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import BaseDocument from './documents/BaseDocument';
|
|
2
2
|
import BaseDocumentWithType from './documents/BaseDocumentWithType';
|
|
3
|
-
import BaseDocumentWithUserId from './documents/BaseDocumentWithUserId';
|
|
4
3
|
import ApiKey from './documents/common/ApiKey';
|
|
5
4
|
import User from './documents/common/User';
|
|
6
5
|
import DashboardUserConfig from './documents/dashboard/UserConfig';
|
|
7
|
-
|
|
6
|
+
import RequiredUserId from './schemas/required-refs/RequiredUserId';
|
|
7
|
+
export { User, ApiKey, DashboardUserConfig, BaseDocument, BaseDocumentWithType, RequiredUserId };
|
|
8
8
|
export type {};
|
|
9
9
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,0BAA0B,CAAC;AACpD,OAAO,oBAAoB,MAAM,kCAAkC,CAAC;AACpE,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,0BAA0B,CAAC;AACpD,OAAO,oBAAoB,MAAM,kCAAkC,CAAC;AACpE,OAAO,MAAM,MAAM,2BAA2B,CAAC;AAC/C,OAAO,IAAI,MAAM,yBAAyB,CAAC;AAC3C,OAAO,mBAAmB,MAAM,kCAAkC,CAAC;AACnE,OAAO,cAAc,MAAM,wCAAwC,CAAC;AAGpE,OAAO,EACL,IAAI,EACJ,MAAM,EACN,mBAAmB,EACnB,YAAY,EACZ,oBAAoB,EACpB,cAAc,EACf,CAAC;AAGF,YAAY,EAAE,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -3,16 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.RequiredUserId = exports.BaseDocumentWithType = exports.BaseDocument = exports.DashboardUserConfig = exports.ApiKey = exports.User = void 0;
|
|
7
7
|
const BaseDocument_1 = __importDefault(require("./documents/BaseDocument"));
|
|
8
8
|
exports.BaseDocument = BaseDocument_1.default;
|
|
9
9
|
const BaseDocumentWithType_1 = __importDefault(require("./documents/BaseDocumentWithType"));
|
|
10
10
|
exports.BaseDocumentWithType = BaseDocumentWithType_1.default;
|
|
11
|
-
const BaseDocumentWithUserId_1 = __importDefault(require("./documents/BaseDocumentWithUserId"));
|
|
12
|
-
exports.BaseDocumentWithUserId = BaseDocumentWithUserId_1.default;
|
|
13
11
|
const ApiKey_1 = __importDefault(require("./documents/common/ApiKey"));
|
|
14
12
|
exports.ApiKey = ApiKey_1.default;
|
|
15
13
|
const User_1 = __importDefault(require("./documents/common/User"));
|
|
16
14
|
exports.User = User_1.default;
|
|
17
15
|
const UserConfig_1 = __importDefault(require("./documents/dashboard/UserConfig"));
|
|
18
16
|
exports.DashboardUserConfig = UserConfig_1.default;
|
|
17
|
+
const RequiredUserId_1 = __importDefault(require("./schemas/required-refs/RequiredUserId"));
|
|
18
|
+
exports.RequiredUserId = RequiredUserId_1.default;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ObjectId } from 'bson';
|
|
2
|
+
import BaseDocument from '../../documents/BaseDocument';
|
|
3
|
+
/**
|
|
4
|
+
* A document that has a `userId` that must be associated to be valid.
|
|
5
|
+
*/
|
|
6
|
+
export default abstract class RequiredUserId extends BaseDocument {
|
|
7
|
+
abstract userId: ObjectId;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=RequiredUserId.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequiredUserId.d.ts","sourceRoot":"","sources":["../../../src/schemas/required-refs/RequiredUserId.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,YAAY,MAAM,8BAA8B,CAAC;AAExD;;GAEG;AACH,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,cAAe,SAAQ,YAAY;IAC/D,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;CAC3B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const BaseDocument_1 = __importDefault(require("../../documents/BaseDocument"));
|
|
7
|
+
/**
|
|
8
|
+
* A document that has a `userId` that must be associated to be valid.
|
|
9
|
+
*/
|
|
10
|
+
class RequiredUserId extends BaseDocument_1.default {
|
|
11
|
+
}
|
|
12
|
+
exports.default = RequiredUserId;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ObjectId } from 'bson';
|
|
2
|
+
import BaseDocument from '../../documents/BaseDocument';
|
|
3
|
+
/**
|
|
4
|
+
* A document that has a `userId` that must be associated to be valid.
|
|
5
|
+
*/
|
|
6
|
+
export default abstract class RequiredUserId extends BaseDocument {
|
|
7
|
+
abstract userId: ObjectId;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=RequiredUserId.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequiredUserId.d.ts","sourceRoot":"","sources":["../../../src/schemas/required_refs/RequiredUserId.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,YAAY,MAAM,8BAA8B,CAAC;AAExD;;GAEG;AACH,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,cAAe,SAAQ,YAAY;IAC/D,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;CAC3B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const BaseDocument_1 = __importDefault(require("../../documents/BaseDocument"));
|
|
7
|
+
/**
|
|
8
|
+
* A document that has a `userId` that must be associated to be valid.
|
|
9
|
+
*/
|
|
10
|
+
class RequiredUserId extends BaseDocument_1.default {
|
|
11
|
+
}
|
|
12
|
+
exports.default = RequiredUserId;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function isOptionalObject(value: unknown): value is undefined | object;
|
|
2
|
+
export declare function isOptionalArray(value: unknown): value is undefined | Array<unknown>;
|
|
3
|
+
export declare function isOptionalString(value: unknown): value is undefined | string;
|
|
4
|
+
export declare function isOptionalNumber(value: unknown): value is undefined | number;
|
|
5
|
+
export declare function isOptionalBoolean(value: unknown): value is undefined | boolean;
|
|
6
|
+
//# sourceMappingURL=commonTypeGuards.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commonTypeGuards.d.ts","sourceRoot":"","sources":["../../../src/schemas/type-guards/commonTypeGuards.ts"],"names":[],"mappings":"AAAA,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,GAAG,MAAM,CAE5E;AAED,wBAAgB,eAAe,CAC7B,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,CAErC;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,GAAG,MAAM,CAE5E;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,GAAG,MAAM,CAE5E;AAED,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,SAAS,GAAG,OAAO,CAE9B"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isOptionalBoolean = exports.isOptionalNumber = exports.isOptionalString = exports.isOptionalArray = exports.isOptionalObject = void 0;
|
|
4
|
+
function isOptionalObject(value) {
|
|
5
|
+
return value === undefined || typeof value === 'object';
|
|
6
|
+
}
|
|
7
|
+
exports.isOptionalObject = isOptionalObject;
|
|
8
|
+
function isOptionalArray(value) {
|
|
9
|
+
return value === undefined || Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
exports.isOptionalArray = isOptionalArray;
|
|
12
|
+
function isOptionalString(value) {
|
|
13
|
+
return value === undefined || typeof value === 'string';
|
|
14
|
+
}
|
|
15
|
+
exports.isOptionalString = isOptionalString;
|
|
16
|
+
function isOptionalNumber(value) {
|
|
17
|
+
return value === undefined || typeof value === 'number';
|
|
18
|
+
}
|
|
19
|
+
exports.isOptionalNumber = isOptionalNumber;
|
|
20
|
+
function isOptionalBoolean(value) {
|
|
21
|
+
return value === undefined || typeof value === 'boolean';
|
|
22
|
+
}
|
|
23
|
+
exports.isOptionalBoolean = isOptionalBoolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DocumentValidator.d.ts","sourceRoot":"","sources":["../../../src/schemas/validators/DocumentValidator.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,8BAA8B,CAAC;AAExD,MAAM,MAAM,iBAAiB,CAAC,QAAQ,SAAS,YAAY,IAAI,CAC7D,GAAG,EAAE,QAAQ,KACV;IAAE,UAAU,EAAE,QAAQ,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
type IndexableObject = {
|
|
2
|
+
[keyName: string]: unknown;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* All the validators in this class will modify the parent object to the
|
|
6
|
+
* default value if the field is not correct. If the field is optional, it
|
|
7
|
+
* will update the value to be undefined if it is not correct.
|
|
8
|
+
*
|
|
9
|
+
* This class can be used to validate nested objects by using a field path
|
|
10
|
+
* (e.g. 'projectAccess.dashboard'). If the field path is invalid, then
|
|
11
|
+
* this class will not report an error, because it is assumed that the
|
|
12
|
+
* parent of the field path will report the error.
|
|
13
|
+
*
|
|
14
|
+
* Document relations are not validated by this class. That is the job of the
|
|
15
|
+
* Document's repository.
|
|
16
|
+
*/
|
|
17
|
+
export default class Validate {
|
|
18
|
+
private errorsArray;
|
|
19
|
+
parentObject: IndexableObject;
|
|
20
|
+
constructor(parentObject: object, errorsArray: string[]);
|
|
21
|
+
optionalString(fieldName: string): void;
|
|
22
|
+
optionalNumber(fieldName: string): void;
|
|
23
|
+
optionalBoolean(fieldName: string): void;
|
|
24
|
+
optionalArray(fieldName: string): void;
|
|
25
|
+
string(fieldName: string, defaultValue: string): void;
|
|
26
|
+
object(fieldName: string, defaultValue: object): void;
|
|
27
|
+
number(fieldName: string, defaultValue: number): void;
|
|
28
|
+
boolean(fieldName: string, defaultValue: boolean): void;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if the provided field path is valid. In other words, if the
|
|
31
|
+
* field path is of length 1, it always returns true. If the field path
|
|
32
|
+
* is any longer than 1, it will check to make sure the final field is
|
|
33
|
+
* defined in the parent object.
|
|
34
|
+
*
|
|
35
|
+
* The idea is that if the field path is invalid, then this class will
|
|
36
|
+
* not report an error, because it is assumed that the parent of the field
|
|
37
|
+
* path will report the error.
|
|
38
|
+
*/
|
|
39
|
+
private fieldPathIsValid;
|
|
40
|
+
private updateField;
|
|
41
|
+
private getField;
|
|
42
|
+
private deleteField;
|
|
43
|
+
}
|
|
44
|
+
export {};
|
|
45
|
+
//# sourceMappingURL=ValidateUtil.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidateUtil.d.ts","sourceRoot":"","sources":["../../../src/schemas/validators/ValidateUtil.ts"],"names":[],"mappings":"AAOA,KAAK,eAAe,GAAG;IAAE,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAC;AAEtD;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,OAAO,OAAO,QAAQ;IAKzB,OAAO,CAAC,WAAW;IAJrB,YAAY,EAAE,eAAe,CAAC;gBAG5B,YAAY,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EAAE;IAK/B,cAAc,CAAC,SAAS,EAAE,MAAM;IAUhC,cAAc,CAAC,SAAS,EAAE,MAAM;IAUhC,eAAe,CAAC,SAAS,EAAE,MAAM;IAUjC,aAAa,CAAC,SAAS,EAAE,MAAM;IAU/B,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM;IAU9C,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM;IAU9C,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM;IAU9C,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO;IAUhD;;;;;;;;;OASG;IACH,OAAO,CAAC,gBAAgB;IAexB,OAAO,CAAC,WAAW;IAYnB,OAAO,CAAC,QAAQ;IAShB,OAAO,CAAC,WAAW;CAWpB"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const commonTypeGuards_1 = require("../type-guards/commonTypeGuards");
|
|
4
|
+
/**
|
|
5
|
+
* All the validators in this class will modify the parent object to the
|
|
6
|
+
* default value if the field is not correct. If the field is optional, it
|
|
7
|
+
* will update the value to be undefined if it is not correct.
|
|
8
|
+
*
|
|
9
|
+
* This class can be used to validate nested objects by using a field path
|
|
10
|
+
* (e.g. 'projectAccess.dashboard'). If the field path is invalid, then
|
|
11
|
+
* this class will not report an error, because it is assumed that the
|
|
12
|
+
* parent of the field path will report the error.
|
|
13
|
+
*
|
|
14
|
+
* Document relations are not validated by this class. That is the job of the
|
|
15
|
+
* Document's repository.
|
|
16
|
+
*/
|
|
17
|
+
class Validate {
|
|
18
|
+
errorsArray;
|
|
19
|
+
parentObject;
|
|
20
|
+
constructor(parentObject, errorsArray) {
|
|
21
|
+
this.errorsArray = errorsArray;
|
|
22
|
+
this.parentObject = parentObject;
|
|
23
|
+
}
|
|
24
|
+
optionalString(fieldName) {
|
|
25
|
+
if (this.fieldPathIsValid(fieldName) &&
|
|
26
|
+
!(0, commonTypeGuards_1.isOptionalString)(this.getField(fieldName))) {
|
|
27
|
+
this.errorsArray.push(`${fieldName} must be a string or undefined`);
|
|
28
|
+
this.deleteField(fieldName);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
optionalNumber(fieldName) {
|
|
32
|
+
if (this.fieldPathIsValid(fieldName) &&
|
|
33
|
+
!(0, commonTypeGuards_1.isOptionalNumber)(this.getField(fieldName))) {
|
|
34
|
+
this.errorsArray.push(`${fieldName} must be a number or undefined`);
|
|
35
|
+
this.deleteField(fieldName);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
optionalBoolean(fieldName) {
|
|
39
|
+
if (this.fieldPathIsValid(fieldName) &&
|
|
40
|
+
!(0, commonTypeGuards_1.isOptionalBoolean)(this.getField(fieldName))) {
|
|
41
|
+
this.errorsArray.push(`${fieldName} must be a boolean or undefined`);
|
|
42
|
+
this.deleteField(fieldName);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
optionalArray(fieldName) {
|
|
46
|
+
if (this.fieldPathIsValid(fieldName) &&
|
|
47
|
+
!(0, commonTypeGuards_1.isOptionalArray)(this.getField(fieldName))) {
|
|
48
|
+
this.errorsArray.push(`${fieldName} must be an array or undefined`);
|
|
49
|
+
this.deleteField(fieldName);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
string(fieldName, defaultValue) {
|
|
53
|
+
if (this.fieldPathIsValid(fieldName) &&
|
|
54
|
+
typeof this.getField(fieldName) !== 'string') {
|
|
55
|
+
this.errorsArray.push(`${fieldName} must be a string`);
|
|
56
|
+
this.updateField(fieldName, defaultValue);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
object(fieldName, defaultValue) {
|
|
60
|
+
if (this.fieldPathIsValid(fieldName) &&
|
|
61
|
+
typeof this.getField(fieldName) !== 'object') {
|
|
62
|
+
this.errorsArray.push(`${fieldName} must be an object`);
|
|
63
|
+
this.updateField(fieldName, defaultValue);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
number(fieldName, defaultValue) {
|
|
67
|
+
if (this.fieldPathIsValid(fieldName) &&
|
|
68
|
+
typeof this.getField(fieldName) !== 'number') {
|
|
69
|
+
this.errorsArray.push(`${fieldName} must be a number`);
|
|
70
|
+
this.updateField(fieldName, defaultValue);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
boolean(fieldName, defaultValue) {
|
|
74
|
+
if (this.fieldPathIsValid(fieldName) &&
|
|
75
|
+
typeof this.getField(fieldName) !== 'boolean') {
|
|
76
|
+
this.errorsArray.push(`${fieldName} must be a boolean`);
|
|
77
|
+
this.updateField(fieldName, defaultValue);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Checks if the provided field path is valid. In other words, if the
|
|
82
|
+
* field path is of length 1, it always returns true. If the field path
|
|
83
|
+
* is any longer than 1, it will check to make sure the final field is
|
|
84
|
+
* defined in the parent object.
|
|
85
|
+
*
|
|
86
|
+
* The idea is that if the field path is invalid, then this class will
|
|
87
|
+
* not report an error, because it is assumed that the parent of the field
|
|
88
|
+
* path will report the error.
|
|
89
|
+
*/
|
|
90
|
+
fieldPathIsValid(fieldName) {
|
|
91
|
+
const fieldPath = fieldName.split('.');
|
|
92
|
+
if (fieldPath.length === 1) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
let currentObject = this.parentObject;
|
|
96
|
+
for (let i = 0; i < fieldPath.length - 1; i += 1) {
|
|
97
|
+
if (currentObject[fieldPath[i]] === undefined) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
currentObject = currentObject[fieldPath[i]];
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
updateField(fieldName, defaultValue) {
|
|
105
|
+
const fieldPath = fieldName.split('.');
|
|
106
|
+
let currentObject = this.parentObject;
|
|
107
|
+
fieldPath.forEach((field, index) => {
|
|
108
|
+
if (index === fieldPath.length - 1) {
|
|
109
|
+
currentObject[field] = defaultValue;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
currentObject = currentObject[field];
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
getField(fieldName) {
|
|
117
|
+
const fieldPath = fieldName.split('.');
|
|
118
|
+
let currentObject = this.parentObject;
|
|
119
|
+
fieldPath.forEach((field) => {
|
|
120
|
+
currentObject = currentObject[field];
|
|
121
|
+
});
|
|
122
|
+
return currentObject;
|
|
123
|
+
}
|
|
124
|
+
deleteField(fieldName) {
|
|
125
|
+
const fieldPath = fieldName.split('.');
|
|
126
|
+
let currentObject = this.parentObject;
|
|
127
|
+
fieldPath.forEach((field, index) => {
|
|
128
|
+
if (index === fieldPath.length - 1) {
|
|
129
|
+
delete currentObject[field];
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
currentObject = currentObject[field];
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.default = Validate;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidateUtil.spec.d.ts","sourceRoot":"","sources":["../../../src/schemas/validators/ValidateUtil.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const ValidateUtil_1 = __importDefault(require("./ValidateUtil"));
|
|
7
|
+
describe('Validate', () => {
|
|
8
|
+
let errorsArray;
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
let parentObject;
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
errorsArray = [];
|
|
13
|
+
parentObject = {};
|
|
14
|
+
});
|
|
15
|
+
it('should validate optional string', () => {
|
|
16
|
+
const validate = new ValidateUtil_1.default(parentObject, errorsArray);
|
|
17
|
+
parentObject.testField = 123;
|
|
18
|
+
validate.optionalString('testField');
|
|
19
|
+
expect(errorsArray).toContain('testField must be a string or undefined');
|
|
20
|
+
errorsArray = [];
|
|
21
|
+
parentObject.testField = 'test';
|
|
22
|
+
validate.optionalString('testField');
|
|
23
|
+
expect(errorsArray.length).toBe(0);
|
|
24
|
+
});
|
|
25
|
+
it('should validate optional number', () => {
|
|
26
|
+
const validate = new ValidateUtil_1.default(parentObject, errorsArray);
|
|
27
|
+
parentObject.testField = true;
|
|
28
|
+
validate.optionalNumber('testField');
|
|
29
|
+
expect(errorsArray).toContain('testField must be a number or undefined');
|
|
30
|
+
errorsArray = [];
|
|
31
|
+
parentObject.testField = 123;
|
|
32
|
+
validate.optionalNumber('testField');
|
|
33
|
+
expect(errorsArray.length).toBe(0);
|
|
34
|
+
});
|
|
35
|
+
it('should validate optional boolean', () => {
|
|
36
|
+
const validate = new ValidateUtil_1.default(parentObject, errorsArray);
|
|
37
|
+
parentObject.testField = 123;
|
|
38
|
+
validate.optionalBoolean('testField');
|
|
39
|
+
expect(errorsArray).toContain('testField must be a boolean or undefined');
|
|
40
|
+
errorsArray = [];
|
|
41
|
+
parentObject.testField = true;
|
|
42
|
+
validate.optionalBoolean('testField');
|
|
43
|
+
expect(errorsArray.length).toBe(0);
|
|
44
|
+
});
|
|
45
|
+
it('should validate optional array', () => {
|
|
46
|
+
const validate = new ValidateUtil_1.default(parentObject, errorsArray);
|
|
47
|
+
parentObject.testField = 123;
|
|
48
|
+
validate.optionalArray('testField');
|
|
49
|
+
expect(errorsArray).toContain('testField must be an array or undefined');
|
|
50
|
+
errorsArray = [];
|
|
51
|
+
parentObject.testField = [];
|
|
52
|
+
validate.optionalArray('testField');
|
|
53
|
+
expect(errorsArray.length).toBe(0);
|
|
54
|
+
});
|
|
55
|
+
});
|