not-node 5.1.19 → 5.1.22

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.19",
3
+ "version": "5.1.22",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,3 @@
1
+ module.exports = (req, name) => {
2
+ return req.body[name];
3
+ };
@@ -0,0 +1,3 @@
1
+ module.exports = (req, name) => {
2
+ return req.params[name];
3
+ };
@@ -0,0 +1,3 @@
1
+ module.exports = (req, name) => {
2
+ return req.query[name];
3
+ };
package/src/form/form.js CHANGED
@@ -2,13 +2,17 @@ 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");
15
+
12
16
  /**
13
17
  * Generic form validation class
14
18
  **/
@@ -26,12 +30,18 @@ class Form {
26
30
  #FORM_NAME;
27
31
  #PROTO_FIELDS;
28
32
  #VALIDATOR;
33
+ #EXTRACTORS = {
34
+ fromQuery,
35
+ fromBody,
36
+ fromParams,
37
+ };
29
38
 
30
- constructor({ FIELDS, FORM_NAME, app }) {
39
+ constructor({ FIELDS, FORM_NAME, app, EXTRACTORS = {} }) {
31
40
  this.#FORM_NAME = FORM_NAME;
32
41
  this.#PROTO_FIELDS = FIELDS;
33
42
  this.#createValidationSchema(app);
34
43
  this.#augmentValidationSchema();
44
+ this.#addExtractors(EXTRACTORS);
35
45
  }
36
46
 
37
47
  /**
@@ -207,6 +217,24 @@ class Form {
207
217
  static fabric() {
208
218
  return FormFabric;
209
219
  }
220
+
221
+ #addExtractors(extractors = {}) {
222
+ if (extractors) {
223
+ this.#EXTRACTORS = { ...this.#EXTRACTORS, ...extractors };
224
+ }
225
+ }
226
+
227
+ extractByInstructions(req, instructions) {
228
+ const results = {};
229
+ for (let fieldName of instructions) {
230
+ const instruction = instructions[fieldName];
231
+ const extractor = this.#EXTRACTORS[instruction];
232
+ if (isFunc(extractor)) {
233
+ results[fieldName] = extractor(req, fieldName);
234
+ }
235
+ }
236
+ return results;
237
+ }
210
238
  }
211
239
 
212
240
  module.exports = Form;