not-node 4.0.15 → 4.0.16
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/package.json +3 -2
- package/src/fields/index.js +2 -1
- package/src/form/form.js +19 -7
- package/src/model/enrich.js +3 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "not-node",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.16",
|
|
4
4
|
"description": "node complimentary part for client side notFramework.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -63,6 +63,7 @@
|
|
|
63
63
|
"not-path": "*",
|
|
64
64
|
"rate-limiter-flexible": "^2.3.6",
|
|
65
65
|
"redis": "^4.0.0",
|
|
66
|
+
"rfdc": "^1.3.0",
|
|
66
67
|
"rmdir": "^1.2.0",
|
|
67
68
|
"serve-static": "*",
|
|
68
69
|
"simple-git": "*",
|
|
@@ -73,7 +74,7 @@
|
|
|
73
74
|
"babel-eslint": "^10.1.0",
|
|
74
75
|
"chai": "*",
|
|
75
76
|
"chai-as-promised": "*",
|
|
76
|
-
"eslint": "^
|
|
77
|
+
"eslint": "^7.0.0",
|
|
77
78
|
"eslint-plugin-node": "^11.1.0",
|
|
78
79
|
"eslint-plugin-promise": "^5.2.0",
|
|
79
80
|
"eslint-plugin-sonarjs": "^0.11.0",
|
package/src/fields/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const clone = require('rfdc')();
|
|
1
2
|
const fs = require('fs');
|
|
2
3
|
const path = require('path');
|
|
3
4
|
const {objHas, tryFile} = require('../common');
|
|
@@ -82,7 +83,7 @@ module.exports.initField = (field, resultOnly = true, type = 'ui') => {
|
|
|
82
83
|
destName = srcName = field;
|
|
83
84
|
}
|
|
84
85
|
let proto = (objHas(FIELDS, srcName) && objHas(FIELDS[srcName], type)) ? FIELDS[srcName][type]:{};
|
|
85
|
-
let result = Object.assign({}, proto, mutation);
|
|
86
|
+
let result = Object.assign({}, clone(proto), mutation);
|
|
86
87
|
if (resultOnly) {
|
|
87
88
|
return result;
|
|
88
89
|
} else {
|
package/src/form/form.js
CHANGED
|
@@ -25,15 +25,20 @@ class Form {
|
|
|
25
25
|
FORM_NAME
|
|
26
26
|
}) {
|
|
27
27
|
this.FORM_NAME = FORM_NAME;
|
|
28
|
-
this.
|
|
29
|
-
this.SCHEMA = byFieldsValidators(initFields(FIELDS, 'model'));
|
|
28
|
+
this.PROTO_FIELDS = FIELDS;
|
|
30
29
|
if (mongoose.modelNames().indexOf(FORM_NAME)===-1){
|
|
30
|
+
this.SCHEMA = byFieldsValidators(initFields(this.PROTO_FIELDS, 'model'));
|
|
31
31
|
this.MODEL = mongoose.model(FORM_NAME, Schema(this.SCHEMA));
|
|
32
32
|
}else{
|
|
33
33
|
this.MODEL = mongoose.connection.model(FORM_NAME);
|
|
34
|
+
this.SCHEMA = this.MODEL.schema;
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
getFields(){
|
|
39
|
+
return Object.keys(this.SCHEMA);
|
|
40
|
+
}
|
|
41
|
+
|
|
37
42
|
/**
|
|
38
43
|
* Extract data from ExpressRequest object and validates it
|
|
39
44
|
* returns it or throws
|
|
@@ -43,7 +48,7 @@ class Form {
|
|
|
43
48
|
**/
|
|
44
49
|
async run(req) {
|
|
45
50
|
let data = await this.extract(req);
|
|
46
|
-
await this.
|
|
51
|
+
await this._validate(data);
|
|
47
52
|
return data;
|
|
48
53
|
}
|
|
49
54
|
|
|
@@ -62,9 +67,9 @@ class Form {
|
|
|
62
67
|
* @return {Object}
|
|
63
68
|
* @throws {notValidationError}
|
|
64
69
|
**/
|
|
65
|
-
async
|
|
70
|
+
async _validate(data) {
|
|
66
71
|
try {
|
|
67
|
-
await this.
|
|
72
|
+
await this.validate(data);
|
|
68
73
|
} catch (e) {
|
|
69
74
|
let fields = {};
|
|
70
75
|
if (e instanceof mongoose.Error.ValidationError) {
|
|
@@ -72,11 +77,14 @@ class Form {
|
|
|
72
77
|
fields[name] = [e.errors[name].message];
|
|
73
78
|
});
|
|
74
79
|
throw new notValidationError(e.message, fields, e, data);
|
|
75
|
-
} else {
|
|
80
|
+
} else if (e instanceof notValidationError){
|
|
81
|
+
throw e;
|
|
82
|
+
}else {
|
|
76
83
|
throw new notError(
|
|
77
84
|
'core:form_validation_error', {
|
|
78
85
|
FORM_NAME: this.FORM_NAME,
|
|
79
|
-
|
|
86
|
+
PROTO_FIELDS: this.PROTO_FIELDS,
|
|
87
|
+
FORM_FIELDS: this.getFields(),
|
|
80
88
|
data
|
|
81
89
|
},
|
|
82
90
|
e
|
|
@@ -85,6 +93,10 @@ class Form {
|
|
|
85
93
|
}
|
|
86
94
|
}
|
|
87
95
|
|
|
96
|
+
async validate(data){
|
|
97
|
+
await this.MODEL.validate(data, this.getFields());
|
|
98
|
+
}
|
|
99
|
+
|
|
88
100
|
static fabric(){
|
|
89
101
|
return FormFabric;
|
|
90
102
|
}
|
package/src/model/enrich.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** @module Model/Enrich */
|
|
2
2
|
|
|
3
3
|
const Schema = require('mongoose').Schema,
|
|
4
|
-
firstLetterToLower = require('../common')
|
|
4
|
+
{firstLetterToLower,isFunc} = require('../common'),
|
|
5
5
|
buildValidator = require('./buildValidator');
|
|
6
6
|
|
|
7
7
|
class ModelEnricher{
|
|
@@ -49,7 +49,7 @@ class ModelEnricher{
|
|
|
49
49
|
mongooseSchema.statics.__incModel = modelName;
|
|
50
50
|
if(options && options.filter){
|
|
51
51
|
mongooseSchema.statics.__incFilter = options.filter;
|
|
52
|
-
}
|
|
52
|
+
}
|
|
53
53
|
return mongooseSchema;
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -61,7 +61,7 @@ class ModelEnricher{
|
|
|
61
61
|
static byFieldsValidators (mongooseSchema) {
|
|
62
62
|
if (mongooseSchema) {
|
|
63
63
|
for (let fieldName in mongooseSchema) {
|
|
64
|
-
if (Object.prototype.hasOwnProperty.call(mongooseSchema[fieldName], 'validate')) {
|
|
64
|
+
if (Object.prototype.hasOwnProperty.call(mongooseSchema[fieldName], 'validate') && mongooseSchema[fieldName].validate.length && !isFunc(mongooseSchema[fieldName].validate[0])) {
|
|
65
65
|
mongooseSchema[fieldName].validate = buildValidator(mongooseSchema[fieldName].validate);
|
|
66
66
|
}
|
|
67
67
|
}
|