not-node 5.1.20 → 5.1.23

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.20",
3
+ "version": "5.1.23",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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
@@ -2,16 +2,18 @@ const FormFabric = require("./fabric");
2
2
 
3
3
  const { createSchemaFromFields } = require("../fields");
4
4
 
5
- const { objHas } = require("../common");
5
+ const { objHas, isFunc } = require("../common");
6
6
 
7
7
  const ValidationBuilder = require("not-validation").Builder;
8
8
  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,12 +33,10 @@ 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
- constructor({ FIELDS, FORM_NAME, app, EXTRACTORS = [] }) {
39
+ constructor({ FIELDS, FORM_NAME, app, EXTRACTORS = {} }) {
40
40
  this.#FORM_NAME = FORM_NAME;
41
41
  this.#PROTO_FIELDS = FIELDS;
42
42
  this.#createValidationSchema(app);
@@ -218,15 +218,28 @@ class Form {
218
218
  return FormFabric;
219
219
  }
220
220
 
221
- #addExtractors(extractors) {
222
- this.#EXTRACTORS = { ...this.#EXTRACTORS, ...extractors };
221
+ #addExtractors(extractors = {}) {
222
+ if (extractors) {
223
+ this.#EXTRACTORS = { ...this.#EXTRACTORS, ...extractors };
224
+ }
223
225
  }
224
226
 
225
227
  extractByInstructions(req, instructions) {
226
228
  const results = {};
227
- for (let fieldName of instructions) {
229
+ for (let fieldName in instructions) {
228
230
  const instruction = instructions[fieldName];
229
- results[fieldName] = instruction(req, fieldName);
231
+ if (isFunc(instruction)) {
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
+ }
242
+ }
230
243
  }
231
244
  return results;
232
245
  }