not-node 4.0.16 → 4.0.17

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "4.0.16",
3
+ "version": "4.0.17",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
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.getModuleName());
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].getModuleName() === moduleName) {
238
+ if (this.modules[t].getName() === moduleName) {
239
239
  return this.modules[t];
240
240
  }
241
241
  }
package/src/form/form.js CHANGED
@@ -81,11 +81,13 @@ class Form {
81
81
  throw e;
82
82
  }else {
83
83
  throw new notError(
84
- 'core:form_validation_error', {
84
+ 'core:form_validation_error',
85
+ {
85
86
  FORM_NAME: this.FORM_NAME,
86
87
  PROTO_FIELDS: this.PROTO_FIELDS,
87
88
  FORM_FIELDS: this.getFields(),
88
- data
89
+ data,
90
+ message: e.message
89
91
  },
90
92
  e
91
93
  );
@@ -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.getModuleName()}`);
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, {}, this.mongoose);
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
- return this.module.name;
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({
@@ -1,5 +1,40 @@
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
+ return validate(rule);
16
+ }
17
+
18
+ function extendModern(rule, options){
19
+ const result = {...rule};
20
+ delete result.validator;
21
+ const validationEnv = extractValidationEnvGetter(options)();
22
+ result.validator = async (val) => {
23
+ return await executeObjectFunction(rule, 'validator', [val, validationEnv]);
24
+ };
25
+ return result;
26
+ }
27
+
28
+
29
+ function extendValidation(rule, options){
30
+ if(typeof rule.validator === 'string'){
31
+ //will extend from text description to validatejs lib validation function
32
+ return extendObsolete(rule);
33
+ }else{
34
+ //more complex validation
35
+ return extendModern(rule, options);
36
+ }
37
+ }
3
38
 
4
39
  /**
5
40
  * Take array of validator (https://www.npmjs.com/package/validator) rules
@@ -7,10 +42,8 @@ const validate = require('mongoose-validator');
7
42
  * then return it
8
43
  **/
9
44
 
10
- module.exports = function(validators) {
11
- var result = null;
12
- result = validators.map((item) => {
13
- return validate(item);
14
- });
45
+ module.exports = function(validators, options) {
46
+ let result = null;
47
+ result = validators.map(rule => extendValidation(rule, options));
15
48
  return result;
16
49
  };
@@ -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
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);
65
+ mongooseSchema[fieldName].validate = buildValidator(mongooseSchema[fieldName].validate, options);
66
66
  }
67
67
  }
68
68
  }
@@ -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, targetModule.thisModelName);
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
- getModuleName(){ return 'Jungle'; }
393
+ getName(){ return 'Jungle'; }
394
394
  };
395
395
  const ctx = {
396
396
  modules:{
397
397
  loop:{
398
- getModuleName(){return 'loop'; }
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
- getModuleName(){ return 'Jungle1'; }
411
+ getName(){ return 'Jungle1'; }
412
412
  };
413
413
  const ctx = {
414
414
  modules:{
415
415
  loop:{
416
- getModuleName(){return 'loop'; }
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.getModuleName.call(ctx)).to.be.equal('fake my name');
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.getModuleName.call(ctx)).to.be.undefined;
154
+ expect(notModule.prototype.getName.call(ctx)).to.be.undefined;
155
155
  });
156
156
  });
157
157