@reldens/utils 0.29.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 -1
- package/lib/schema-validator.js +116 -0
- package/lib/shortcuts.js +26 -0
- package/package.json +1 -1
- package/lib/file-handler.js +0 -88
package/index.js
CHANGED
|
@@ -11,7 +11,7 @@ module.exports = {
|
|
|
11
11
|
EventsManagerSingleton: new EventsManager(),
|
|
12
12
|
ErrorManager: require('./lib/error-manager'),
|
|
13
13
|
InteractionArea: require('./lib/interaction-area'),
|
|
14
|
-
|
|
14
|
+
SchemaValidator: require('./lib/schema-validator'),
|
|
15
15
|
Logger: require('./lib/logger'),
|
|
16
16
|
sc: require('./lib/shortcuts')
|
|
17
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)){
|
package/package.json
CHANGED
package/lib/file-handler.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* Reldens - FileHandler
|
|
4
|
-
*
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const fs = require('fs');
|
|
8
|
-
const path = require('path');
|
|
9
|
-
const Logger = require('./logger');
|
|
10
|
-
|
|
11
|
-
class FileHandler
|
|
12
|
-
{
|
|
13
|
-
|
|
14
|
-
constructor()
|
|
15
|
-
{
|
|
16
|
-
this.encoding = 'utf8';
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
joinPaths(...paths)
|
|
20
|
-
{
|
|
21
|
-
return path.join(...paths);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
copyFile(from, to, folder)
|
|
25
|
-
{
|
|
26
|
-
if(!this.exists(from)){
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
if(!this.exists(folder)){
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
fs.copyFileSync(from, path.join(folder, to));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
createFolder(folderPath)
|
|
36
|
-
{
|
|
37
|
-
fs.mkdirSync(folderPath, { recursive: true });
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
readFile(filePath)
|
|
41
|
-
{
|
|
42
|
-
return fs.readFileSync(filePath);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async writeFile(fileName, content)
|
|
46
|
-
{
|
|
47
|
-
return fs.writeFile(fileName, content, this.encoding, (err) => {
|
|
48
|
-
if(err){
|
|
49
|
-
Logger.error('Error saving the file:', err);
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
Logger.info('The file has been saved! New file name: '+fileName);
|
|
53
|
-
return true;
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
exists(fullPath)
|
|
58
|
-
{
|
|
59
|
-
if(!fs.existsSync(fullPath)){
|
|
60
|
-
Logger.info(`File or folder "${fullPath}" does not exist.`);
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
removeByPath(fullPath)
|
|
67
|
-
{
|
|
68
|
-
if(!this.exists(fullPath)){
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
let stats = fs.statSync(fullPath);
|
|
72
|
-
if(stats.isFile()){
|
|
73
|
-
fs.unlinkSync(fullPath);
|
|
74
|
-
Logger.info(`File "${fullPath}" has been removed.`);
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
77
|
-
if(stats.isDirectory()){
|
|
78
|
-
fs.rmdirSync(fullPath, { recursive: true }); // Remove folder recursively
|
|
79
|
-
Logger.info(`Folder "${fullPath}" has been removed.`);
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
Logger.warning(`"${fullPath}" is neither a file nor a folder.`);
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
module.exports = FileHandler;
|