valgen 5.19.1 → 5.19.3
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/constants.js +1 -1
- package/index.d.ts +1 -0
- package/index.js +6 -2
- package/package.json +1 -1
- package/rules/type-rules/is-date.d.ts +1 -1
- package/rules/type-rules/is-date.js +20 -19
package/constants.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { DatePrecision } from './rules/index.js';
|
|
|
3
3
|
import * as vg from './rules/index.js';
|
|
4
4
|
export * from './constants.js';
|
|
5
5
|
export * from './core/index.js';
|
|
6
|
+
export type { DatePrecision } from './rules/type-rules/is-date.js';
|
|
6
7
|
export type { IsObject } from './rules/type-rules/is-object.js';
|
|
7
8
|
declare const isAlpha: import("./core/validator.js").Validator<string, string, ExecutionOptions>;
|
|
8
9
|
declare const isAlphanumeric: import("./core/validator.js").Validator<string, string, ExecutionOptions>;
|
package/index.js
CHANGED
|
@@ -58,10 +58,14 @@ const toBoolean = vg.isBoolean({ coerce: true });
|
|
|
58
58
|
const toDate = vg.isDate({ coerce: true });
|
|
59
59
|
const toDateStringValidators = new Map();
|
|
60
60
|
const toDateString = (input, options, ctx) => {
|
|
61
|
-
const trim = options?.trim ?? '
|
|
61
|
+
const trim = options?.trim ?? 'ms';
|
|
62
62
|
let validator = toDateStringValidators.get(trim);
|
|
63
63
|
if (!validator) {
|
|
64
|
-
validator = vg.isDateString({
|
|
64
|
+
validator = vg.isDateString({
|
|
65
|
+
coerce: true,
|
|
66
|
+
precisionMax: trim,
|
|
67
|
+
trim: true,
|
|
68
|
+
});
|
|
65
69
|
toDateStringValidators.set(trim, validator);
|
|
66
70
|
}
|
|
67
71
|
return validator(input, options, ctx);
|
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@ export declare function isDate(options?: IsDateOptions): import("../../core/vali
|
|
|
12
12
|
export interface IsDateStringOptions extends ValidationOptions {
|
|
13
13
|
precisionMin?: DatePrecision;
|
|
14
14
|
precisionMax?: DatePrecision;
|
|
15
|
-
trim?:
|
|
15
|
+
trim?: boolean;
|
|
16
16
|
timeZone?: boolean | number;
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
@@ -37,33 +37,32 @@ export function isDate(options) {
|
|
|
37
37
|
* @validator isDateString
|
|
38
38
|
*/
|
|
39
39
|
export function isDateString(options) {
|
|
40
|
-
const
|
|
41
|
-
const precisionMin = options?.precisionMin || options?.trim || 'minutes';
|
|
40
|
+
const trim = options?.trim;
|
|
42
41
|
const precisionMax = options?.precisionMax || 'tz';
|
|
43
|
-
const precisionMaxIdx = PRECISION_INDEX[precisionMax] ||
|
|
44
|
-
|
|
42
|
+
const precisionMaxIdx = PRECISION_INDEX[precisionMax] || 8;
|
|
43
|
+
let precisionMin = options?.precisionMin || 'minutes';
|
|
44
|
+
const precisionMinIdx = Math.min(precisionMaxIdx, PRECISION_INDEX[precisionMin] || 6);
|
|
45
|
+
precisionMin = (PRECISION_INDEX_KEYS[PRECISION_INDEX_VALUES.indexOf(precisionMinIdx)] || precisionMin);
|
|
45
46
|
return validator('isDateString', (input, context, _this) => {
|
|
46
47
|
const coerce = options?.coerce ?? context.coerce;
|
|
47
|
-
const parsed = coerceDateString(input,
|
|
48
|
+
const parsed = coerceDateString(input, trim ? precisionMax : undefined);
|
|
48
49
|
if (parsed) {
|
|
49
|
-
if (parsed.precision
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
if (parsed.precision < precisionMinIdx) {
|
|
51
|
+
context.fail(_this, `Minimum date precision should be ${precisionMin}`, input, options);
|
|
52
|
+
}
|
|
53
|
+
if (parsed.precision > precisionMaxIdx) {
|
|
54
|
+
context.fail(_this, `Maximum date precision should be ${precisionMax}`, input, options);
|
|
52
55
|
}
|
|
56
|
+
return coerce ? parsed.value : input;
|
|
53
57
|
}
|
|
54
|
-
context.fail(_this, `Value is not valid date string
|
|
55
|
-
(options?.precisionMin || options?.precisionMax
|
|
56
|
-
? ` with required precision`
|
|
57
|
-
: ''), input, {
|
|
58
|
-
...options,
|
|
59
|
-
});
|
|
58
|
+
context.fail(_this, `Value "${input}" is not a valid date string`, input, options);
|
|
60
59
|
}, options);
|
|
61
60
|
}
|
|
62
61
|
// noinspection RegExpUnnecessaryNonCapturingGroup
|
|
63
62
|
const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[T ](?:([0-1][0-9]|2[0-4]):([0-5][0-9])(?::([0-5][0-9]))?(?:\.(\d{0,6}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
|
|
64
63
|
const DATE_PATTERN2 = /^(\d{4,14})(?:\.(\d{1,6}))?(?:(?:([+-])(0[0-9]|1[0-2])(\d{2})?)|Z)?$/;
|
|
65
|
-
function coerceDateString(input,
|
|
66
|
-
const precisionIndex = (
|
|
64
|
+
function coerceDateString(input, trimPrecision) {
|
|
65
|
+
const precisionIndex = (trimPrecision ? PRECISION_INDEX[trimPrecision] : 9) || 9;
|
|
67
66
|
let dateParts;
|
|
68
67
|
let detectedPrecision = 0;
|
|
69
68
|
if (input instanceof Date || typeof input === 'number') {
|
|
@@ -114,8 +113,8 @@ function coerceDateString(input, precision) {
|
|
|
114
113
|
}
|
|
115
114
|
if (!dateParts)
|
|
116
115
|
return;
|
|
117
|
-
detectedPrecision = dateParts.findIndex(v => !v);
|
|
118
|
-
if (detectedPrecision
|
|
116
|
+
detectedPrecision = dateParts[7] ? 8 : dateParts.findIndex(v => !v);
|
|
117
|
+
if (detectedPrecision < 0)
|
|
119
118
|
detectedPrecision = Math.min(dateParts.length, 8);
|
|
120
119
|
let value = dateParts[0] || '0000';
|
|
121
120
|
if (precisionIndex > 1)
|
|
@@ -134,7 +133,7 @@ function coerceDateString(input, precision) {
|
|
|
134
133
|
value += dateParts[7] || '';
|
|
135
134
|
return {
|
|
136
135
|
value,
|
|
137
|
-
precision: detectedPrecision
|
|
136
|
+
precision: Math.min(precisionIndex, detectedPrecision),
|
|
138
137
|
};
|
|
139
138
|
}
|
|
140
139
|
const PRECISION_INDEX = {
|
|
@@ -154,6 +153,8 @@ const PRECISION_INDEX = {
|
|
|
154
153
|
ms: 7,
|
|
155
154
|
tz: 8,
|
|
156
155
|
};
|
|
156
|
+
const PRECISION_INDEX_KEYS = Object.keys(PRECISION_INDEX);
|
|
157
|
+
const PRECISION_INDEX_VALUES = Object.values(PRECISION_INDEX);
|
|
157
158
|
function setPrecision(d, precision) {
|
|
158
159
|
switch (precision) {
|
|
159
160
|
case 'year': {
|