nox-validation 1.5.9 → 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,7 +646,8 @@ 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;
@@ -698,7 +700,8 @@ const validateOperatorRule = (
698
700
  }
699
701
  if (isDateTime) {
700
702
  if (forMessage) {
701
- return value;
703
+ const format = field?.options?.format;
704
+ return formatDate({ date: value, format, timeZone });
702
705
  }
703
706
  // Return UTC timestamp for comparison
704
707
  return parseDateTimeToUTC(value);
@@ -889,7 +892,8 @@ const handleRule = (
889
892
  addError,
890
893
  currentPath,
891
894
  formData,
892
- error_messages
895
+ error_messages,
896
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
893
897
  ) => {
894
898
  const ruleValue = rule?.value;
895
899
  let messageValue = Array.isArray(ruleValue)
@@ -990,7 +994,8 @@ const handleRule = (
990
994
  currentPath,
991
995
  formData,
992
996
  error_messages,
993
- custom_message
997
+ custom_message,
998
+ timeZone
994
999
  );
995
1000
  break;
996
1001
  default:
@@ -1011,7 +1016,8 @@ const validateField = (
1011
1016
  fieldOptions,
1012
1017
  language_codes,
1013
1018
  existingForm,
1014
- getNodes = true
1019
+ getNodes = true,
1020
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
1015
1021
  ) => {
1016
1022
  if (onlyFormFields == true && value === undefined) return;
1017
1023
 
@@ -1073,7 +1079,8 @@ const validateField = (
1073
1079
  fieldOptions,
1074
1080
  language_codes,
1075
1081
  existingForm,
1076
- false
1082
+ false,
1083
+ timeZone
1077
1084
  )
1078
1085
  );
1079
1086
  } else if (field.type === constants.types.ARRAY && Array.isArray(value)) {
@@ -1093,7 +1100,8 @@ const validateField = (
1093
1100
  addError,
1094
1101
  itemPath,
1095
1102
  formData,
1096
- error_messages
1103
+ error_messages,
1104
+ timeZone
1097
1105
  );
1098
1106
  }
1099
1107
 
@@ -1130,7 +1138,8 @@ const validateField = (
1130
1138
  fieldOptions,
1131
1139
  language_codes,
1132
1140
  existingForm,
1133
- false
1141
+ false,
1142
+ timeZone
1134
1143
  )
1135
1144
  );
1136
1145
  });
@@ -1143,7 +1152,8 @@ const validateField = (
1143
1152
  addError,
1144
1153
  currentPath,
1145
1154
  formData,
1146
- error_messages
1155
+ error_messages,
1156
+ timeZone
1147
1157
  )
1148
1158
  ) {
1149
1159
  addError(
@@ -1166,7 +1176,8 @@ const applyValidations = (
1166
1176
  addError,
1167
1177
  currentPath,
1168
1178
  formData,
1169
- error_messages
1179
+ error_messages,
1180
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
1170
1181
  ) => {
1171
1182
  if (
1172
1183
  !field.validations ||
@@ -1185,7 +1196,8 @@ const applyValidations = (
1185
1196
  addError,
1186
1197
  currentPath,
1187
1198
  formData,
1188
- error_messages
1199
+ error_messages,
1200
+ timeZone
1189
1201
  ) === false
1190
1202
  );
1191
1203
  };
@@ -1213,6 +1225,7 @@ const schema = {
1213
1225
  type: constants.types.ARRAY,
1214
1226
  array_type: constants.types.OBJECT_ID,
1215
1227
  },
1228
+ timeZone: { type: constants.types.STRING, array_type: null },
1216
1229
  };
1217
1230
 
1218
1231
  const validate = (data) => {
@@ -1239,6 +1252,7 @@ const validate = (data) => {
1239
1252
  maxLevel,
1240
1253
  onlyFormFields,
1241
1254
  existingForm = {},
1255
+ timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone,
1242
1256
  } = data;
1243
1257
 
1244
1258
  const error_messages =
@@ -1499,7 +1513,8 @@ const validate = (data) => {
1499
1513
  apiVersion,
1500
1514
  fieldOptions,
1501
1515
  data.language_codes,
1502
- existingForm
1516
+ existingForm,
1517
+ timeZone
1503
1518
  );
1504
1519
  });
1505
1520
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nox-validation",
3
- "version": "1.5.9",
3
+ "version": "1.6.0",
4
4
  "description": "validate dynamic schema",
5
5
  "main": "index.js",
6
6
  "scripts": {