not-node 6.5.31 → 6.5.33

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": "6.5.31",
3
+ "version": "6.5.33",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -50,8 +50,12 @@ module.exports = [
50
50
  message: "not-node:identity_field_sid_is_undefined",
51
51
  },
52
52
  {
53
- validator(val) {
54
- return objHas(val, "ip") && typeof val.ip === "string";
53
+ validator(val, { validator }) {
54
+ return (
55
+ objHas(val, "ip") &&
56
+ typeof val.ip === "string" &&
57
+ validator.isIP(val.ip)
58
+ );
55
59
  },
56
60
  message: "not-node:identity_field_ip_is_not_valid",
57
61
  },
@@ -1,7 +1,11 @@
1
1
  module.exports = [
2
2
  {
3
3
  validator: (val, { validator }) => {
4
- return validator.isMongoId(val);
4
+ return (
5
+ (typeof val === "string" && validator.isMongoId(val)) ||
6
+ (typeof val.toString === "function" &&
7
+ validator.isMongoId(val.toString()))
8
+ );
5
9
  },
6
10
  message: `not-node:value_item_format_is_not_objectId`,
7
11
  },
@@ -1,14 +1,11 @@
1
+ const objectId = require("./objectId.js");
2
+
1
3
  module.exports = [
2
4
  {
3
5
  validator: (val) => {
4
6
  return Array.isArray(val);
5
7
  },
6
8
  message: `not-node:value_is_not_array`,
7
- },
8
- {
9
- validator: (val, { validator }) => {
10
- return val.every((itm) => validator.isMongoId(itm));
11
- },
12
- message: `not-node:value_item_format_is_not_objectId`,
13
9
  },
10
+ ...objectId,
14
11
  ];
@@ -1,16 +1,8 @@
1
+ const objectId = require("./objectId");
2
+
1
3
  module.exports = [
2
4
  {
3
- validator(val, { validator }) {
4
- if (typeof val === "string") {
5
- return validator.isMongoId(val);
6
- } else {
7
- return (
8
- val &&
9
- typeof val === "object" &&
10
- val.constructor.name === "ObjectId"
11
- );
12
- }
13
- },
5
+ ...objectId[0],
14
6
  message: "not-node:owner_is_not_valid",
15
7
  },
16
8
  ];
@@ -4,10 +4,10 @@ module.exports = [
4
4
  return !isNaN(val);
5
5
  },
6
6
  message: "not-node:value_is_not_number",
7
- },
7
+ },
8
8
  {
9
9
  validator(val) {
10
- return (val > 0) && (val === 0);
10
+ return val >= 0;
11
11
  },
12
12
  message: "not-node:value_should_be_greater_than_zero",
13
13
  },
