@ciwergrp/nuxid 1.6.5 → 1.6.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/dist/module.json +1 -1
- package/dist/runtime/validator.d.ts +1 -1
- package/dist/runtime/validator.js +71 -5
- package/package.json +1 -1
package/dist/module.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { FormRules } from 'element-plus';
|
|
2
|
-
export type ValidationRule = 'required' | 'string' | 'nullable' | 'email' | 'accepted' | `accepted_if:${string}` | 'boolean' | 'filled' | 'present' | `required_if:${string}` | `required_unless:${string}` | `required_with:${string}` | `required_with_all:${string}` | `required_without:${string}` | `required_without_all:${string}` | 'prohibited' | `prohibited_if:${string}` | `prohibited_unless:${string}` | `prohibits:${string}` | `min:${number}` | `max:${number}` | `between:${number},${number}` | `size:${number}` | `same:${string}` | 'alpha' | 'alpha_num' | 'alpha_dash' | 'alphanumeric_space' | 'alpha_space' | 'numeric' | 'integer' | 'confirmed' | `digits:${number}` | `digits_between:${number},${number}` | 'bail' | `different:${string}` | `gt:${string}` | `gte:${string}` | `lt:${string}` | `lte:${string}` | `starts_with:${string}` | `ends_with:${string}` | `regex:${string}` | `date_format:${string}` | 'date' | `before:${string}` | `after:${string}` | `before_or_equal:${string}` | `after_or_equal:${string}` | 'array' | 'distinct' | `in_array:${string}` | `required_array_keys:${string}` | 'list' | 'url' | 'ip' | 'hex_color' | `in:${string}` | `multiple_of:${number}` | `decimal:${number}` | `decimal:${number},${number}` | 'lowercase' | 'uppercase' | 'uuid' | 'ulid' | `timezone:${string}` | 'timezone';
|
|
2
|
+
export type ValidationRule = 'required' | 'time' | 'string' | 'nullable' | 'email' | 'accepted' | `accepted_if:${string}` | 'boolean' | 'filled' | 'present' | `required_if:${string}` | `required_unless:${string}` | `required_with:${string}` | `required_with_all:${string}` | `required_without:${string}` | `required_without_all:${string}` | 'prohibited' | `prohibited_if:${string}` | `prohibited_unless:${string}` | `prohibits:${string}` | `min:${number}` | `max:${number}` | `between:${number},${number}` | `size:${number}` | `same:${string}` | 'alpha' | 'alpha_num' | 'alpha_dash' | 'alphanumeric_space' | 'alpha_space' | 'numeric' | 'integer' | 'confirmed' | `digits:${number}` | `digits_between:${number},${number}` | 'bail' | `different:${string}` | `gt:${string}` | `gte:${string}` | `lt:${string}` | `lte:${string}` | `starts_with:${string}` | `ends_with:${string}` | `regex:${string}` | `date_format:${string}` | 'date' | `before:${string}` | `after:${string}` | `before_or_equal:${string}` | `after_or_equal:${string}` | 'array' | 'distinct' | `in_array:${string}` | `required_array_keys:${string}` | 'list' | 'url' | 'ip' | 'hex_color' | `in:${string}` | `multiple_of:${number}` | `decimal:${number}` | `decimal:${number},${number}` | 'lowercase' | 'uppercase' | 'uuid' | 'ulid' | `timezone:${string}` | 'timezone';
|
|
3
3
|
export interface ValidationOptions {
|
|
4
4
|
messages?: Record<string, string>;
|
|
5
5
|
attributes?: Record<string, string>;
|
|
@@ -84,6 +84,55 @@ const isAcceptedValue = (value) => {
|
|
|
84
84
|
const isBooleanValue = (value) => {
|
|
85
85
|
return value === true || value === false || value === 0 || value === 1 || value === "0" || value === "1";
|
|
86
86
|
};
|
|
87
|
+
const parseTimeToMinutes = (value) => {
|
|
88
|
+
if (typeof value !== "string") {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
const match = value.match(/^([01]\d|2[0-3]):([0-5]\d)$/);
|
|
92
|
+
if (!match) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
const hours = Number(match[1]);
|
|
96
|
+
const minutes = Number(match[2]);
|
|
97
|
+
return hours * 60 + minutes;
|
|
98
|
+
};
|
|
99
|
+
const parseDateToTimestamp = (value) => {
|
|
100
|
+
if (typeof value !== "string") {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const date = /* @__PURE__ */ new Date(`${value}T00:00:00.000Z`);
|
|
107
|
+
if (Number.isNaN(date.getTime()) || date.toISOString().slice(0, 10) !== value) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
return date.getTime();
|
|
111
|
+
};
|
|
112
|
+
const compareFieldValues = (left, right, compare) => {
|
|
113
|
+
const leftTime = parseTimeToMinutes(left);
|
|
114
|
+
const rightTime = parseTimeToMinutes(right);
|
|
115
|
+
if (leftTime !== null || rightTime !== null) {
|
|
116
|
+
if (leftTime === null || rightTime === null) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
return compare(leftTime, rightTime);
|
|
120
|
+
}
|
|
121
|
+
const leftDate = parseDateToTimestamp(left);
|
|
122
|
+
const rightDate = parseDateToTimestamp(right);
|
|
123
|
+
if (leftDate !== null || rightDate !== null) {
|
|
124
|
+
if (leftDate === null || rightDate === null) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return compare(leftDate, rightDate);
|
|
128
|
+
}
|
|
129
|
+
const leftNumber = Number(left);
|
|
130
|
+
const rightNumber = Number(right);
|
|
131
|
+
if (Number.isNaN(leftNumber) || Number.isNaN(rightNumber)) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
return compare(leftNumber, rightNumber);
|
|
135
|
+
};
|
|
87
136
|
const createDateFormatValidator = (format, message) => {
|
|
88
137
|
const tokenMap = {
|
|
89
138
|
Y: "\\d{4}",
|
|
@@ -119,6 +168,17 @@ const createDateFormatValidator = (format, message) => {
|
|
|
119
168
|
callback();
|
|
120
169
|
};
|
|
121
170
|
};
|
|
171
|
+
const createTimeValidator = (message) => {
|
|
172
|
+
return (_rule, value, callback) => {
|
|
173
|
+
if (isEmptyValue(value)) {
|
|
174
|
+
return callback();
|
|
175
|
+
}
|
|
176
|
+
if (parseTimeToMinutes(value) === null) {
|
|
177
|
+
return callback(new Error(message));
|
|
178
|
+
}
|
|
179
|
+
callback();
|
|
180
|
+
};
|
|
181
|
+
};
|
|
122
182
|
const createPatternValidator = (pattern, message) => {
|
|
123
183
|
return (_rule, value, callback) => {
|
|
124
184
|
if (value && !pattern.test(value)) {
|
|
@@ -148,7 +208,7 @@ const createIntegerValidator = (message) => {
|
|
|
148
208
|
};
|
|
149
209
|
const createComparisonValidator = (otherField, formState, message, compare) => {
|
|
150
210
|
return (_rule, value, callback) => {
|
|
151
|
-
const otherValue = formState
|
|
211
|
+
const otherValue = getNestedValue(formState, otherField);
|
|
152
212
|
if (value && otherValue && !compare(value, otherValue)) {
|
|
153
213
|
callback(new Error(message));
|
|
154
214
|
} else {
|
|
@@ -599,6 +659,12 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
599
659
|
trigger: pickTrigger(fieldName, "blur")
|
|
600
660
|
});
|
|
601
661
|
break;
|
|
662
|
+
case "time":
|
|
663
|
+
fieldRules.push({
|
|
664
|
+
validator: createTimeValidator(getMessage(fieldName, "time", `${attributeName} must be a valid time in HH:mm format`)),
|
|
665
|
+
trigger: pickTrigger(fieldName, "blur")
|
|
666
|
+
});
|
|
667
|
+
break;
|
|
602
668
|
case "nullable":
|
|
603
669
|
break;
|
|
604
670
|
case "email":
|
|
@@ -1144,7 +1210,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
1144
1210
|
}
|
|
1145
1211
|
const gtOtherAttr = attributes[firstParam] || firstParam;
|
|
1146
1212
|
fieldRules.push({
|
|
1147
|
-
validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "gt", `${attributeName} must be greater than ${gtOtherAttr}`), (a, b) =>
|
|
1213
|
+
validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "gt", `${attributeName} must be greater than ${gtOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x > y)),
|
|
1148
1214
|
trigger: pickTrigger(fieldName, "blur")
|
|
1149
1215
|
});
|
|
1150
1216
|
break;
|
|
@@ -1155,7 +1221,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
1155
1221
|
}
|
|
1156
1222
|
const gteOtherAttr = attributes[firstParam] || firstParam;
|
|
1157
1223
|
fieldRules.push({
|
|
1158
|
-
validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "gte", `${attributeName} must be greater than or equal to ${gteOtherAttr}`), (a, b) =>
|
|
1224
|
+
validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "gte", `${attributeName} must be greater than or equal to ${gteOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x >= y)),
|
|
1159
1225
|
trigger: "blur"
|
|
1160
1226
|
});
|
|
1161
1227
|
break;
|
|
@@ -1166,7 +1232,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
1166
1232
|
}
|
|
1167
1233
|
const ltOtherAttr = attributes[firstParam] || firstParam;
|
|
1168
1234
|
fieldRules.push({
|
|
1169
|
-
validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "lt", `${attributeName} must be less than ${ltOtherAttr}`), (a, b) =>
|
|
1235
|
+
validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "lt", `${attributeName} must be less than ${ltOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x < y)),
|
|
1170
1236
|
trigger: "blur"
|
|
1171
1237
|
});
|
|
1172
1238
|
break;
|
|
@@ -1177,7 +1243,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
1177
1243
|
}
|
|
1178
1244
|
const lteOtherAttr = attributes[firstParam] || firstParam;
|
|
1179
1245
|
fieldRules.push({
|
|
1180
|
-
validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "lte", `${attributeName} must be less than or equal to ${lteOtherAttr}`), (a, b) =>
|
|
1246
|
+
validator: createComparisonValidator(firstParam, dataToValidate, getMessage(fieldName, "lte", `${attributeName} must be less than or equal to ${lteOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x <= y)),
|
|
1181
1247
|
trigger: "blur"
|
|
1182
1248
|
});
|
|
1183
1249
|
break;
|