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 +1 -1
- package/src/core/locales/en.json +2 -1
- package/src/core/locales/ru.json +2 -1
- package/src/exceptions/form.js +11 -0
- package/src/form/extractors/activeUserId.js +3 -0
- package/src/form/extractors/activeUserModelName.js +1 -0
- package/src/form/extractors/index.js +7 -0
- package/src/form/form.js +25 -12
package/package.json
CHANGED
package/src/core/locales/en.json
CHANGED
|
@@ -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
|
}
|
package/src/core/locales/ru.json
CHANGED
|
@@ -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 @@
|
|
|
1
|
+
module.exports = () => "User";
|
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
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
229
|
+
for (let fieldName in instructions) {
|
|
228
230
|
const instruction = instructions[fieldName];
|
|
229
|
-
|
|
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
|
}
|