@@ -21,7 +21,7 @@ module.exports.initFileSchemaFromFields = ({
21
21
  if (FIELDS && Array.isArray(FIELDS)) {
22
22
  const schema = module.exports.createSchemaFromFields(
23
23
  app,
24
- FIELDS,
24
+ normalizeFieldsDescriptionsList(FIELDS),
25
25
  type,
26
26
  moduleName
27
27
  );
@@ -30,11 +30,11 @@ module.exports.initFileSchemaFromFields = ({
30
30
  };
31
31
 
32
32
  /**
33
- fields = [
34
- 'title', //for standart only name
35
- ['titleNonStandart', {component: 'UITextforBlind'}] //arrays of [name, mutation]
36
- ['someID', {}, 'ID'], //copy of standart ID field under name as someID
37
- ]
33
+ fields = [{
34
+ srcName:string,
35
+ destName:string,
36
+ mutation:object,
37
+ }]
38
38
  **/
39
39
  module.exports.createSchemaFromFields = (
40
40
  app,
@@ -43,9 +43,15 @@ module.exports.createSchemaFromFields = (
43
43
  moduleName
44
44
  ) => {
45
45
  let schema = {};
46
- fields.forEach((field) => {
46
+ fields.forEach((fieldDescription) => {
47
47
  let [schemaFieldName, schemaFieldValue] =
48
- module.exports.initSchemaField(app, field, false, type, moduleName);
48
+ module.exports.initSchemaField(
49
+ app,
50
+ fieldDescription,
51
+ false,
52
+ type,
53
+ moduleName
54
+ );
49
55
  schema[schemaFieldName] = schemaFieldValue;
50
56
  });
51
57
  return schema;
@@ -53,13 +59,13 @@ module.exports.createSchemaFromFields = (
53
59
 
54
60
  module.exports.initSchemaField = (
55
61
  app,
56
- field,
62
+ fieldDescription,
57
63
  resultOnly = true,
58
64
  type = "ui",
59
65
  moduleName
60
66
  ) => {
61
67
  //log(field);
62
- let { srcName, destName, mutation } = parseFieldDescription(field);
68
+ let { srcName, destName, mutation } = fieldDescription;
63
69
  let proto = findFieldPrototype(app, srcName, type);
64
70
  if (!proto) {
65
71
  error(
@@ -124,11 +130,24 @@ const findFieldPrototype = (app, name, type) => {
124
130
  }
125
131
  };
126
132
 
133
+ const filterOutPrivateFieldsFromNormalizedFieldDescription = (
134
+ privateFields
135
+ ) => {
136
+ return ({ destName }) => {
137
+ return !privateFields.includes(destName);
138
+ };
139
+ };
140
+
141
+ const normalizeFieldsDescriptionsList = (list) => {
142
+ return list.map(parseFieldDescription);
143
+ };
144
+ module.exports.normalizeFieldsDescriptionsList =
145
+ normalizeFieldsDescriptionsList;
127
146
  /**
128
147
  * Creates fields UI representation schema from list of fields in DB model
129
148
  * and library of fields
130
149
  * @param {object} app notApplication instance
131
- * @param {object} schema model db schema
150
+ * @param {object} rawFieldsList model db schema
132
151
  * @param {Array<string|Array>} rawMutationsList fields mutations, for little tuning
133
152
  * @param {Array<string>} privateFields fields to omit from result
134
153
  * @param {string} moduleName for detailed reports on issues, in which module and what field is faulty
@@ -137,47 +156,82 @@ const findFieldPrototype = (app, name, type) => {
137
156
 
138
157
  module.exports.initManifestFields = (
139
158
  app, //notApplication
140
- schema, //schema of model
159
+ rawFieldsList, //raw fields list of model
141
160
  rawMutationsList = [], //fields mutations
142
161
  privateFields = [], //fields to omit
143
162
  moduleName //module name for reporting
144
163
  ) => {
145
- let //shallow copy of array
146
- mutationsList = [...rawMutationsList],
147
- list = [];
148
- if (schema && Object.keys(schema).length > 0) {
149
- let rawKeys = Object.keys(schema);
150
- rawKeys
151
- .filter((key) => !privateFields.includes(key))
152
- .forEach((key) => {
153
- let mutation = getMutationForField(key, mutationsList);
154
- if (mutation.length) {
155
- list.push(mutation);
156
- mutationsList.splice(mutationsList.indexOf(mutation), 1);
164
+ const //shallow copy of array
165
+ normalizedMutationsList = [
166
+ ...normalizeFieldsDescriptionsList(rawMutationsList),
167
+ ],
168
+ resultFields = [];
169
+
170
+ if (
171
+ rawFieldsList &&
172
+ Array.isArray(rawFieldsList) &&
173
+ rawFieldsList.length > 0
174
+ ) {
175
+ const normalizedFieldsList =
176
+ normalizeFieldsDescriptionsList(rawFieldsList);
177
+ normalizedFieldsList
178
+ .filter(
179
+ filterOutPrivateFieldsFromNormalizedFieldDescription(
180
+ privateFields
181
+ )
182
+ )
183
+ .forEach((fieldDescription) => {
184
+ const { srcName, destName } = fieldDescription;
185
+ const fieldMutationDescription = getMutationForField(
186
+ destName,
187
+ normalizedMutationsList
188
+ );
189
+ if (fieldMutationDescription) {
190
+ resultFields.push({
191
+ srcName,
192
+ destName,
193
+ mutation: fieldMutationDescription.mutation,
194
+ });
195
+ normalizedMutationsList.splice(
196
+ normalizedMutationsList.indexOf(
197
+ fieldMutationDescription
198
+ ),
199
+ 1
200
+ );
157
201
  } else {
158
- list.push(key);
202
+ resultFields.push({
203
+ srcName,
204
+ destName,
205
+ mutation: {},
206
+ });
159
207
  }
160
208
  });
161
- list.push(...mutationsList);
209
+ resultFields.push(...normalizedMutationsList);
162
210
  } else {
163
- list = mutationsList;
211
+ resultFields.push(...normalizedMutationsList);
164
212
  }
165
- return module.exports.createSchemaFromFields(app, list, "ui", moduleName);
213
+ return module.exports.createSchemaFromFields(
214
+ app,
215
+ resultFields,
216
+ "ui",
217
+ moduleName
218
+ );
166
219
  };
167
220
 
168
221
  /**
169
222
  * Returns mutation tuple for a field or false
170
- * @param {string} name field name
171
- * @param {Array} list fields description lists
172
- * @return {Array<string|Object>}
223
+ * @param {string} destName field name
224
+ * @param {Array} normalizedFieldsList fields description lists
225
+ * @return {Object}
173
226
  */
174
- function getMutationForField(name, list) {
175
- for (let item of list) {
176
- if (Array.isArray(item) && item[0] === name) {
177
- return item;
227
+ function getMutationForField(destName, normalizedFieldsList) {
228
+ for (let mutationFieldDescription of normalizedFieldsList) {
229
+ const { destName: mutationDest } = mutationFieldDescription;
230
+ if (mutationDest === destName) {
231
+ return mutationFieldDescription;
178
232
  }
179
233
  }
180
- return [];
234
+ return null;
181
235
  }
182
236
  module.exports.getMutationForField = getMutationForField;
183
237
 
package/src/form/form.js CHANGED
@@ -3,8 +3,12 @@ const validator = require("validator");
3
3
  const notPath = require("not-path");
4
4
  const notConfig = require("not-config");
5
5
  const FormFabric = require("./fabric");
6
+ const notEnv = require("../env.js");
6
7
  const Auth = require("../auth");
7
- const { createSchemaFromFields } = require("../fields");
8
+ const {
9
+ createSchemaFromFields,
10
+ normalizeFieldsDescriptionsList,
11
+ } = require("../fields");
8
12
  const notFieldsFilter = require("../fields/filter.js");
9
13
  const getApp = require("../getApp.js");
10
14
  const {
@@ -36,6 +40,11 @@ const DEFAULT_AFTER_EXTRACT_TRANSFORMERS = [];
36
40
  const notAppIdentity = require("../identity/index.js");
37
41
  const { ACTION_DATA_TYPES } = require("../const.js");
38
42
 
43
+ const DEFAULT_VALIDATOR_ENVS = {
44
+ validator,
45
+ env: true, //some env variables for validators
46
+ };
47
+
39
48
  /**
40
49
  * Generic form validation class
41
50
  * @class Form
@@ -427,15 +436,27 @@ class Form {
427
436
  * Returns function that works as a getter for additional environment variables for
428
437
  * validators.
429
438
  * validationFunction(value, additionalEnvVars = {}){}
439
+ * @return {function} getter of validator envs
430
440
  **/
431
441
  getValidatorEnvGetter() {
432
- return () => {
442
+ //app wide validation envs
443
+ const appLevelValidationEnvsGetter = notEnv.get("validationEnv");
444
+ if (typeof appLevelValidationEnvsGetter === "function") {
433
445
  //should be sync function
434
- return {
435
- validator,
436
- env: true, //some env variables for validators
446
+ return () => {
447
+ return Object.freeze({
448
+ ...DEFAULT_VALIDATOR_ENVS,
449
+ ...appLevelValidationEnvsGetter(),
450
+ });
437
451
  };
438
- };
452
+ } else {
453
+ return () => {
454
+ //should be sync function
455
+ return Object.freeze({
456
+ ...DEFAULT_VALIDATOR_ENVS,
457
+ });
458
+ };
459
+ }
439
460
  }
440
461
 
441
462
  /**
@@ -474,7 +495,7 @@ class Form {
474
495
  #createModelSchema(app) {
475
496
  return createSchemaFromFields(
476
497
  app,
477
- this.#PROTO_FIELDS,
498
+ normalizeFieldsDescriptionsList(this.#PROTO_FIELDS),
478
499
  "model",
479
500
  this.#FORM_NAME
480
501
  );
@@ -743,6 +764,8 @@ class Form {
743
764
  case String:
744
765
  case Schema.Types.String:
745
766
  return ["xss"];
767
+ case Schema.Types.Number:
768
+ return ["parseNumber"];
746
769
  default:
747
770
  return [];
748
771
  }
@@ -6,5 +6,6 @@ module.exports = {
6
6
  stringToJSON: require("./stringToJSON.js"),
7
7
  xss: require("./xss.js"),
8
8
  boolean: require("./boolean.js"),
9
+ parseNumber: require("./parseNumber.js"),
9
10
  __CLEAR__: require("./__CLEAR__.js"),
10
11
  };
@@ -0,0 +1,3 @@
1
+ module.exports = (input) => {
2
+ return ~~input;
3
+ };
@@ -4,34 +4,48 @@ const { initManifestFields } = require("../../fields");
4
4
  const { firstLetterToUpper } = require("../../common");
5
5
 
6
6
  const fieldsIsArray = (mod) => mod && Array.isArray(mod.fields);
7
- const extractPrivateFields = (mod) =>
8
- Array.isArray(mod.privateFields) ? [...mod.privateFields] : [];
7
+
8
+
9
+ const getModelName = (routeManifest)=>firstLetterToUpper(routeManifest.model);
9
10
 
10
11
  module.exports = class notModuleInitializatorManifests {
11
12
  static openFile = require;
12
13
 
14
+ static getMutationsFromManifest(nModule,routeName){
15
+ const routeManifest = nModule.getRouteManifest(routeName);
16
+ if(fieldsIsArray(routeManifest)){
17
+ return [...routeManifest.fields];
18
+ }
19
+ return [];
20
+ }
21
+
22
+ static extractPrivateFields(routeManifest){
23
+ const privateFields = [];
24
+ if (routeManifest.privateFields) {
25
+ privateFields.push(...(Array.isArray(routeManifest.privateFields) ? [...routeManifest.privateFields] : []));
26
+ delete routeManifest.privateFields;
27
+ }
28
+ return privateFields;
29
+ }
30
+
13
31
  static run({ nModule }) {
14
32
  const moduleName = nModule.getName();
15
33
  for (let routeName in nModule.getRoutesManifests()) {
16
34
  try {
17
- const mod = nModule.getRouteManifest(routeName);
18
- if (fieldsIsArray(mod)) {
19
- const rawMutationsList = [...mod.fields];
20
- const ModelName = firstLetterToUpper(mod.model);
21
- const schema = nModule.getModelSchema(ModelName);
22
- let privateFields = [];
23
- if (mod.privateFields) {
24
- privateFields = extractPrivateFields(mod);
25
- delete mod.privateFields;
26
- }
27
- mod.fields = initManifestFields(
28
- nModule.getApp(),
29
- schema,
30
- rawMutationsList,
31
- privateFields,
32
- moduleName
33
- );
34
- }
35
+ const routeManifest = nModule.getRouteManifest(routeName);
36
+ const rawMutationsList = notModuleInitializatorManifests.getMutationsFromManifest(nModule, routeName);
37
+ const ModelName = getModelName(routeManifest);
38
+ const rawFieldsList = nModule.getModelFields(ModelName);
39
+ const privateFields = notModuleInitializatorManifests.extractPrivateFields(routeManifest);
40
+
41
+ routeManifest.fields = initManifestFields(
42
+ nModule.getApp(),
43
+ rawFieldsList,
44
+ rawMutationsList,
45
+ privateFields,
46
+ moduleName
47
+ );
48
+
35
49
  } catch (e) {
36
50
  error(
37
51
  `Error while initialization of route: ${moduleName}//${routeName}`
@@ -171,6 +171,14 @@ class notModule {
171
171
  return null;
172
172
  }
173
173
 
174
+ getModelFields(modelName) {
175
+ const modelFile = this.getModelFile(modelName);
176
+ if (modelFile && objHas(modelFile, 'FIELDS') && modelFile.FIELDS) {
177
+ return modelFile.FIELDS;
178
+ }
179
+ return null;
180
+ }
181
+
174
182
  expose(expressApp, moduleName) {
175
183
  if (this.manifests && expressApp) {
176
184
  notModuleInitializator.exec({ nModule: this });