@reldens/utils 0.30.0 → 0.31.0
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/index.js +1 -0
- package/lib/schema-validator.js +116 -0
- package/lib/shortcuts.js +26 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -11,6 +11,7 @@ module.exports = {
|
|
|
11
11
|
EventsManagerSingleton: new EventsManager(),
|
|
12
12
|
ErrorManager: require('./lib/error-manager'),
|
|
13
13
|
InteractionArea: require('./lib/interaction-area'),
|
|
14
|
+
SchemaValidator: require('./lib/schema-validator'),
|
|
14
15
|
Logger: require('./lib/logger'),
|
|
15
16
|
sc: require('./lib/shortcuts')
|
|
16
17
|
};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Reldens - SchemaValidator
|
|
4
|
+
*
|
|
5
|
+
* This module is to validate objects properties at runtime.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const sc = require('./shortcuts');
|
|
10
|
+
const Logger = require('./logger');
|
|
11
|
+
|
|
12
|
+
class SchemaValidator
|
|
13
|
+
{
|
|
14
|
+
|
|
15
|
+
constructor(schema)
|
|
16
|
+
{
|
|
17
|
+
this.schema = schema;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Sample schema:
|
|
22
|
+
* {
|
|
23
|
+
* prop: {type: 'string', min: 1},
|
|
24
|
+
* prop2: {type: 'object', nested: {
|
|
25
|
+
* anotherProp: {type: 'int', float: 1}
|
|
26
|
+
* }}
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* @param obj
|
|
30
|
+
* @param schema
|
|
31
|
+
* @returns {boolean}
|
|
32
|
+
*/
|
|
33
|
+
validate(obj, schema = false)
|
|
34
|
+
{
|
|
35
|
+
if(!schema){
|
|
36
|
+
schema = this.schema;
|
|
37
|
+
}
|
|
38
|
+
if (!sc.isObject(schema)){
|
|
39
|
+
Logger.debug('No schema provided.');
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
for(let i of Object.keys(schema)){
|
|
43
|
+
let validate = schema[i];
|
|
44
|
+
if(!this.isValidSchema(obj[i], validate, i)){
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if(validate.nested){
|
|
48
|
+
if(!this.validate(obj[i], validate.nested)){
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
isValidSchema(obj, schema, objectKey)
|
|
57
|
+
{
|
|
58
|
+
if(!schema ||!schema.type){
|
|
59
|
+
Logger.debug('No schema type provided.', schema, obj, objectKey);
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
switch(schema.type){
|
|
63
|
+
case 'string':
|
|
64
|
+
if(!sc.isString(obj)){
|
|
65
|
+
Logger.debug('Object is not a string.', objectKey, obj, schema);
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case 'int':
|
|
70
|
+
if(!sc.isInt(obj)){
|
|
71
|
+
Logger.debug('Object is not an integer.', objectKey, obj, schema);
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
case 'float':
|
|
76
|
+
if(!sc.isFloat(obj)){
|
|
77
|
+
Logger.debug('Object is not a float.', objectKey, obj, schema);
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
case 'boolean':
|
|
82
|
+
if(!sc.isBoolean(obj)){
|
|
83
|
+
Logger.debug('Object is not a boolean.', objectKey, obj, schema);
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
break;
|
|
87
|
+
case 'object':
|
|
88
|
+
if(!sc.isObject(obj)){
|
|
89
|
+
Logger.debug('Object is not an object.', objectKey, obj, schema);
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
case 'array':
|
|
94
|
+
if(!sc.isArray(obj)){
|
|
95
|
+
Logger.debug('Object is not an array.', objectKey, obj, schema);
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
if(schema.valuesType){
|
|
99
|
+
for(let i of obj){
|
|
100
|
+
if(!this.isValidSchema(i, {type: schema.valuesType}, objectKey)){
|
|
101
|
+
Logger.debug('Invalid array value.', i, schema);
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
default:
|
|
108
|
+
Logger.debug('Invalid schema type provided.', schema.type);
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
module.exports.SchemaValidator = SchemaValidator;
|
package/lib/shortcuts.js
CHANGED
|
@@ -57,6 +57,32 @@ class Shortcuts
|
|
|
57
57
|
return this.isObject(obj) && property && 'function' === typeof obj[property];
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
isString(value)
|
|
61
|
+
{
|
|
62
|
+
return 'string' === typeof value;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
isInt(value)
|
|
66
|
+
{
|
|
67
|
+
if('number' !== typeof value){
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return Number.isInteger(value);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
isFloat(value)
|
|
74
|
+
{
|
|
75
|
+
if('number' !== typeof value){
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return Number(value) === value && 0 !== value % 1;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
isBoolean(value)
|
|
82
|
+
{
|
|
83
|
+
return 'boolean' === typeof value;
|
|
84
|
+
}
|
|
85
|
+
|
|
60
86
|
deepMergeProperties(target, source)
|
|
61
87
|
{
|
|
62
88
|
if(!this.isObject(target) || !this.isObject(source)){
|