not-node 6.0.4 → 6.0.6

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.0.4",
3
+ "version": "6.0.6",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -9,3 +9,15 @@ class FormExceptionExtractorForFieldIsUndefined extends notRequestError {
9
9
 
10
10
  module.exports.FormExceptionExtractorForFieldIsUndefined =
11
11
  FormExceptionExtractorForFieldIsUndefined;
12
+
13
+ class FormExceptionTransformerForFieldIsUndefined extends notRequestError {
14
+ constructor(fieldName, instruction) {
15
+ super("not-node:form_exception_field_transformer_is_undefined", {
16
+ fieldName,
17
+ instruction,
18
+ });
19
+ }
20
+ }
21
+
22
+ module.exports.FormExceptionTransformerForFieldIsUndefined =
23
+ FormExceptionTransformerForFieldIsUndefined;
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Extracts specific environmental values
3
+ */
4
+
1
5
  module.exports = {
2
6
  _id: require("./_id.js"),
3
7
  ID: require("./ID.js"),
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Extracts values from different input sources
3
+ */
1
4
  module.exports = {
2
5
  activeUserId: require("./activeUserId.js"),
3
6
  activeUserModelName: require("./activeUserModelName.js"),
package/src/form/form.js CHANGED
@@ -11,10 +11,12 @@ const { notValidationError, notError } = require("not-error");
11
11
 
12
12
  const {
13
13
  FormExceptionExtractorForFieldIsUndefined,
14
+ FormExceptionTransformerForFieldIsUndefined,
14
15
  } = require("../exceptions/form.js");
15
16
 
16
17
  const DEFAULT_EXTRACTORS = require("./extractors");
17
18
  const DEFAULT_ID_EXTRACTORS = require("./env_extractors");
19
+ const DEFAULT_TRANSFORMERS = require("./transformers");
18
20
 
19
21
  /**
20
22
  * Generic form validation class
@@ -43,6 +45,10 @@ class Form {
43
45
  ...DEFAULT_ID_EXTRACTORS,
44
46
  };
45
47
 
48
+ #TRANSFORMERS = {
49
+ ...DEFAULT_TRANSFORMERS,
50
+ };
51
+
46
52
  constructor({
47
53
  FIELDS,
48
54
  FORM_NAME,
@@ -51,6 +57,7 @@ class Form {
51
57
  app,
52
58
  EXTRACTORS = {},
53
59
  ENV_EXTRACTORS = {},
60
+ TRANSFORMERS = {},
54
61
  }) {
55
62
  this.#FORM_NAME = FORM_NAME;
56
63
  this.#MODEL_NAME = MODEL_NAME;
@@ -60,6 +67,7 @@ class Form {
60
67
  this.#augmentValidationSchema();
61
68
  this.#addExtractors(EXTRACTORS);
62
69
  this.#addEnvExtractors(ENV_EXTRACTORS);
70
+ this.#addTransformers(TRANSFORMERS);
63
71
  }
64
72
 
65
73
  getModelName(req) {
@@ -283,22 +291,74 @@ class Form {
283
291
  const results = {};
284
292
  for (let fieldName in instructions) {
285
293
  const instruction = instructions[fieldName];
286
- if (isFunc(instruction)) {
287
- results[fieldName] = instruction(req, fieldName);
288
- } else if (typeof instruction == "string") {
289
- const extractor = this.#EXTRACTORS[instruction];
290
- if (isFunc(extractor)) {
291
- results[fieldName] = extractor(req, fieldName);
292
- } else {
293
- throw new FormExceptionExtractorForFieldIsUndefined(
294
- fieldName
295
- );
296
- }
294
+ if (Array.isArray(instruction)) {
295
+ this.#extractByInstructionPipe({
296
+ results,
297
+ instructions: instruction,
298
+ fieldName,
299
+ req,
300
+ });
301
+ } else {
302
+ this.#extractByInstruction({
303
+ results,
304
+ instruction,
305
+ fieldName,
306
+ req,
307
+ });
297
308
  }
298
309
  }
299
310
  return results;
300
311
  }
301
312
 
313
+ #extractByInstruction({ results, instruction, fieldName, req }) {
314
+ if (isFunc(instruction)) {
315
+ results[fieldName] = instruction(req, fieldName);
316
+ } else if (typeof instruction == "string") {
317
+ const extractor = this.#EXTRACTORS[instruction];
318
+ if (isFunc(extractor)) {
319
+ results[fieldName] = extractor(req, fieldName);
320
+ } else {
321
+ throw new FormExceptionExtractorForFieldIsUndefined(fieldName);
322
+ }
323
+ }
324
+ }
325
+
326
+ #extractByInstructionPipe({ results, instructions, fieldName, req }) {
327
+ if (instructions.length === 0) {
328
+ throw new FormExceptionExtractorForFieldIsUndefined(fieldName);
329
+ }
330
+ this.#extractByInstruction({
331
+ results,
332
+ instruction: instructions[0],
333
+ fieldName,
334
+ req,
335
+ });
336
+ for (let t = 1; t < instructions.length; t++) {
337
+ const instruction = instructions[t];
338
+ this.#transformByInstruction({
339
+ results,
340
+ instruction,
341
+ fieldName,
342
+ });
343
+ }
344
+ }
345
+
346
+ #transformByInstruction({ results, instruction, fieldName }) {
347
+ if (isFunc(instruction)) {
348
+ results[fieldName] = instruction(results[fieldName]);
349
+ } else if (typeof instruction == "string") {
350
+ const transformer = this.#TRANSFORMERS[instruction];
351
+ if (isFunc(transformer)) {
352
+ results[fieldName] = transformer(results[fieldName]);
353
+ } else {
354
+ throw new FormExceptionTransformerForFieldIsUndefined(
355
+ fieldName,
356
+ instruction
357
+ );
358
+ }
359
+ }
360
+ }
361
+
302
362
  createInstructionFromRouteActionFields(
303
363
  req,
304
364
  mainInstruction = "fromBody",
@@ -333,6 +393,15 @@ class Form {
333
393
  );
334
394
  return this.extractByInstructions(req, instructions);
335
395
  }
396
+
397
+ /**
398
+ * Value transformers
399
+ */
400
+ #addTransformers(transformers = {}) {
401
+ if (transformers) {
402
+ this.#TRANSFORMERS = { ...this.#TRANSFORMERS, ...transformers };
403
+ }
404
+ }
336
405
  }
337
406
 
338
407
  module.exports = Form;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Transforms data
3
+ */
4
+
5
+ module.exports = {
6
+ stringToJSON: require("./stringToJSON.js"),
7
+ };
@@ -0,0 +1,3 @@
1
+ module.exports = (input) => {
2
+ return JSON.parse(input);
3
+ };