not-node 4.0.16 → 4.0.20

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.20",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/common.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const fs = require('fs');
2
+ const path = require('path');
2
3
  const notPath = require('not-path');
3
4
 
4
5
  /** @module Common */
@@ -8,7 +9,7 @@ const notPath = require('not-path');
8
9
  * @return {string} result
9
10
  */
10
11
 
11
- module.exports.firstLetterToLower = function (string) {
12
+ module.exports.firstLetterToLower = function(string) {
12
13
  return string.charAt(0).toLowerCase() + string.slice(1);
13
14
  };
14
15
 
@@ -18,7 +19,7 @@ module.exports.firstLetterToLower = function (string) {
18
19
  * @return {string} result
19
20
  */
20
21
 
21
- module.exports.firstLetterToUpper = function (string) {
22
+ module.exports.firstLetterToUpper = function(string) {
22
23
  return string.charAt(0).toUpperCase() + string.slice(1);
23
24
  };
24
25
 
@@ -27,10 +28,10 @@ module.exports.firstLetterToUpper = function (string) {
27
28
  * @param {string} id ObjectId string to validate
28
29
  * @return {booelean} true if check is not failed
29
30
  */
30
- module.exports.validateObjectId = (id)=>{
31
- try{
32
- return id.match(/^[0-9a-fA-F]{24}$/)?true:false;
33
- }catch(e){
31
+ module.exports.validateObjectId = (id) => {
32
+ try {
33
+ return id.match(/^[0-9a-fA-F]{24}$/) ? true : false;
34
+ } catch (e) {
34
35
  return false;
35
36
  }
36
37
  };
@@ -41,24 +42,24 @@ module.exports.validateObjectId = (id)=>{
41
42
  * @param {string|ObjectId} secondId second id
42
43
  * @return {booelean} true if equal
43
44
  */
44
- module.exports.compareObjectIds = (firstId, secondId)=>{
45
- try{
46
- let a = firstId, b = secondId;
47
- if(typeof firstId !== 'string'){
45
+ module.exports.compareObjectIds = (firstId, secondId) => {
46
+ try {
47
+ let a = firstId,
48
+ b = secondId;
49
+ if (typeof firstId !== 'string') {
48
50
  a = a.toString();
49
51
  }
50
- if(typeof secondId !== 'string'){
52
+ if (typeof secondId !== 'string') {
51
53
  b = b.toString();
52
54
  }
53
- if(
54
- !module.exports.validateObjectId(a)
55
- ||
55
+ if (
56
+ !module.exports.validateObjectId(a) ||
56
57
  !module.exports.validateObjectId(b)
57
- ){
58
+ ) {
58
59
  return false;
59
60
  }
60
61
  return a === b;
61
- }catch(e){
62
+ } catch (e) {
62
63
  return false;
63
64
  }
64
65
  };
@@ -67,9 +68,9 @@ module.exports.compareObjectIds = (firstId, secondId)=>{
67
68
  * Returns today Date object without hours, minutes, seconds
68
69
  * @return {number} current date with 00:00:00 in ms of unix time
69
70
  */
70
- module.exports.getTodayDate = ()=>{
71
+ module.exports.getTodayDate = () => {
71
72
  let t = new Date();
72
- return (new Date(t.getFullYear(), t.getMonth(),t.getDate())).getTime();
73
+ return (new Date(t.getFullYear(), t.getMonth(), t.getDate())).getTime();
73
74
  };
74
75
 
75
76
 
@@ -84,22 +85,22 @@ module.exports.objHas = objHas;
84
85
 
85
86
 
86
87
  /**
87
- * Copies object to secure it from changes
88
- * @param {object} obj original object
89
- * @return {object} copy of object
90
- **/
88
+ * Copies object to secure it from changes
89
+ * @param {object} obj original object
90
+ * @return {object} copy of object
91
+ **/
91
92
  module.exports.copyObj = (obj) => {
92
93
  return JSON.parse(JSON.stringify(obj));
93
94
  };
94
95
 
95
96
  /**
96
- * Copies object to secure it from changes
97
- * @param {object} obj original object
98
- * @return {object} copy of object
99
- **/
97
+ * Copies object to secure it from changes
98
+ * @param {object} obj original object
99
+ * @return {object} copy of object
100
+ **/
100
101
  module.exports.partCopyObj = (obj, list) => {
101
- let partObj = Object.keys(obj).reduce((prev, curr)=>{
102
- if(list.includes(curr)){
102
+ let partObj = Object.keys(obj).reduce((prev, curr) => {
103
+ if (list.includes(curr)) {
103
104
  prev[curr] = obj[curr];
104
105
  }
105
106
  return prev;
@@ -109,38 +110,38 @@ module.exports.partCopyObj = (obj, list) => {
109
110
 
110
111
 
111
112
  /**
112
- * Test argument type to be 'function'
113
- * @param {any} func possible function
114
- * @return {boolean} if this is a function
115
- **/
113
+ * Test argument type to be 'function'
114
+ * @param {any} func possible function
115
+ * @return {boolean} if this is a function
116
+ **/
116
117
  const isFunc = module.exports.isFunc = (func) => {
117
118
  return typeof func === 'function';
118
119
  };
119
120
 
120
121
  /**
121
- * Returns true if argument is Async function
122
- * @param {function} func to test
123
- * @return {boolean} if this function is constructed as AsyncFunction
124
- **/
122
+ * Returns true if argument is Async function
123
+ * @param {function} func to test
124
+ * @return {boolean} if this function is constructed as AsyncFunction
125
+ **/
125
126
  const isAsync = module.exports.isAsync = (func) => {
126
127
  return func.constructor.name === 'AsyncFunction';
127
128
  };
128
129
 
129
130
 
130
131
  /**
131
- * Executes method of object in appropriate way inside Promise
132
- * @param {object} obj original object
133
- * @param {string} name method name to execute
134
- * @param {Array} params array of params
135
- * @return {Promise} results of method execution
136
- **/
132
+ * Executes method of object in appropriate way inside Promise
133
+ * @param {object} obj original object
134
+ * @param {string} name method name to execute
135
+ * @param {Array} params array of params
136
+ * @return {Promise} results of method execution
137
+ **/
137
138
  module.exports.executeObjectFunction = async (obj, name, params) => {
138
- if (obj){
139
+ if (obj) {
139
140
  const proc = notPath.get(':' + name, obj);
140
- if(isFunc(proc)){
141
- if(isAsync(proc)){
141
+ if (isFunc(proc)) {
142
+ if (isAsync(proc)) {
142
143
  return await proc(...params);
143
- }else{
144
+ } else {
144
145
  return proc(...params);
145
146
  }
146
147
  }
@@ -149,14 +150,14 @@ module.exports.executeObjectFunction = async (obj, name, params) => {
149
150
 
150
151
 
151
152
  /**
152
- * Executes method of object in apropriate way inside Promise
153
- * @param {Object} from original object
154
- * @param {Object} name method name to execute
155
- * @param {Array} list array of params
156
- * @return {Promise} results of method execution
157
- **/
153
+ * Executes method of object in apropriate way inside Promise
154
+ * @param {Object} from original object
155
+ * @param {Object} name method name to execute
156
+ * @param {Array} list array of params
157
+ * @return {Promise} results of method execution
158
+ **/
158
159
  module.exports.mapBind = (from, to, list) => {
159
- list.forEach((item)=>{
160
+ list.forEach((item) => {
160
161
  if (typeof from[item] === 'function') {
161
162
  to[item] = from[item].bind(from);
162
163
  }
@@ -165,10 +166,10 @@ module.exports.mapBind = (from, to, list) => {
165
166
 
166
167
 
167
168
  /**
168
- * Synchronously check file existence and if it's really a file
169
- * @param {string} filePath full path to file
170
- * @return {boolean} true if path exists and it's a file
171
- **/
169
+ * Synchronously check file existence and if it's really a file
170
+ * @param {string} filePath full path to file
171
+ * @return {boolean} true if path exists and it's a file
172
+ **/
172
173
  module.exports.tryFile = (filePath) => {
173
174
  try {
174
175
  const stat = fs.lstatSync(filePath);
@@ -177,3 +178,18 @@ module.exports.tryFile = (filePath) => {
177
178
  return false;
178
179
  }
179
180
  };
181
+
182
+ /**
183
+ * Generates paths object for module/index.js files based on content and relative
184
+ * path
185
+ * @param {Array<string>} content list of module components ['models', 'logics', 'routes',...]
186
+ * @param {string} relative relative path to parent folder of components
187
+ * @param {Object} paths object for module/index.js
188
+ **/
189
+ module.exports.generatePaths = (content = [], relative = 'src') => {
190
+ const toPath = (name) => path.join(__dirname, relative, name);
191
+ return content.reduce((prev, cur) => {
192
+ prev[cur] = toPath(cur);
193
+ return prev;
194
+ }, {});
195
+ };
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({
@@ -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, //field name
42
- field, //field description
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
- var result = null;
12
- result = validators.map((item) => {
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
  };
@@ -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
@@ -24,7 +24,8 @@ module.exports = ({
24
24
  describe('run', function() {
25
25
  it('path to fields is not defined', function() {
26
26
  const ctx = {
27
- findAll() {}
27
+ findAll() {},
28
+ extendByFrontValidators(){}
28
29
  };
29
30
  const param = {
30
31
  nModule: {
@@ -39,7 +40,8 @@ module.exports = ({
39
40
 
40
41
  it('paths to fields is defined', function() {
41
42
  const ctx = {
42
- findAll() {}
43
+ findAll() {},
44
+ extendByFrontValidators(){}
43
45
  };
44
46
  const param = {
45
47
  nModule: {
@@ -149,7 +151,9 @@ module.exports = ({
149
151
 
150
152
  describe('registerField', function() {
151
153
  it('file is a single field', () => {
152
- const ctx = {};
154
+ const ctx = {
155
+ extendByFrontValidators(){}
156
+ };
153
157
  const param = {
154
158
  name: 'single',
155
159
  field: require(path.resolve(__dirname, '../testies/module/fields/single.js')),
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