nox-validation 1.5.8 → 1.6.0

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/helpers.js CHANGED
@@ -1456,6 +1456,77 @@ const getSelectedNodes = ({
1456
1456
  return result;
1457
1457
  };
1458
1458
 
1459
+ const getParts = (date, timeZone) => {
1460
+ const formatter = new Intl.DateTimeFormat("en-US", {
1461
+ timeZone,
1462
+ year: "numeric",
1463
+ month: "2-digit",
1464
+ day: "2-digit",
1465
+ hour: "2-digit",
1466
+ minute: "2-digit",
1467
+ second: "2-digit",
1468
+ hour12: true,
1469
+ });
1470
+
1471
+ const parts = {};
1472
+ formatter.formatToParts(new Date(date)).forEach(({ type, value }) => {
1473
+ parts[type] = value;
1474
+ });
1475
+
1476
+ return {
1477
+ year: parts.year,
1478
+ month: parts.month,
1479
+ day: parts.day,
1480
+ hour: parts.hour,
1481
+ minute: parts.minute,
1482
+ second: parts.second,
1483
+ dayPeriod: parts.dayPeriod, // AM/PM
1484
+ };
1485
+ };
1486
+
1487
+ const formatDate = ({
1488
+ date,
1489
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone,
1490
+ format,
1491
+ }) => {
1492
+ const formats = [
1493
+ "MM/DD/YYYY h:mm A",
1494
+ "M/D/YYYY h:mm A",
1495
+ "YYYY-MM-DD HH:mm",
1496
+ "DD/MM/YYYY HH:mm",
1497
+ "DD-MM-YYYY HH:mm",
1498
+ "MM/DD/YYYY h:mm:ss A",
1499
+ "DD/MM/YYYY HH:mm:ss",
1500
+ "DD-MM-YYYY HH:mm:ss",
1501
+ "YYYY-MM-DD HH:mm:ss",
1502
+ "YYYY.MM.DD HH:mm:ss",
1503
+ ];
1504
+
1505
+ if (!formats.includes(format)) format = formats[0];
1506
+ if (!date || isNaN(new Date(date).getTime())) {
1507
+ throw new RangeError("Invalid time value");
1508
+ }
1509
+ const dateObj = date instanceof Date ? date : new Date(date);
1510
+ const { year, month, day, hour, minute, second, dayPeriod } = getParts(
1511
+ dateObj,
1512
+ timeZone
1513
+ );
1514
+
1515
+ let hour24 = Number(hour);
1516
+ if (dayPeriod === "PM" && hour24 !== 12) hour24 += 12;
1517
+ if (dayPeriod === "AM" && hour24 === 12) hour24 = 0;
1518
+
1519
+ return format
1520
+ .replace("YYYY", year)
1521
+ .replace("MM", month)
1522
+ .replace("DD", day)
1523
+ .replace("HH", String(hour24).padStart(2, "0"))
1524
+ .replace("h", String(hour24 % 12 || 12))
1525
+ .replace("mm", minute)
1526
+ .replace("ss", second)
1527
+ .replace("A", dayPeriod);
1528
+ };
1529
+
1459
1530
  module.exports = {
1460
1531
  generateModifiedRules,
1461
1532
  getFieldsGroupBySchemaId,
@@ -1478,4 +1549,5 @@ module.exports = {
1478
1549
  getM2AItemParentPath,
1479
1550
  getSelectedNodes,
1480
1551
  remainingItems,
1552
+ formatDate,
1481
1553
  };
package/lib/validate.js CHANGED
@@ -12,6 +12,7 @@ const {
12
12
  getM2AItemParentPath,
13
13
  getSelectedNodes,
14
14
  remainingItems,
15
+ formatDate,
15
16
  } = require("./helpers");
16
17
 
17
18
  const choices = ["radio", "checkboxes", "dropdown_multiple", "dropdown"];
@@ -645,14 +646,17 @@ const validateOperatorRule = (
645
646
  currentPath,
646
647
  formData,
647
648
  error_messages,
648
- custom_message
649
+ custom_message,
650
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
649
651
  ) => {
650
652
  const { isFieldCompare = false } = rule?.options ?? {};
651
653
  let valid = false;
652
654
 
653
655
  const isNumber = field.type === constants.types.NUMBER;
654
656
  const isDate = field.type === constants.types.DATE || field.type === "date";
655
- const isDateTime = field.type === constants.types.DATE_TIME;
657
+ const isDateTime =
658
+ field.type === constants.types.DATE_TIME ||
659
+ field.type === constants.types.TIMESTAMP;
656
660
  const isTime = field.type === constants.types.TIME;
657
661
  const isArray = field.type === constants.types.ARRAY;
658
662
 
@@ -696,7 +700,8 @@ const validateOperatorRule = (
696
700
  }
697
701
  if (isDateTime) {
698
702
  if (forMessage) {
699
- return value;
703
+ const format = field?.options?.format;
704
+ return formatDate({ date: value, format, timeZone });
700
705
  }
701
706
  // Return UTC timestamp for comparison
702
707
  return parseDateTimeToUTC(value);
@@ -887,7 +892,8 @@ const handleRule = (
887
892
  addError,
888
893
  currentPath,
889
894
  formData,
890
- error_messages
895
+ error_messages,
896
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
891
897
  ) => {
892
898
  const ruleValue = rule?.value;
893
899
  let messageValue = Array.isArray(ruleValue)
@@ -988,7 +994,8 @@ const handleRule = (
988
994
  currentPath,
989
995
  formData,
990
996
  error_messages,
991
- custom_message
997
+ custom_message,
998
+ timeZone
992
999
  );
993
1000
  break;
994
1001
  default:
@@ -1009,7 +1016,8 @@ const validateField = (
1009
1016
  fieldOptions,
1010
1017
  language_codes,
1011
1018
  existingForm,
1012
- getNodes = true
1019
+ getNodes = true,
1020
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
1013
1021
  ) => {
1014
1022
  if (onlyFormFields == true && value === undefined) return;
1015
1023
 
@@ -1071,7 +1079,8 @@ const validateField = (
1071
1079
  fieldOptions,
1072
1080
  language_codes,
1073
1081
  existingForm,
1074
- false
1082
+ false,
1083
+ timeZone
1075
1084
  )
1076
1085
  );
1077
1086
  } else if (field.type === constants.types.ARRAY && Array.isArray(value)) {
@@ -1091,7 +1100,8 @@ const validateField = (
1091
1100
  addError,
1092
1101
  itemPath,
1093
1102
  formData,
1094
- error_messages
1103
+ error_messages,
1104
+ timeZone
1095
1105
  );
1096
1106
  }
1097
1107
 
@@ -1128,7 +1138,8 @@ const validateField = (
1128
1138
  fieldOptions,
1129
1139
  language_codes,
1130
1140
  existingForm,
1131
- false
1141
+ false,
1142
+ timeZone
1132
1143
  )
1133
1144
  );
1134
1145
  });
@@ -1141,7 +1152,8 @@ const validateField = (
1141
1152
  addError,
1142
1153
  currentPath,
1143
1154
  formData,
1144
- error_messages
1155
+ error_messages,
1156
+ timeZone
1145
1157
  )
1146
1158
  ) {
1147
1159
  addError(
@@ -1164,7 +1176,8 @@ const applyValidations = (
1164
1176
  addError,
1165
1177
  currentPath,
1166
1178
  formData,
1167
- error_messages
1179
+ error_messages,
1180
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
1168
1181
  ) => {
1169
1182
  if (
1170
1183
  !field.validations ||
@@ -1183,7 +1196,8 @@ const applyValidations = (
1183
1196
  addError,
1184
1197
  currentPath,
1185
1198
  formData,
1186
- error_messages
1199
+ error_messages,
1200
+ timeZone
1187
1201
  ) === false
1188
1202
  );
1189
1203
  };
@@ -1211,6 +1225,7 @@ const schema = {
1211
1225
  type: constants.types.ARRAY,
1212
1226
  array_type: constants.types.OBJECT_ID,
1213
1227
  },
1228
+ timeZone: { type: constants.types.STRING, array_type: null },
1214
1229
  };
1215
1230
 
1216
1231
  const validate = (data) => {
@@ -1237,6 +1252,7 @@ const validate = (data) => {
1237
1252
  maxLevel,
1238
1253
  onlyFormFields,
1239
1254
  existingForm = {},
1255
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone,
1240
1256
  } = data;
1241
1257
 
1242
1258
  const error_messages =
@@ -1497,7 +1513,8 @@ const validate = (data) => {
1497
1513
  apiVersion,
1498
1514
  fieldOptions,
1499
1515
  data.language_codes,
1500
- existingForm
1516
+ existingForm,
1517
+ timeZone
1501
1518
  );
1502
1519
  });
1503
1520
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nox-validation",
3
- "version": "1.5.8",
3
+ "version": "1.6.0",
4
4
  "description": "validate dynamic schema",
5
5
  "main": "index.js",
6
6
  "scripts": {