not-node 5.1.21 → 5.1.24

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": "5.1.21",
3
+ "version": "5.1.24",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/common.js CHANGED
@@ -156,7 +156,8 @@ module.exports.executeFunctionAsAsync = executeFunctionAsAsync;
156
156
  **/
157
157
  module.exports.executeObjectFunction = async (obj, name, params) => {
158
158
  if (obj) {
159
- const proc = notPath.get(":" + name, obj);
159
+ const proc =
160
+ typeof obj == "object" ? notPath.get(":" + name, obj) : obj[name];
160
161
  return await executeFunctionAsAsync(proc, params);
161
162
  }
162
163
  };
@@ -64,5 +64,6 @@
64
64
  "crud_delete_action_waiting": "Deleting record...",
65
65
  "add_label": "Add",
66
66
  "select_from_list_label": "Select from list...",
67
- "field_actions_label": "Actions"
67
+ "field_actions_label": "Actions",
68
+ "form_exception_field_extractor_is_undefined": "Field extractor is undefined"
68
69
  }
@@ -63,7 +63,8 @@
63
63
  "crud_delete_action_waiting": "Удаление записи...",
64
64
  "add_label": "Добавить",
65
65
  "select_from_list_label": "Выберите из списка...",
66
- "field_actions_label": "Действия"
66
+ "field_actions_label": "Действия",
67
+ "form_exception_field_extractor_is_undefined": "Экстрактор для поля входных данных отсутствует"
67
68
 
68
69
 
69
70
  }
@@ -0,0 +1,11 @@
1
+ const { notRequestError } = require("not-error");
2
+ class FormExceptionExtractorForFieldIsUndefined extends notRequestError {
3
+ constructor(fieldName) {
4
+ super("not-node:form_exception_field_extractor_is_undefined", {
5
+ fieldName,
6
+ });
7
+ }
8
+ }
9
+
10
+ module.exports.FormExceptionExtractorForFieldIsUndefined =
11
+ FormExceptionExtractorForFieldIsUndefined;
@@ -0,0 +1,3 @@
1
+ module.exports = (req) => {
2
+ return req.user ? req.user._id : undefined;
3
+ };
@@ -0,0 +1 @@
1
+ module.exports = () => "User";
@@ -0,0 +1,7 @@
1
+ module.exports = {
2
+ activeUserId: require("./activeUserId.js"),
3
+ activeUserModelName: require("./activeUserModelName.js"),
4
+ fromBody: require("./fromBody.js"),
5
+ fromQuery: require("./fromQuery.js"),
6
+ fromParams: require("./fromParams.js"),
7
+ };
package/src/form/form.js CHANGED
@@ -9,9 +9,11 @@ const ValidationSession = require("not-validation").Session;
9
9
 
10
10
  const { notValidationError, notError } = require("not-error");
11
11
 
12
- const fromBody = require("./extractors/fromBody.js");
13
- const fromQuery = require("./extractors/fromQuery.js");
14
- const fromParams = require("./extractors/fromParams.js");
12
+ const {
13
+ FormExceptionExtractorForFieldIsUndefined,
14
+ } = require("../exceptions/form.js");
15
+
16
+ const DEFAULT_EXTRACTORS = require("./extractors");
15
17
 
16
18
  /**
17
19
  * Generic form validation class
@@ -31,9 +33,7 @@ class Form {
31
33
  #PROTO_FIELDS;
32
34
  #VALIDATOR;
33
35
  #EXTRACTORS = {
34
- fromQuery,
35
- fromBody,
36
- fromParams,
36
+ ...DEFAULT_EXTRACTORS,
37
37
  };
38
38
 
39
39
  constructor({ FIELDS, FORM_NAME, app, EXTRACTORS = {} }) {
@@ -226,10 +226,19 @@ class Form {
226
226
 
227
227
  extractByInstructions(req, instructions) {
228
228
  const results = {};
229
- for (let fieldName of instructions) {
229
+ for (let fieldName in instructions) {
230
230
  const instruction = instructions[fieldName];
231
231
  if (isFunc(instruction)) {
232
232
  results[fieldName] = instruction(req, fieldName);
233
+ } else if (typeof instruction == "string") {
234
+ const extractor = this.#EXTRACTORS[instruction];
235
+ if (isFunc(extractor)) {
236
+ results[fieldName] = extractor(req, fieldName);
237
+ } else {
238
+ throw new FormExceptionExtractorForFieldIsUndefined(
239
+ fieldName
240
+ );
241
+ }
233
242
  }
234
243
  }
235
244
  return results;