not-node 4.0.15 → 4.0.19
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/domain.js +2 -2
- package/src/fields/index.js +2 -1
- package/src/form/form.js +23 -9
- package/src/manifest/module.js +10 -9
- package/src/manifest/registrator/fields.js +21 -6
- package/src/model/buildValidator.js +42 -5
- package/src/model/enrich.js +5 -5
- package/src/model/proto.js +3 -3
- package/test/notDomain.js +4 -4
- package/test/notModule.js +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "not-node",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.19",
|
|
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/domain.js
CHANGED
|
@@ -100,7 +100,7 @@ class notDomain extends EventEmitter {
|
|
|
100
100
|
notApp: this,
|
|
101
101
|
fields: this.options.fields
|
|
102
102
|
});
|
|
103
|
-
this.importModule(mod, moduleName || mod.
|
|
103
|
+
this.importModule(mod, moduleName || mod.getName());
|
|
104
104
|
return this;
|
|
105
105
|
}
|
|
106
106
|
|
|
@@ -235,7 +235,7 @@ class notDomain extends EventEmitter {
|
|
|
235
235
|
return this.modules[moduleName];
|
|
236
236
|
} else {
|
|
237
237
|
for (let t in this.modules) {
|
|
238
|
-
if (this.modules[t].
|
|
238
|
+
if (this.modules[t].getName() === moduleName) {
|
|
239
239
|
return this.modules[t];
|
|
240
240
|
}
|
|
241
241
|
}
|
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,12 +77,17 @@ 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
|
-
'core:form_validation_error',
|
|
84
|
+
'core:form_validation_error',
|
|
85
|
+
{
|
|
78
86
|
FORM_NAME: this.FORM_NAME,
|
|
79
|
-
|
|
80
|
-
|
|
87
|
+
PROTO_FIELDS: this.PROTO_FIELDS,
|
|
88
|
+
FORM_FIELDS: this.getFields(),
|
|
89
|
+
data,
|
|
90
|
+
message: e.message
|
|
81
91
|
},
|
|
82
92
|
e
|
|
83
93
|
);
|
|
@@ -85,6 +95,10 @@ class Form {
|
|
|
85
95
|
}
|
|
86
96
|
}
|
|
87
97
|
|
|
98
|
+
async validate(data){
|
|
99
|
+
await this.MODEL.validate(data, this.getFields());
|
|
100
|
+
}
|
|
101
|
+
|
|
88
102
|
static fabric(){
|
|
89
103
|
return FormFabric;
|
|
90
104
|
}
|
package/src/manifest/module.js
CHANGED
|
@@ -56,17 +56,11 @@ class notModule {
|
|
|
56
56
|
};
|
|
57
57
|
this.fieldsImportRules = (objHas(options, 'fields') && options.fields) ? options.fields : {};
|
|
58
58
|
|
|
59
|
-
log.info(`Creating module: ${this.
|
|
59
|
+
log.info(`Creating module: ${this.getName()}`);
|
|
60
60
|
this.init();
|
|
61
61
|
return this;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
getModuleName() {
|
|
65
|
-
if(this.module && this.module.name){
|
|
66
|
-
return this.module.name;
|
|
67
|
-
}
|
|
68
|
-
return this.path;
|
|
69
|
-
}
|
|
70
64
|
|
|
71
65
|
init() {
|
|
72
66
|
if (this.path) {
|
|
@@ -174,7 +168,7 @@ class notModule {
|
|
|
174
168
|
}
|
|
175
169
|
|
|
176
170
|
fabricateModel(model) {
|
|
177
|
-
protoModel.fabricate(model,
|
|
171
|
+
protoModel.fabricate(model, this.getOptions(), this.mongoose);
|
|
178
172
|
}
|
|
179
173
|
|
|
180
174
|
fabricateModels() {
|
|
@@ -318,7 +312,14 @@ class notModule {
|
|
|
318
312
|
}
|
|
319
313
|
|
|
320
314
|
getName(){
|
|
321
|
-
|
|
315
|
+
if(this.module && this.module.name){
|
|
316
|
+
return this.module.name;
|
|
317
|
+
}
|
|
318
|
+
return this.path;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
getOptions(){
|
|
322
|
+
return (this.module && this.module.options)?this.module.options:{};
|
|
322
323
|
}
|
|
323
324
|
|
|
324
325
|
setRouteWS({
|
|
@@ -36,14 +36,30 @@ module.exports = class notModuleRegistratorFields{
|
|
|
36
36
|
);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
registerField({name, field, fieldsImportRules}){
|
|
39
|
+
registerField({name, field, fieldsImportRules, fromPath}){
|
|
40
|
+
this.extendByFrontValidators({name, field, fromPath});
|
|
40
41
|
notModuleRegistratorFields.fieldsManager.registerField(
|
|
41
|
-
name,
|
|
42
|
-
field,
|
|
42
|
+
name, //field name
|
|
43
|
+
field, //field description
|
|
43
44
|
fieldsImportRules //global import rules
|
|
44
45
|
);
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
**/
|
|
51
|
+
extendByFrontValidators({name, field, fromPath}){
|
|
52
|
+
if(!(field && objHas(field, 'model'))){ return; }
|
|
53
|
+
//load validators
|
|
54
|
+
const validatorName = path.join(fromPath, 'validators', name + '.js');
|
|
55
|
+
if(!tryFile(validatorName)){return;}
|
|
56
|
+
const validators = notModuleRegistratorFields.openFile(validatorName);
|
|
57
|
+
//inject into field.model
|
|
58
|
+
if(!objHas(field.model, 'validate')){
|
|
59
|
+
field.model.validate = [];
|
|
60
|
+
}
|
|
61
|
+
field.model.validate.push(...validators);
|
|
62
|
+
}
|
|
47
63
|
|
|
48
64
|
|
|
49
65
|
/**
|
|
@@ -72,11 +88,10 @@ module.exports = class notModuleRegistratorFields{
|
|
|
72
88
|
this.registerField({
|
|
73
89
|
name: parts.name, //fields name
|
|
74
90
|
field: fields, //field description
|
|
75
|
-
fieldsImportRules: nModule.fieldsImportRules //global import rules
|
|
91
|
+
fieldsImportRules: nModule.fieldsImportRules, //global import rules
|
|
92
|
+
fromPath
|
|
76
93
|
});
|
|
77
94
|
}
|
|
78
95
|
}
|
|
79
96
|
|
|
80
|
-
|
|
81
|
-
|
|
82
97
|
};
|
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
/** @module Model/Validator */
|
|
2
2
|
const validate = require('mongoose-validator');
|
|
3
|
+
const {objHas, executeObjectFunction, isFunc} = require('../common');
|
|
4
|
+
|
|
5
|
+
function extractValidationEnvGetter(options){
|
|
6
|
+
if(options && objHas(options, 'getValidationEnv') && isFunc(options.getValidationEnv)){
|
|
7
|
+
return options.getValidationEnv;
|
|
8
|
+
}else{
|
|
9
|
+
//should return at least empty object
|
|
10
|
+
return ()=>{return {};};
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function extendObsolete(rule){
|
|
15
|
+
const result = {...rule};
|
|
16
|
+
if(objHas(result, 'arguments') && !Array.isArray(result.arguments)){
|
|
17
|
+
result.arguments = Object.values(result.arguments);
|
|
18
|
+
}
|
|
19
|
+
return validate(result);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function extendModern(rule, options){
|
|
23
|
+
const result = {...rule};
|
|
24
|
+
delete result.validator;
|
|
25
|
+
const validationEnv = extractValidationEnvGetter(options)();
|
|
26
|
+
result.validator = async (val) => {
|
|
27
|
+
return await executeObjectFunction(rule, 'validator', [val, validationEnv]);
|
|
28
|
+
};
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
function extendValidation(rule, options){
|
|
34
|
+
if(typeof rule.validator === 'string'){
|
|
35
|
+
//will extend from text description to validatejs lib validation function
|
|
36
|
+
return extendObsolete(rule);
|
|
37
|
+
}else{
|
|
38
|
+
//more complex validation
|
|
39
|
+
return extendModern(rule, options);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
3
42
|
|
|
4
43
|
/**
|
|
5
44
|
* Take array of validator (https://www.npmjs.com/package/validator) rules
|
|
@@ -7,10 +46,8 @@ const validate = require('mongoose-validator');
|
|
|
7
46
|
* then return it
|
|
8
47
|
**/
|
|
9
48
|
|
|
10
|
-
module.exports = function(validators) {
|
|
11
|
-
|
|
12
|
-
result = validators.map(
|
|
13
|
-
return validate(item);
|
|
14
|
-
});
|
|
49
|
+
module.exports = function(validators, options) {
|
|
50
|
+
let result = null;
|
|
51
|
+
result = validators.map(rule => extendValidation(rule, options));
|
|
15
52
|
return result;
|
|
16
53
|
};
|
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
|
|
|
@@ -58,11 +58,11 @@ class ModelEnricher{
|
|
|
58
58
|
return mongooseSchema;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
static byFieldsValidators (mongooseSchema) {
|
|
61
|
+
static byFieldsValidators (mongooseSchema, options) {
|
|
62
62
|
if (mongooseSchema) {
|
|
63
63
|
for (let fieldName in mongooseSchema) {
|
|
64
|
-
if (Object.prototype.hasOwnProperty.call(mongooseSchema[fieldName], 'validate')) {
|
|
65
|
-
mongooseSchema[fieldName].validate = buildValidator(mongooseSchema[fieldName].validate);
|
|
64
|
+
if (Object.prototype.hasOwnProperty.call(mongooseSchema[fieldName], 'validate') && mongooseSchema[fieldName].validate.length && !isFunc(mongooseSchema[fieldName].validate[0])) {
|
|
65
|
+
mongooseSchema[fieldName].validate = buildValidator(mongooseSchema[fieldName].validate, options);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
}
|
package/src/model/proto.js
CHANGED
|
@@ -59,10 +59,10 @@ module.exports = class ModelFabricate{
|
|
|
59
59
|
this.extendSchemaFrom(targetModule.thisPost, schema.post.bind(schema));
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
static enrichByFields(targetModule){
|
|
62
|
+
static enrichByFields(targetModule, options){
|
|
63
63
|
if (targetModule.enrich) {
|
|
64
64
|
if (targetModule.enrich.validators) {
|
|
65
|
-
targetModule.thisSchema = enrich.byFieldsValidators(targetModule.thisSchema,
|
|
65
|
+
targetModule.thisSchema = enrich.byFieldsValidators(targetModule.thisSchema, options);
|
|
66
66
|
}
|
|
67
67
|
if (targetModule.enrich.versioning) {
|
|
68
68
|
targetModule.thisSchema = enrich.byFieldsForVersioning(targetModule.thisSchema, targetModule.thisModelName);
|
|
@@ -125,7 +125,7 @@ module.exports = class ModelFabricate{
|
|
|
125
125
|
if (ModelFabricate.isNotExtendable(targetModule)) {
|
|
126
126
|
return new Schema(targetModule.thisSchema, options.schemaOptions);
|
|
127
127
|
} else {
|
|
128
|
-
ModelFabricate.enrichByFields(targetModule);
|
|
128
|
+
ModelFabricate.enrichByFields(targetModule, options);
|
|
129
129
|
//collecting information of unique fields, removing unique flag from schema
|
|
130
130
|
let fieldsForIndexes = ModelFabricate.collectFieldsForIndexes(targetModule);
|
|
131
131
|
//creating schema for model
|
package/test/notDomain.js
CHANGED
|
@@ -390,12 +390,12 @@ describe('notDomain', function() {
|
|
|
390
390
|
it('exists, but with custom name', function() {
|
|
391
391
|
const route = 'Jungle';
|
|
392
392
|
const targetMod = {
|
|
393
|
-
|
|
393
|
+
getName(){ return 'Jungle'; }
|
|
394
394
|
};
|
|
395
395
|
const ctx = {
|
|
396
396
|
modules:{
|
|
397
397
|
loop:{
|
|
398
|
-
|
|
398
|
+
getName(){return 'loop'; }
|
|
399
399
|
},
|
|
400
400
|
trees: targetMod
|
|
401
401
|
}
|
|
@@ -408,12 +408,12 @@ describe('notDomain', function() {
|
|
|
408
408
|
it('not exists', function() {
|
|
409
409
|
const route = 'Jungle';
|
|
410
410
|
const targetMod = {
|
|
411
|
-
|
|
411
|
+
getName(){ return 'Jungle1'; }
|
|
412
412
|
};
|
|
413
413
|
const ctx = {
|
|
414
414
|
modules:{
|
|
415
415
|
loop:{
|
|
416
|
-
|
|
416
|
+
getName(){return 'loop'; }
|
|
417
417
|
},
|
|
418
418
|
trees: targetMod
|
|
419
419
|
}
|
package/test/notModule.js
CHANGED
|
@@ -144,14 +144,14 @@ describe('notModule', function() {
|
|
|
144
144
|
name: 'fake my name'
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
|
-
expect(notModule.prototype.
|
|
147
|
+
expect(notModule.prototype.getName.call(ctx)).to.be.equal('fake my name');
|
|
148
148
|
});
|
|
149
149
|
|
|
150
150
|
it('no module.name, no path', function() {
|
|
151
151
|
const ctx = {
|
|
152
152
|
module: {}
|
|
153
153
|
}
|
|
154
|
-
expect(notModule.prototype.
|
|
154
|
+
expect(notModule.prototype.getName.call(ctx)).to.be.undefined;
|
|
155
155
|
});
|
|
156
156
|
});
|
|
157
157
|
|