@golemio/validator 0.3.1 → 0.3.2-dev.833945190
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/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -2
- package/dist/ObjectKeysValidator.d.ts +0 -22
- package/dist/ObjectKeysValidator.js +0 -71
- package/dist/ObjectKeysValidator.js.map +0 -1
- package/dist/Validator.d.ts +0 -34
- package/dist/Validator.js +0 -83
- package/dist/Validator.js.map +0 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -12,6 +12,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
__exportStar(require("./IValidator"), exports);
|
|
14
14
|
__exportStar(require("./JSONSchemaValidator"), exports);
|
|
15
|
-
__exportStar(require("./Validator"), exports);
|
|
16
|
-
__exportStar(require("./ObjectKeysValidator"), exports);
|
|
17
15
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;;;;;;;AACb,+CAA6B;AAC7B,wDAAsC
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;;;;;;;AACb,+CAA6B;AAC7B,wDAAsC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@golemio/validator",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2-dev.833945190",
|
|
4
4
|
"description": "Library of Validator classes of the Golemio Data Platform System",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -49,7 +49,6 @@
|
|
|
49
49
|
"ajv": "^6.10.2",
|
|
50
50
|
"debug": "^4.1.1",
|
|
51
51
|
"dotenv": "^8.0.0",
|
|
52
|
-
"mongoose": "^6.5.3",
|
|
53
52
|
"pino": "^8.7.0"
|
|
54
53
|
},
|
|
55
54
|
"nyc": {
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { SchemaDefinition } from "mongoose";
|
|
2
|
-
import { IValidator, Validator } from "./";
|
|
3
|
-
/**
|
|
4
|
-
* Helper class for validation by mongoose model.
|
|
5
|
-
*/
|
|
6
|
-
export declare class ObjectKeysValidator extends Validator implements IValidator {
|
|
7
|
-
constructor(modelName: string, schemaObject: SchemaDefinition);
|
|
8
|
-
/**
|
|
9
|
-
* Data validation
|
|
10
|
-
*
|
|
11
|
-
* @param {object} data
|
|
12
|
-
* @returns {boolean} Returns true or throw error
|
|
13
|
-
*/
|
|
14
|
-
Validate(data: any): Promise<boolean>;
|
|
15
|
-
/**
|
|
16
|
-
* Helper method to validate one object key (which can be array or object)
|
|
17
|
-
*
|
|
18
|
-
* @param {any} data
|
|
19
|
-
* @returns {boolean} Returns true or throw error
|
|
20
|
-
*/
|
|
21
|
-
private ValidateObject;
|
|
22
|
-
}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.ObjectKeysValidator = void 0;
|
|
13
|
-
const _1 = require("./");
|
|
14
|
-
/**
|
|
15
|
-
* Helper class for validation by mongoose model.
|
|
16
|
-
*/
|
|
17
|
-
class ObjectKeysValidator extends _1.Validator {
|
|
18
|
-
constructor(modelName, schemaObject) {
|
|
19
|
-
super(modelName, schemaObject);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Data validation
|
|
23
|
-
*
|
|
24
|
-
* @param {object} data
|
|
25
|
-
* @returns {boolean} Returns true or throw error
|
|
26
|
-
*/
|
|
27
|
-
Validate(data) {
|
|
28
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
if (data instanceof Object) {
|
|
30
|
-
const promises = Object.keys(data).map((key) => {
|
|
31
|
-
const element = data[key];
|
|
32
|
-
return this.ValidateObject(element);
|
|
33
|
-
});
|
|
34
|
-
yield Promise.all(promises);
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
const error = "data must be object";
|
|
39
|
-
this.logger.debug(error);
|
|
40
|
-
throw new Error(error);
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Helper method to validate one object key (which can be array or object)
|
|
46
|
-
*
|
|
47
|
-
* @param {any} data
|
|
48
|
-
* @returns {boolean} Returns true or throw error
|
|
49
|
-
*/
|
|
50
|
-
ValidateObject(data) {
|
|
51
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
if (data instanceof Array) {
|
|
53
|
-
if (data.length === 0) {
|
|
54
|
-
return true;
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
const promises = data.map((element) => {
|
|
58
|
-
return this.ValidateElement(element);
|
|
59
|
-
});
|
|
60
|
-
yield Promise.all(promises);
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
return yield this.ValidateElement(data);
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
exports.ObjectKeysValidator = ObjectKeysValidator;
|
|
71
|
-
//# sourceMappingURL=ObjectKeysValidator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ObjectKeysValidator.js","sourceRoot":"","sources":["../src/ObjectKeysValidator.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;;;;;;;AAGb,yBAA2C;AAE3C;;GAEG;AACH,MAAa,mBAAoB,SAAQ,YAAS;IAC9C,YAAY,SAAiB,EAAE,YAA8B;QACzD,KAAK,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;IACnC,CAAC;IAED;;;;;OAKG;IACU,QAAQ,CAAC,IAAS;;YAC3B,IAAI,IAAI,YAAY,MAAM,EAAE;gBACxB,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;oBAC3C,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;oBAC1B,OAAO,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;gBACxC,CAAC,CAAC,CAAC;gBACH,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBAC5B,OAAO,IAAI,CAAC;aACf;iBAAM;gBACH,MAAM,KAAK,GAAG,qBAAqB,CAAC;gBACpC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBACzB,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;aAC1B;QACL,CAAC;KAAA;IAED;;;;;OAKG;IACW,cAAc,CAAC,IAAS;;YAClC,IAAI,IAAI,YAAY,KAAK,EAAE;gBACvB,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;oBACnB,OAAO,IAAI,CAAC;iBACf;qBAAM;oBACH,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE;wBAClC,OAAO,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;oBACzC,CAAC,CAAC,CAAC;oBACH,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;oBAC5B,OAAO,IAAI,CAAC;iBACf;aACJ;iBAAM;gBACH,OAAO,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;aAC3C;QACL,CAAC;KAAA;CACJ;AA/CD,kDA+CC"}
|
package/dist/Validator.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Model, SchemaDefinition } from "mongoose";
|
|
2
|
-
import { IValidator } from "./";
|
|
3
|
-
import { ILogger } from "./Logger";
|
|
4
|
-
/**
|
|
5
|
-
* Mongoose validator class. Validates data (object, array of objects, object with sub-objects)
|
|
6
|
-
* according to a given schema (Mongoose Schema Definition)
|
|
7
|
-
*/
|
|
8
|
-
export declare class Validator implements IValidator {
|
|
9
|
-
/** Name of the model */
|
|
10
|
-
protected modelName: string;
|
|
11
|
-
/** Reference to mongoose model object, used for validation */
|
|
12
|
-
protected mongooseModel: Model<any>;
|
|
13
|
-
protected logger: ILogger;
|
|
14
|
-
/**
|
|
15
|
-
* @param modelName Name of the model
|
|
16
|
-
* @param schemaObject Mongoose SchemaDefinition object by which to validate the input data
|
|
17
|
-
*/
|
|
18
|
-
constructor(modelName: string, schemaObject: SchemaDefinition);
|
|
19
|
-
setLogger(logger: any): void;
|
|
20
|
-
/**
|
|
21
|
-
* Data validation - validates either an array of objects or a single object
|
|
22
|
-
*
|
|
23
|
-
* @param {any} data Data to be validated by the validator's schema
|
|
24
|
-
* @returns {boolean} Returns true or throw error
|
|
25
|
-
*/
|
|
26
|
-
Validate(data: any): Promise<boolean>;
|
|
27
|
-
/**
|
|
28
|
-
* Method to validate a single object
|
|
29
|
-
*
|
|
30
|
-
* @param {object} data
|
|
31
|
-
* @returns {boolean} Returns true or throw error
|
|
32
|
-
*/
|
|
33
|
-
protected ValidateElement(data: object): Promise<boolean>;
|
|
34
|
-
}
|
package/dist/Validator.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Validator = void 0;
|
|
13
|
-
const mongoose_1 = require("mongoose");
|
|
14
|
-
const Logger_1 = require("./Logger");
|
|
15
|
-
/**
|
|
16
|
-
* Mongoose validator class. Validates data (object, array of objects, object with sub-objects)
|
|
17
|
-
* according to a given schema (Mongoose Schema Definition)
|
|
18
|
-
*/
|
|
19
|
-
class Validator {
|
|
20
|
-
/**
|
|
21
|
-
* @param modelName Name of the model
|
|
22
|
-
* @param schemaObject Mongoose SchemaDefinition object by which to validate the input data
|
|
23
|
-
*/
|
|
24
|
-
constructor(modelName, schemaObject) {
|
|
25
|
-
this.modelName = modelName;
|
|
26
|
-
try {
|
|
27
|
-
this.mongooseModel = (0, mongoose_1.model)(this.modelName);
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
this.mongooseModel = (0, mongoose_1.model)(this.modelName, new mongoose_1.Schema(schemaObject, { bufferCommands: false }));
|
|
31
|
-
}
|
|
32
|
-
this.logger = Logger_1.log;
|
|
33
|
-
}
|
|
34
|
-
setLogger(logger) {
|
|
35
|
-
this.logger = logger;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Data validation - validates either an array of objects or a single object
|
|
39
|
-
*
|
|
40
|
-
* @param {any} data Data to be validated by the validator's schema
|
|
41
|
-
* @returns {boolean} Returns true or throw error
|
|
42
|
-
*/
|
|
43
|
-
Validate(data) {
|
|
44
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
if (data instanceof Array) {
|
|
46
|
-
if (data.length === 0) {
|
|
47
|
-
return true;
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
const promises = data.map((element) => {
|
|
51
|
-
return this.ValidateElement(element);
|
|
52
|
-
});
|
|
53
|
-
yield Promise.all(promises);
|
|
54
|
-
return true;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
return yield this.ValidateElement(data);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Method to validate a single object
|
|
64
|
-
*
|
|
65
|
-
* @param {object} data
|
|
66
|
-
* @returns {boolean} Returns true or throw error
|
|
67
|
-
*/
|
|
68
|
-
ValidateElement(data) {
|
|
69
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
-
try {
|
|
71
|
-
const modelInstance = new this.mongooseModel(data);
|
|
72
|
-
yield modelInstance.validate();
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
this.logger.debug(error);
|
|
77
|
-
throw error;
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
exports.Validator = Validator;
|
|
83
|
-
//# sourceMappingURL=Validator.js.map
|
package/dist/Validator.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Validator.js","sourceRoot":"","sources":["../src/Validator.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;;;;;;;AAEb,uCAAkE;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