nox-validation 1.4.0 → 1.4.1

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/lib/constant.js CHANGED
@@ -128,6 +128,7 @@ const types = Object.freeze({
128
128
  MIXED: "Mixed",
129
129
  DATE_TIME: "datetime",
130
130
  TIME: "time",
131
+ TIMESTAMP: "timestamp",
131
132
  });
132
133
 
133
134
  const interfaces = Object.freeze({
@@ -154,14 +155,14 @@ const API_VERSIONS = Object.freeze(Object.values(API_VERSION));
154
155
  const LANGUAGES = Object.freeze({
155
156
  en: "en",
156
157
  nl: "nl",
157
- lv:"lv",
158
- pl:"pl",
159
- ro:"ro",
160
- ru:"ru",
158
+ lv: "lv",
159
+ pl: "pl",
160
+ ro: "ro",
161
+ ru: "ru",
161
162
  });
162
163
 
163
164
  const LOCALE_NL_MESSAGES = Object.freeze({
164
- ADD_CHOICE: `Voeg minstens één optie toe voor {field}.`,
165
+ ADD_CHOICE: `Voeg minstens één optie toe voor {field}.`,
165
166
  INVALID_VALUE: `{field} bevat een ongeldige waarde.`,
166
167
  EMPTY: `{field} moet leeg zijn.`,
167
168
  NOT_EMPTY: `{field} mag niet leeg zijn.`,
@@ -170,7 +171,8 @@ const LOCALE_NL_MESSAGES = Object.freeze({
170
171
  REQUIRED: `{field} is verplicht.`,
171
172
  OPTIONAL: `{field} is optioneel.`,
172
173
  NOT_NULLABLE: `{field} mag niet null zijn.`,
173
- INVALID_TYPE: "{field} bevat een ongeldig invoertype. Alleen {type} is toegestaan.",
174
+ INVALID_TYPE:
175
+ "{field} bevat een ongeldig invoertype. Alleen {type} is toegestaan.",
174
176
  NOT_ALLOWED_FIELD: `{field} is niet toegestaan.`,
175
177
  COMPARE: `{field} komt niet overeen met de verwachte waarde.`,
176
178
  RULES_COMPARE: `{field} voldoet niet aan de vergelijkingsregels.`,
@@ -206,7 +208,7 @@ const LOCALE_NL_MESSAGES = Object.freeze({
206
208
  });
207
209
 
208
210
  const LOCALE_EN_MESSAGES = Object.freeze({
209
- ADD_CHOICE:`Please add at least one choice for {field}.`,
211
+ ADD_CHOICE: `Please add at least one choice for {field}.`,
210
212
  INVALID_VALUE: `{field} contains invalid value.`,
211
213
  EMPTY: `{field} should be empty.`,
212
214
  NOT_EMPTY: `{field} should not be empty.`,
@@ -251,7 +253,7 @@ const LOCALE_EN_MESSAGES = Object.freeze({
251
253
  });
252
254
 
253
255
  const LOCALE_LV_MESSAGES = Object.freeze({
254
- ADD_CHOICE: `Lūdzu, pievienojiet vismaz vienu izvēli laukam {field}.`,
256
+ ADD_CHOICE: `Lūdzu, pievienojiet vismaz vienu izvēli laukam {field}.`,
255
257
  INVALID_VALUE: `{field} satur nederīgu vērtību.`,
256
258
  EMPTY: `{field} jābūt tukšam.`,
257
259
  NOT_EMPTY: `{field} nedrīkst būt tukšs.`,
@@ -341,7 +343,7 @@ const LOCALE_PL_MESSAGES = Object.freeze({
341
343
  });
342
344
 
343
345
  const LOCALE_RO_MESSAGES = Object.freeze({
344
- ADD_CHOICE: `Vă rugăm să adăugați cel puțin o opțiune pentru {field}.`,
346
+ ADD_CHOICE: `Vă rugăm să adăugați cel puțin o opțiune pentru {field}.`,
345
347
  INVALID_VALUE: `{field} conține o valoare invalidă.`,
346
348
  EMPTY: `{field} trebuie să fie gol.`,
347
349
  NOT_EMPTY: `{field} nu trebuie să fie gol.`,
@@ -431,13 +433,13 @@ const LOCALE_RU_MESSAGES = Object.freeze({
431
433
  });
432
434
 
433
435
  const LOCALE_MESSAGES = Object.freeze({
434
- [LANGUAGES.en]:LOCALE_EN_MESSAGES,
435
- [LANGUAGES.nl]:LOCALE_NL_MESSAGES,
436
- [LANGUAGES.lv]:LOCALE_LV_MESSAGES,
437
- [LANGUAGES.pl]:LOCALE_PL_MESSAGES,
438
- [LANGUAGES.ro]:LOCALE_RO_MESSAGES,
439
- [LANGUAGES.ru]:LOCALE_RU_MESSAGES
440
- })
436
+ [LANGUAGES.en]: LOCALE_EN_MESSAGES,
437
+ [LANGUAGES.nl]: LOCALE_NL_MESSAGES,
438
+ [LANGUAGES.lv]: LOCALE_LV_MESSAGES,
439
+ [LANGUAGES.pl]: LOCALE_PL_MESSAGES,
440
+ [LANGUAGES.ro]: LOCALE_RO_MESSAGES,
441
+ [LANGUAGES.ru]: LOCALE_RU_MESSAGES,
442
+ });
441
443
 
442
444
  module.exports = {
443
445
  dataTypes,
@@ -451,5 +453,5 @@ module.exports = {
451
453
  API_VERSIONS,
452
454
  API_VERSION,
453
455
  LANGUAGES,
454
- LOCALE_MESSAGES
456
+ LOCALE_MESSAGES,
455
457
  };
package/lib/validate.js CHANGED
@@ -14,6 +14,16 @@ const {
14
14
  } = require("./helpers");
15
15
 
16
16
  const choices = ["radio", "checkboxes", "dropdown_multiple", "dropdown"];
17
+ const relational_interfaces = [
18
+ constants.interfaces.FILES,
19
+ constants.interfaces.FILE,
20
+ constants.interfaces.FILE_IMAGE,
21
+ constants.interfaces.MANY_TO_MANY,
22
+ constants.interfaces.ONE_TO_MANY,
23
+ constants.interfaces.MANY_TO_ONE,
24
+ constants.interfaces.MANY_TO_ANY,
25
+ constants.interfaces.TRANSLATIONS,
26
+ ];
17
27
 
18
28
  const typeChecks = {
19
29
  date: (val, data) => {
@@ -26,6 +36,7 @@ const typeChecks = {
26
36
  }
27
37
  return false;
28
38
  },
39
+
29
40
  [constants.types.DATE]: (val, data) => {
30
41
  if (val instanceof Date && !isNaN(val)) return true;
31
42
  if (typeof val === "string" && !isNaN(Date.parse(val))) {
@@ -36,6 +47,23 @@ const typeChecks = {
36
47
  }
37
48
  return false;
38
49
  },
50
+ [constants.types.DATE_TIME]: (val, data) => {
51
+ if (val instanceof Date && !isNaN(val)) return true;
52
+ if (typeof val === "string" && !isNaN(Date.parse(val))) {
53
+ if (data && data?.key && data?.updateValue) {
54
+ data.updateValue(data.key, new Date(val));
55
+ }
56
+ return true;
57
+ }
58
+ return false;
59
+ },
60
+ [constants.types.TIME]: (val) =>
61
+ typeof val === "string" &&
62
+ /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/.test(val),
63
+ [constants.types.TIMESTAMP]: (val) =>
64
+ typeof val === "string" &&
65
+ /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/.test(val),
66
+
39
67
  [constants.types.BOOLEAN]: (val, data) => {
40
68
  if (typeof val === "boolean") return true;
41
69
  if (typeof val === "string") {
@@ -49,16 +77,6 @@ const typeChecks = {
49
77
  }
50
78
  return false;
51
79
  },
52
- [constants.types.DATE_TIME]: (val, data) => {
53
- if (val instanceof Date && !isNaN(val)) return true;
54
- if (typeof val === "string" && !isNaN(Date.parse(val))) {
55
- if (data && data?.key && data?.updateValue) {
56
- data.updateValue(data.key, new Date(val));
57
- }
58
- return true;
59
- }
60
- return false;
61
- },
62
80
  [constants.types.NUMBER]: (val, data) => {
63
81
  if (typeof val === "number" && !isNaN(val)) return true;
64
82
  if (typeof val === "string" && !isNaN(parseFloat(val))) {
@@ -69,10 +87,6 @@ const typeChecks = {
69
87
  }
70
88
  return false;
71
89
  },
72
-
73
- [constants.types.TIME]: (val) =>
74
- typeof val === "string" &&
75
- /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/.test(val),
76
90
  [constants.types.STRING]: (val) => typeof val === "string",
77
91
  [constants.types.OBJECT]: (val) =>
78
92
  typeof val === "object" && val !== null && !Array.isArray(val),
@@ -198,16 +212,7 @@ const validateMetaRules = (
198
212
  const fieldValue = providedValue;
199
213
 
200
214
  const { required = false, nullable = false, options } = field?.meta ?? {};
201
- const relational_interfaces = [
202
- constants.interfaces.FILES,
203
- constants.interfaces.FILE,
204
- constants.interfaces.FILE_IMAGE,
205
- constants.interfaces.MANY_TO_MANY,
206
- constants.interfaces.ONE_TO_MANY,
207
- constants.interfaces.MANY_TO_ONE,
208
- constants.interfaces.MANY_TO_ANY,
209
- constants.interfaces.TRANSLATIONS,
210
- ];
215
+
211
216
  const isRelational = relational_interfaces.includes(field?.meta?.interface);
212
217
 
213
218
  if (
@@ -284,7 +289,7 @@ const validateMetaRules = (
284
289
  constants.interfaces.ONE_TO_MANY,
285
290
  constants.interfaces.MANY_TO_ONE,
286
291
  constants.interfaces.MANY_TO_ANY,
287
- "none"
292
+ "none",
288
293
  ].includes(field?.meta?.interface)
289
294
  ) {
290
295
  const isTranslationChild =
@@ -304,7 +309,6 @@ const validateMetaRules = (
304
309
  if (fPath.includes("delete.") && !isTranslationChild) return;
305
310
  if (fPath.includes("existing.") && !isTranslationChild) return;
306
311
 
307
-
308
312
  const label = formatLabel(node.display_label);
309
313
  const message = error_messages.REQUIRED.replace("{field}", label);
310
314
 
@@ -787,12 +791,20 @@ const validateField = (
787
791
  fieldOptions,
788
792
  language_codes
789
793
  ) => {
790
- if (onlyFormFields == true && (value === undefined || value === null)) return;
794
+ const isRelational = relational_interfaces.includes(field?.meta?.interface);
795
+
796
+ if (
797
+ onlyFormFields == true &&
798
+ isRelational &&
799
+ (value === undefined || value === null)
800
+ )
801
+ return;
791
802
 
792
803
  const { is_m2a_item } = field?.meta;
793
804
  const currentPath = fieldPath
794
805
  ? `${fieldPath}.${field.key.split(".").pop()}`
795
806
  : field.key;
807
+
796
808
  const fieldLabel = formatLabel(field.display_label);
797
809
 
798
810
  validateMetaRules(
@@ -816,15 +828,17 @@ const validateField = (
816
828
  typeChecks[constants.types.OBJECT](value)
817
829
  ) {
818
830
  let itemSchemaId = null;
819
- let childrenToValidate = field.children
831
+ let childrenToValidate = field.children;
820
832
  if (is_m2a_item) {
821
833
  const fieldPath = getM2AItemParentPath(currentPath);
822
834
  if (fieldPath) {
823
835
  itemSchemaId = getValue(formData, fieldPath)?.collection_id;
824
- if(!itemSchemaId){
825
- childrenToValidate = []
826
- }else{
827
- childrenToValidate = field.children.filter((child) => child.schema_id === itemSchemaId)
836
+ if (!itemSchemaId) {
837
+ childrenToValidate = [];
838
+ } else {
839
+ childrenToValidate = field.children.filter(
840
+ (child) => child.schema_id === itemSchemaId
841
+ );
828
842
  }
829
843
  }
830
844
  }
@@ -1011,7 +1025,7 @@ const validate = (data) => {
1011
1025
  };
1012
1026
 
1013
1027
  const addError = (fieldPath, obj, field) => {
1014
- if (byPassKeys.some((key) => fieldPath.startsWith(key))) return;
1028
+ if (byPassKeys.some((key) => fieldPath.includes(key))) return;
1015
1029
  if (!result.errors[fieldPath] && !field?.meta?.hidden) {
1016
1030
  const pathResult = extractRelationalParents(fieldPath);
1017
1031
  if (pathResult) {
@@ -1217,7 +1231,8 @@ const validate = (data) => {
1217
1231
  findDisallowedKeys(formData, fieldOptions, maxLevel).forEach((fieldPath) => {
1218
1232
  if (abortEarly && !result.status) return result;
1219
1233
  const fieldKey = getLastChildKey(fieldPath);
1220
- if (fieldKey && !result.errors[fieldPath]) {
1234
+ const isBypass = byPassKeys?.some((key) => fieldPath?.includes(key));
1235
+ if (fieldKey && !result.errors[fieldPath] && !isBypass) {
1221
1236
  addError(fieldPath, {
1222
1237
  label: formatLabel(fieldKey),
1223
1238
  fieldPath,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nox-validation",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "validate dynamic schema",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,4 +18,4 @@
18
18
  ],
19
19
  "author": "NowOnline Team",
20
20
  "license": "ISC"
21
- }
21
+ }