not-node 6.4.11 → 6.4.12

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/form/form.js +39 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "6.4.11",
3
+ "version": "6.4.12",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/form/form.js CHANGED
@@ -11,6 +11,7 @@ const {
11
11
  isFunc,
12
12
  firstLetterToUpper,
13
13
  firstLetterToLower,
14
+ copyObj,
14
15
  } = require("../common");
15
16
 
16
17
  const ValidationBuilder = require("not-validation").Builder;
@@ -97,7 +98,7 @@ class Form {
97
98
  * @param {import('../types.js').notAppFormRateLimiterOptions} options.rate
98
99
  */
99
100
  constructor({
100
- FIELDS,
101
+ FIELDS = [],
101
102
  FORM_NAME,
102
103
  MODEL_NAME,
103
104
  MODULE_NAME,
@@ -114,7 +115,7 @@ class Form {
114
115
  FORM_NAME ?? Form.createName(MODULE_NAME, MODEL_NAME, actionName);
115
116
  this.#MODEL_NAME = MODEL_NAME;
116
117
  this.#MODULE_NAME = MODULE_NAME;
117
- this.#PROTO_FIELDS = FIELDS;
118
+ this.#setFields(app, FIELDS);
118
119
  this.#createValidationSchema(app);
119
120
  this.#augmentValidationSchema();
120
121
  this.#addInstructions(INSTRUCTIONS);
@@ -125,6 +126,42 @@ class Form {
125
126
  this.#createRateLimiter(rate);
126
127
  }
127
128
 
129
+ #setFields(app, FIELDS = []) {
130
+ try {
131
+ const warning = () =>
132
+ app.logger.warn(`No PROTO_FIELDS for ${this.#FORM_NAME} form`);
133
+ if (FIELDS && Array.isArray(FIELDS) && FIELDS.length) {
134
+ this.#PROTO_FIELDS = FIELDS;
135
+ } else if (this.#MODEL_NAME) {
136
+ let modelPath = `${this.#MODEL_NAME}`;
137
+ if (this.#MODULE_NAME) {
138
+ modelPath = `${this.#MODULE_NAME}//${this.#MODEL_NAME}`;
139
+ }
140
+ //no path to model - returning after warning
141
+ if (!modelPath) {
142
+ warning();
143
+ return;
144
+ }
145
+ const mod = app.getModelFile(modelPath);
146
+ //no module or fields in module exports - returning after warning
147
+ if (
148
+ !(
149
+ mod &&
150
+ mod.FIELDS &&
151
+ Array.isArray(mod.FIELDS) &&
152
+ mod.FIELDS.length
153
+ )
154
+ ) {
155
+ warning();
156
+ return;
157
+ }
158
+ this.#PROTO_FIELDS = copyObj(mod.FIELDS);
159
+ }
160
+ } catch (e) {
161
+ app.logger.error(e);
162
+ }
163
+ }
164
+
128
165
  get FORM_NAME() {
129
166
  return this.#FORM_NAME;
130
167
  }