valgen 5.15.4 → 5.17.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/CHANGELOG.md +23 -1
- package/cjs/rules/type-rules/is-array.js +1 -1
- package/cjs/rules/type-rules/is-bigint.js +1 -1
- package/cjs/rules/type-rules/is-boolean.js +1 -1
- package/cjs/rules/type-rules/is-date.js +130 -65
- package/cjs/rules/type-rules/is-integer.js +1 -1
- package/cjs/rules/type-rules/is-number.js +1 -1
- package/cjs/rules/type-rules/is-object.js +1 -1
- package/cjs/rules/type-rules/is-string.js +1 -1
- package/cjs/rules/type-rules/is-tuple.js +1 -1
- package/cjs/rules/type-rules/is-undefined.js +1 -1
- package/esm/rules/type-rules/is-array.js +1 -1
- package/esm/rules/type-rules/is-bigint.js +1 -1
- package/esm/rules/type-rules/is-boolean.js +1 -1
- package/esm/rules/type-rules/is-date.js +129 -65
- package/esm/rules/type-rules/is-integer.js +1 -1
- package/esm/rules/type-rules/is-number.js +1 -1
- package/esm/rules/type-rules/is-object.js +1 -1
- package/esm/rules/type-rules/is-string.js +1 -1
- package/esm/rules/type-rules/is-tuple.js +1 -1
- package/esm/rules/type-rules/is-undefined.js +1 -1
- package/package.json +1 -1
- package/types/rules/type-rules/is-date.d.ts +12 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
## Changelog
|
|
2
2
|
|
|
3
|
-
### [v5.
|
|
3
|
+
### [v5.17.1](https://github.com/panates/valgen/compare/v5.17.0...v5.17.1) -
|
|
4
|
+
|
|
5
|
+
#### 🪲 Fixes
|
|
6
|
+
|
|
7
|
+
- fix: isDate milliseconds padding @Eray Hanoğlu
|
|
8
|
+
|
|
9
|
+
### [v5.17.0](https://github.com/panates/valgen/compare/v5.16.0...v5.17.0) - 24 July 2025
|
|
10
|
+
|
|
11
|
+
#### 🪲 Fixes
|
|
12
|
+
|
|
13
|
+
- refactor: Refactored isDate and isDateString validation rules @Eray Hanoğlu
|
|
14
|
+
|
|
15
|
+
#### 🛠 Refactoring and Updates
|
|
16
|
+
|
|
17
|
+
- refactor: Refactored isDate and isDateString validation rules @Eray Hanoğlu
|
|
18
|
+
|
|
19
|
+
### [v5.16.0](https://github.com/panates/valgen/compare/v5.15.4...v5.16.0) - 16 July 2025
|
|
20
|
+
|
|
21
|
+
#### 🛠 Refactoring and Updates
|
|
22
|
+
|
|
23
|
+
- refactor: Refactored isDate and isDateString validation rules @Eray Hanoğlu
|
|
24
|
+
|
|
25
|
+
### [v5.15.4](https://github.com/panates/valgen/compare/v5.15.3...v5.15.4) - 11 July 2025
|
|
4
26
|
|
|
5
27
|
#### 🛠 Refactoring and Updates
|
|
6
28
|
|
|
@@ -9,7 +9,7 @@ const index_js_1 = require("../../core/index.js");
|
|
|
9
9
|
*/
|
|
10
10
|
function isArray(itemValidator, options) {
|
|
11
11
|
return (0, index_js_1.validator)('isArray', (input, context, _this) => {
|
|
12
|
-
const coerce = options?.coerce
|
|
12
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
13
13
|
let output = input;
|
|
14
14
|
if (output != null && coerce && !Array.isArray(output))
|
|
15
15
|
output = [output];
|
|
@@ -9,7 +9,7 @@ const index_js_1 = require("../../core/index.js");
|
|
|
9
9
|
*/
|
|
10
10
|
function isBigint(options) {
|
|
11
11
|
return (0, index_js_1.validator)('isBigint', (input, context, _this) => {
|
|
12
|
-
const coerce = options?.coerce
|
|
12
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
13
13
|
if (typeof input === 'bigint')
|
|
14
14
|
return input;
|
|
15
15
|
if ((typeof input === 'number' && !isNaN(input)) ||
|
|
@@ -11,7 +11,7 @@ const FALSE_PATTERN = /^false|f|0|no|n$/i;
|
|
|
11
11
|
*/
|
|
12
12
|
function isBoolean(options) {
|
|
13
13
|
return (0, index_js_1.validator)('isBoolean', (input, context, _this) => {
|
|
14
|
-
const coerce = options?.coerce
|
|
14
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
15
15
|
let output = input;
|
|
16
16
|
if (output != null && typeof output !== 'boolean' && coerce) {
|
|
17
17
|
if (typeof input === 'string') {
|
|
@@ -2,97 +2,162 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isDate = isDate;
|
|
4
4
|
exports.isDateString = isDateString;
|
|
5
|
-
const
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const datefns = tslib_1.__importStar(require("date-fns"));
|
|
6
7
|
const index_js_1 = require("../../core/index.js");
|
|
7
|
-
// noinspection RegExpUnnecessaryNonCapturingGroup
|
|
8
|
-
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,3}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
|
|
9
8
|
/**
|
|
10
|
-
* Validates if value is
|
|
11
|
-
*
|
|
9
|
+
* Validates if value is a "Date" instance or ISO 8601 formatted date string.
|
|
10
|
+
* if a `coerce` option is `true`, converts input value to Date instance
|
|
12
11
|
* @validator isDate
|
|
13
12
|
*/
|
|
14
13
|
function isDate(options) {
|
|
15
|
-
const
|
|
14
|
+
const trim = options?.trim;
|
|
16
15
|
return (0, index_js_1.validator)('isDate', (input, context, _this) => {
|
|
17
|
-
const coerce = options?.coerce
|
|
16
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
18
17
|
let d;
|
|
19
18
|
if (input instanceof Date)
|
|
20
19
|
d = input;
|
|
21
|
-
else if (
|
|
22
|
-
if (typeof input === '
|
|
23
|
-
d = (0, date_fns_1.parseISO)(input);
|
|
24
|
-
}
|
|
25
|
-
else if (typeof input === 'number')
|
|
20
|
+
else if (coerce) {
|
|
21
|
+
if (typeof input === 'number')
|
|
26
22
|
d = new Date(input);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
if (precision === 'month') {
|
|
34
|
-
d.setHours(0, 0, 0, 0);
|
|
35
|
-
d.setDate(1);
|
|
23
|
+
else if (typeof input === 'string') {
|
|
24
|
+
const parsed = coerceDateString(input);
|
|
25
|
+
if (parsed) {
|
|
26
|
+
d = new Date(parsed.value);
|
|
27
|
+
}
|
|
36
28
|
}
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
}
|
|
30
|
+
if (datefns.isValid(d)) {
|
|
31
|
+
setPrecision(d, trim);
|
|
39
32
|
return d;
|
|
40
33
|
}
|
|
41
|
-
context.fail(_this, `Value
|
|
34
|
+
context.fail(_this, `Value is not valid date`, input, {
|
|
42
35
|
...options,
|
|
43
36
|
});
|
|
44
37
|
}, options);
|
|
45
38
|
}
|
|
46
39
|
/**
|
|
47
40
|
* Validates if value is DFS (date formatted string).
|
|
48
|
-
* Converts input value to DFS if coerce option is set to 'true'.
|
|
41
|
+
* Converts input value to DFS if the "coerce" option is set to 'true'.
|
|
49
42
|
* @validator isDateString
|
|
50
43
|
*/
|
|
51
44
|
function isDateString(options) {
|
|
52
|
-
const
|
|
53
|
-
const
|
|
45
|
+
const precisionMin = options?.precisionMin || 'minutes';
|
|
46
|
+
const precisionMax = options?.precisionMax || 'tz';
|
|
47
|
+
const precisionMaxIdx = PRECISION_INDEX[precisionMax] || 9;
|
|
48
|
+
const precisionMinIdx = Math.min(precisionMaxIdx, PRECISION_INDEX[precisionMin] || 6);
|
|
54
49
|
return (0, index_js_1.validator)('isDateString', (input, context, _this) => {
|
|
55
|
-
const coerce = options?.coerce
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (!precision ||
|
|
62
|
-
precision === 'year' ||
|
|
63
|
-
(precision === 'month' && m[2]) ||
|
|
64
|
-
(precision === 'date' && m[2] && m[3]) ||
|
|
65
|
-
(precision === 'time' && m[2] && m[3] && m[4])) {
|
|
66
|
-
if (!coerce)
|
|
67
|
-
return input;
|
|
68
|
-
let s = m[1];
|
|
69
|
-
if (m[2])
|
|
70
|
-
s += '-' + m[2];
|
|
71
|
-
else
|
|
72
|
-
return s;
|
|
73
|
-
if (m[3])
|
|
74
|
-
s += '-' + m[3];
|
|
75
|
-
else
|
|
76
|
-
return s;
|
|
77
|
-
if (trim === 'date' || !m[4])
|
|
78
|
-
return s;
|
|
79
|
-
s += 'T' + m[4].substring(0, 8);
|
|
80
|
-
if (trim === 'time')
|
|
81
|
-
return s;
|
|
82
|
-
if (m[9])
|
|
83
|
-
s += m[9];
|
|
84
|
-
return s;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
50
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
51
|
+
const parsed = coerceDateString(input, options?.trim);
|
|
52
|
+
if (parsed) {
|
|
53
|
+
if (parsed.precision >= precisionMinIdx &&
|
|
54
|
+
parsed.precision <= precisionMaxIdx) {
|
|
55
|
+
return coerce ? parsed.value : input;
|
|
87
56
|
}
|
|
88
57
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
?
|
|
92
|
-
:
|
|
93
|
-
}
|
|
94
|
-
context.fail(_this, `Value must be a valid date string`, input, {
|
|
58
|
+
context.fail(_this, `Value is not valid date string` +
|
|
59
|
+
(options?.precisionMin || options?.precisionMax
|
|
60
|
+
? ` with required precision`
|
|
61
|
+
: ''), input, {
|
|
95
62
|
...options,
|
|
96
63
|
});
|
|
97
64
|
}, options);
|
|
98
65
|
}
|
|
66
|
+
// noinspection RegExpUnnecessaryNonCapturingGroup
|
|
67
|
+
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,3}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
|
|
68
|
+
function coerceDateString(input, precision) {
|
|
69
|
+
let dateParts;
|
|
70
|
+
let outPrecision = 0;
|
|
71
|
+
if (input instanceof Date || typeof input === 'number') {
|
|
72
|
+
const d = typeof input === 'number' ? new Date(input) : input;
|
|
73
|
+
dateParts = [
|
|
74
|
+
String(d.getFullYear()).padStart(4, '0'),
|
|
75
|
+
String(d.getMonth() + 1).padStart(2, '0'),
|
|
76
|
+
String(d.getDate()).padStart(2, '0'),
|
|
77
|
+
String(d.getHours()).padStart(2, '0'),
|
|
78
|
+
String(d.getMinutes()).padStart(2, '0'),
|
|
79
|
+
String(d.getSeconds()).padStart(2, '0'),
|
|
80
|
+
String(d.getMilliseconds()).padStart(3, '0'),
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
else if (typeof input === 'string') {
|
|
84
|
+
const d = datefns.parseISO(input);
|
|
85
|
+
const m = DATE_PATTERN.exec(input);
|
|
86
|
+
if (m && datefns.isValid(d)) {
|
|
87
|
+
m.shift();
|
|
88
|
+
dateParts = m;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (!dateParts)
|
|
92
|
+
return;
|
|
93
|
+
outPrecision = dateParts.findIndex(v => !v);
|
|
94
|
+
if (outPrecision === -1)
|
|
95
|
+
outPrecision = dateParts.length + 1;
|
|
96
|
+
const precisionIndex = (precision ? PRECISION_INDEX[precision] : 9) || 9;
|
|
97
|
+
let value = dateParts[0] || '0000';
|
|
98
|
+
if (precisionIndex > 1)
|
|
99
|
+
value += '-' + (dateParts[1] || '01');
|
|
100
|
+
if (precisionIndex > 2)
|
|
101
|
+
value += '-' + (dateParts[2] || '01');
|
|
102
|
+
if (precisionIndex > 3)
|
|
103
|
+
value += 'T' + (dateParts[3] || '00');
|
|
104
|
+
if (precisionIndex > 4)
|
|
105
|
+
value += ':' + (dateParts[4] || '00');
|
|
106
|
+
if (precisionIndex > 5)
|
|
107
|
+
value += ':' + (dateParts[5] || '00');
|
|
108
|
+
if (precisionIndex > 6)
|
|
109
|
+
value += dateParts[6] ? '.' + dateParts[6] : '';
|
|
110
|
+
if (precisionIndex > 7)
|
|
111
|
+
value += dateParts[7] || '';
|
|
112
|
+
return {
|
|
113
|
+
value,
|
|
114
|
+
precision: outPrecision || 8,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
const PRECISION_INDEX = {
|
|
118
|
+
year: 1,
|
|
119
|
+
yr: 1,
|
|
120
|
+
month: 2,
|
|
121
|
+
mo: 2,
|
|
122
|
+
day: 3,
|
|
123
|
+
d: 4,
|
|
124
|
+
hours: 4,
|
|
125
|
+
hr: 4,
|
|
126
|
+
minutes: 5,
|
|
127
|
+
min: 5,
|
|
128
|
+
seconds: 6,
|
|
129
|
+
sec: 6,
|
|
130
|
+
milliseconds: 7,
|
|
131
|
+
ms: 7,
|
|
132
|
+
tz: 8,
|
|
133
|
+
};
|
|
134
|
+
function setPrecision(d, precision) {
|
|
135
|
+
switch (precision) {
|
|
136
|
+
case 'year': {
|
|
137
|
+
d.setMonth(0, 1);
|
|
138
|
+
d.setHours(0, 0, 0, 0);
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
case 'month': {
|
|
142
|
+
d.setDate(1);
|
|
143
|
+
d.setHours(0, 0, 0, 0);
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
case 'day': {
|
|
147
|
+
d.setHours(0, 0, 0, 0);
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case 'hours': {
|
|
151
|
+
d.setMinutes(0, 0, 0);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case 'minutes': {
|
|
155
|
+
d.setSeconds(0, 0);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case 'seconds': {
|
|
159
|
+
d.setMilliseconds(0);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -9,7 +9,7 @@ const index_js_1 = require("../../core/index.js");
|
|
|
9
9
|
*/
|
|
10
10
|
function isInteger(options) {
|
|
11
11
|
return (0, index_js_1.validator)('isInteger', (input, context, _this) => {
|
|
12
|
-
const coerce = options?.coerce
|
|
12
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
13
13
|
let output = input;
|
|
14
14
|
if (output != null && typeof output !== 'number' && coerce) {
|
|
15
15
|
if (typeof input === 'string')
|
|
@@ -9,7 +9,7 @@ const index_js_1 = require("../../core/index.js");
|
|
|
9
9
|
*/
|
|
10
10
|
function isNumber(options) {
|
|
11
11
|
return (0, index_js_1.validator)('isNumber', (input, context, _this) => {
|
|
12
|
-
const coerce = options?.coerce
|
|
12
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
13
13
|
let output = input;
|
|
14
14
|
if (output != null && typeof output !== 'number' && coerce) {
|
|
15
15
|
if (typeof input === 'string')
|
|
@@ -41,7 +41,7 @@ function isObject(schema, options) {
|
|
|
41
41
|
if (ctor && ctor[constants_js_1.preValidation]) {
|
|
42
42
|
output = ctor[constants_js_1.preValidation](output, context, _this);
|
|
43
43
|
}
|
|
44
|
-
const coerce = options?.coerce
|
|
44
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
45
45
|
if (typeof output === 'string' && coerce)
|
|
46
46
|
output = JSON.parse(output);
|
|
47
47
|
if (!(output && typeof output === 'object')) {
|
|
@@ -13,7 +13,7 @@ const index_js_1 = require("../../core/index.js");
|
|
|
13
13
|
*/
|
|
14
14
|
function isString(options) {
|
|
15
15
|
return (0, index_js_1.validator)('isString', (input, context, _this) => {
|
|
16
|
-
const coerce = options?.coerce
|
|
16
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
17
17
|
let output = input;
|
|
18
18
|
if (output != null && typeof output !== 'string' && coerce) {
|
|
19
19
|
if (typeof output === 'object') {
|
|
@@ -4,7 +4,7 @@ exports.isTuple = isTuple;
|
|
|
4
4
|
const index_js_1 = require("../../core/index.js");
|
|
5
5
|
function isTuple(items, options) {
|
|
6
6
|
return (0, index_js_1.validator)('isTuple', (input, context, _this) => {
|
|
7
|
-
const coerce = options?.coerce
|
|
7
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
8
8
|
let output = input;
|
|
9
9
|
if (output != null && coerce && !Array.isArray(output))
|
|
10
10
|
output = [output];
|
|
@@ -8,7 +8,7 @@ const index_js_1 = require("../../core/index.js");
|
|
|
8
8
|
*/
|
|
9
9
|
function isUndefined(options) {
|
|
10
10
|
return (0, index_js_1.validator)('isUndefined', (input, context, _this) => {
|
|
11
|
-
if (options?.coerce
|
|
11
|
+
if (options?.coerce ?? context.coerce)
|
|
12
12
|
return undefined;
|
|
13
13
|
if (input === undefined)
|
|
14
14
|
return;
|
|
@@ -6,7 +6,7 @@ import { validator, } from '../../core/index.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export function isArray(itemValidator, options) {
|
|
8
8
|
return validator('isArray', (input, context, _this) => {
|
|
9
|
-
const coerce = options?.coerce
|
|
9
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
10
10
|
let output = input;
|
|
11
11
|
if (output != null && coerce && !Array.isArray(output))
|
|
12
12
|
output = [output];
|
|
@@ -6,7 +6,7 @@ import { validator, } from '../../core/index.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export function isBigint(options) {
|
|
8
8
|
return validator('isBigint', (input, context, _this) => {
|
|
9
|
-
const coerce = options?.coerce
|
|
9
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
10
10
|
if (typeof input === 'bigint')
|
|
11
11
|
return input;
|
|
12
12
|
if ((typeof input === 'number' && !isNaN(input)) ||
|
|
@@ -8,7 +8,7 @@ const FALSE_PATTERN = /^false|f|0|no|n$/i;
|
|
|
8
8
|
*/
|
|
9
9
|
export function isBoolean(options) {
|
|
10
10
|
return validator('isBoolean', (input, context, _this) => {
|
|
11
|
-
const coerce = options?.coerce
|
|
11
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
12
12
|
let output = input;
|
|
13
13
|
if (output != null && typeof output !== 'boolean' && coerce) {
|
|
14
14
|
if (typeof input === 'string') {
|
|
@@ -1,94 +1,158 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as datefns from 'date-fns';
|
|
2
2
|
import { validator, } from '../../core/index.js';
|
|
3
|
-
// noinspection RegExpUnnecessaryNonCapturingGroup
|
|
4
|
-
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,3}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
|
|
5
3
|
/**
|
|
6
|
-
* Validates if value is
|
|
7
|
-
*
|
|
4
|
+
* Validates if value is a "Date" instance or ISO 8601 formatted date string.
|
|
5
|
+
* if a `coerce` option is `true`, converts input value to Date instance
|
|
8
6
|
* @validator isDate
|
|
9
7
|
*/
|
|
10
8
|
export function isDate(options) {
|
|
11
|
-
const
|
|
9
|
+
const trim = options?.trim;
|
|
12
10
|
return validator('isDate', (input, context, _this) => {
|
|
13
|
-
const coerce = options?.coerce
|
|
11
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
14
12
|
let d;
|
|
15
13
|
if (input instanceof Date)
|
|
16
14
|
d = input;
|
|
17
|
-
else if (
|
|
18
|
-
if (typeof input === '
|
|
19
|
-
d = parseISO(input);
|
|
20
|
-
}
|
|
21
|
-
else if (typeof input === 'number')
|
|
15
|
+
else if (coerce) {
|
|
16
|
+
if (typeof input === 'number')
|
|
22
17
|
d = new Date(input);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
if (precision === 'month') {
|
|
30
|
-
d.setHours(0, 0, 0, 0);
|
|
31
|
-
d.setDate(1);
|
|
18
|
+
else if (typeof input === 'string') {
|
|
19
|
+
const parsed = coerceDateString(input);
|
|
20
|
+
if (parsed) {
|
|
21
|
+
d = new Date(parsed.value);
|
|
22
|
+
}
|
|
32
23
|
}
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
}
|
|
25
|
+
if (datefns.isValid(d)) {
|
|
26
|
+
setPrecision(d, trim);
|
|
35
27
|
return d;
|
|
36
28
|
}
|
|
37
|
-
context.fail(_this, `Value
|
|
29
|
+
context.fail(_this, `Value is not valid date`, input, {
|
|
38
30
|
...options,
|
|
39
31
|
});
|
|
40
32
|
}, options);
|
|
41
33
|
}
|
|
42
34
|
/**
|
|
43
35
|
* Validates if value is DFS (date formatted string).
|
|
44
|
-
* Converts input value to DFS if coerce option is set to 'true'.
|
|
36
|
+
* Converts input value to DFS if the "coerce" option is set to 'true'.
|
|
45
37
|
* @validator isDateString
|
|
46
38
|
*/
|
|
47
39
|
export function isDateString(options) {
|
|
48
|
-
const
|
|
49
|
-
const
|
|
40
|
+
const precisionMin = options?.precisionMin || 'minutes';
|
|
41
|
+
const precisionMax = options?.precisionMax || 'tz';
|
|
42
|
+
const precisionMaxIdx = PRECISION_INDEX[precisionMax] || 9;
|
|
43
|
+
const precisionMinIdx = Math.min(precisionMaxIdx, PRECISION_INDEX[precisionMin] || 6);
|
|
50
44
|
return validator('isDateString', (input, context, _this) => {
|
|
51
|
-
const coerce = options?.coerce
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (!precision ||
|
|
58
|
-
precision === 'year' ||
|
|
59
|
-
(precision === 'month' && m[2]) ||
|
|
60
|
-
(precision === 'date' && m[2] && m[3]) ||
|
|
61
|
-
(precision === 'time' && m[2] && m[3] && m[4])) {
|
|
62
|
-
if (!coerce)
|
|
63
|
-
return input;
|
|
64
|
-
let s = m[1];
|
|
65
|
-
if (m[2])
|
|
66
|
-
s += '-' + m[2];
|
|
67
|
-
else
|
|
68
|
-
return s;
|
|
69
|
-
if (m[3])
|
|
70
|
-
s += '-' + m[3];
|
|
71
|
-
else
|
|
72
|
-
return s;
|
|
73
|
-
if (trim === 'date' || !m[4])
|
|
74
|
-
return s;
|
|
75
|
-
s += 'T' + m[4].substring(0, 8);
|
|
76
|
-
if (trim === 'time')
|
|
77
|
-
return s;
|
|
78
|
-
if (m[9])
|
|
79
|
-
s += m[9];
|
|
80
|
-
return s;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
45
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
46
|
+
const parsed = coerceDateString(input, options?.trim);
|
|
47
|
+
if (parsed) {
|
|
48
|
+
if (parsed.precision >= precisionMinIdx &&
|
|
49
|
+
parsed.precision <= precisionMaxIdx) {
|
|
50
|
+
return coerce ? parsed.value : input;
|
|
83
51
|
}
|
|
84
52
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
?
|
|
88
|
-
:
|
|
89
|
-
}
|
|
90
|
-
context.fail(_this, `Value must be a valid date string`, input, {
|
|
53
|
+
context.fail(_this, `Value is not valid date string` +
|
|
54
|
+
(options?.precisionMin || options?.precisionMax
|
|
55
|
+
? ` with required precision`
|
|
56
|
+
: ''), input, {
|
|
91
57
|
...options,
|
|
92
58
|
});
|
|
93
59
|
}, options);
|
|
94
60
|
}
|
|
61
|
+
// noinspection RegExpUnnecessaryNonCapturingGroup
|
|
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,3}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
|
|
63
|
+
function coerceDateString(input, precision) {
|
|
64
|
+
let dateParts;
|
|
65
|
+
let outPrecision = 0;
|
|
66
|
+
if (input instanceof Date || typeof input === 'number') {
|
|
67
|
+
const d = typeof input === 'number' ? new Date(input) : input;
|
|
68
|
+
dateParts = [
|
|
69
|
+
String(d.getFullYear()).padStart(4, '0'),
|
|
70
|
+
String(d.getMonth() + 1).padStart(2, '0'),
|
|
71
|
+
String(d.getDate()).padStart(2, '0'),
|
|
72
|
+
String(d.getHours()).padStart(2, '0'),
|
|
73
|
+
String(d.getMinutes()).padStart(2, '0'),
|
|
74
|
+
String(d.getSeconds()).padStart(2, '0'),
|
|
75
|
+
String(d.getMilliseconds()).padStart(3, '0'),
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
else if (typeof input === 'string') {
|
|
79
|
+
const d = datefns.parseISO(input);
|
|
80
|
+
const m = DATE_PATTERN.exec(input);
|
|
81
|
+
if (m && datefns.isValid(d)) {
|
|
82
|
+
m.shift();
|
|
83
|
+
dateParts = m;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!dateParts)
|
|
87
|
+
return;
|
|
88
|
+
outPrecision = dateParts.findIndex(v => !v);
|
|
89
|
+
if (outPrecision === -1)
|
|
90
|
+
outPrecision = dateParts.length + 1;
|
|
91
|
+
const precisionIndex = (precision ? PRECISION_INDEX[precision] : 9) || 9;
|
|
92
|
+
let value = dateParts[0] || '0000';
|
|
93
|
+
if (precisionIndex > 1)
|
|
94
|
+
value += '-' + (dateParts[1] || '01');
|
|
95
|
+
if (precisionIndex > 2)
|
|
96
|
+
value += '-' + (dateParts[2] || '01');
|
|
97
|
+
if (precisionIndex > 3)
|
|
98
|
+
value += 'T' + (dateParts[3] || '00');
|
|
99
|
+
if (precisionIndex > 4)
|
|
100
|
+
value += ':' + (dateParts[4] || '00');
|
|
101
|
+
if (precisionIndex > 5)
|
|
102
|
+
value += ':' + (dateParts[5] || '00');
|
|
103
|
+
if (precisionIndex > 6)
|
|
104
|
+
value += dateParts[6] ? '.' + dateParts[6] : '';
|
|
105
|
+
if (precisionIndex > 7)
|
|
106
|
+
value += dateParts[7] || '';
|
|
107
|
+
return {
|
|
108
|
+
value,
|
|
109
|
+
precision: outPrecision || 8,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
const PRECISION_INDEX = {
|
|
113
|
+
year: 1,
|
|
114
|
+
yr: 1,
|
|
115
|
+
month: 2,
|
|
116
|
+
mo: 2,
|
|
117
|
+
day: 3,
|
|
118
|
+
d: 4,
|
|
119
|
+
hours: 4,
|
|
120
|
+
hr: 4,
|
|
121
|
+
minutes: 5,
|
|
122
|
+
min: 5,
|
|
123
|
+
seconds: 6,
|
|
124
|
+
sec: 6,
|
|
125
|
+
milliseconds: 7,
|
|
126
|
+
ms: 7,
|
|
127
|
+
tz: 8,
|
|
128
|
+
};
|
|
129
|
+
function setPrecision(d, precision) {
|
|
130
|
+
switch (precision) {
|
|
131
|
+
case 'year': {
|
|
132
|
+
d.setMonth(0, 1);
|
|
133
|
+
d.setHours(0, 0, 0, 0);
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
case 'month': {
|
|
137
|
+
d.setDate(1);
|
|
138
|
+
d.setHours(0, 0, 0, 0);
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
case 'day': {
|
|
142
|
+
d.setHours(0, 0, 0, 0);
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
case 'hours': {
|
|
146
|
+
d.setMinutes(0, 0, 0);
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case 'minutes': {
|
|
150
|
+
d.setSeconds(0, 0);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case 'seconds': {
|
|
154
|
+
d.setMilliseconds(0);
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -6,7 +6,7 @@ import { validator, } from '../../core/index.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export function isInteger(options) {
|
|
8
8
|
return validator('isInteger', (input, context, _this) => {
|
|
9
|
-
const coerce = options?.coerce
|
|
9
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
10
10
|
let output = input;
|
|
11
11
|
if (output != null && typeof output !== 'number' && coerce) {
|
|
12
12
|
if (typeof input === 'string')
|
|
@@ -6,7 +6,7 @@ import { validator, } from '../../core/index.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export function isNumber(options) {
|
|
8
8
|
return validator('isNumber', (input, context, _this) => {
|
|
9
|
-
const coerce = options?.coerce
|
|
9
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
10
10
|
let output = input;
|
|
11
11
|
if (output != null && typeof output !== 'number' && coerce) {
|
|
12
12
|
if (typeof input === 'string')
|
|
@@ -38,7 +38,7 @@ export function isObject(schema, options) {
|
|
|
38
38
|
if (ctor && ctor[preValidation]) {
|
|
39
39
|
output = ctor[preValidation](output, context, _this);
|
|
40
40
|
}
|
|
41
|
-
const coerce = options?.coerce
|
|
41
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
42
42
|
if (typeof output === 'string' && coerce)
|
|
43
43
|
output = JSON.parse(output);
|
|
44
44
|
if (!(output && typeof output === 'object')) {
|
|
@@ -6,7 +6,7 @@ import { validator, } from '../../core/index.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export function isString(options) {
|
|
8
8
|
return validator('isString', (input, context, _this) => {
|
|
9
|
-
const coerce = options?.coerce
|
|
9
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
10
10
|
let output = input;
|
|
11
11
|
if (output != null && typeof output !== 'string' && coerce) {
|
|
12
12
|
if (typeof output === 'object') {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
export function isTuple(items, options) {
|
|
3
3
|
return validator('isTuple', (input, context, _this) => {
|
|
4
|
-
const coerce = options?.coerce
|
|
4
|
+
const coerce = options?.coerce ?? context.coerce;
|
|
5
5
|
let output = input;
|
|
6
6
|
if (output != null && coerce && !Array.isArray(output))
|
|
7
7
|
output = [output];
|
|
@@ -5,7 +5,7 @@ import { validator, } from '../../core/index.js';
|
|
|
5
5
|
*/
|
|
6
6
|
export function isUndefined(options) {
|
|
7
7
|
return validator('isUndefined', (input, context, _this) => {
|
|
8
|
-
if (options?.coerce
|
|
8
|
+
if (options?.coerce ?? context.coerce)
|
|
9
9
|
return undefined;
|
|
10
10
|
if (input === undefined)
|
|
11
11
|
return;
|
package/package.json
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
|
+
type Precision = 'year' | 'yr' | 'month' | 'mo' | 'day' | 'd' | 'hours' | 'hr' | 'minutes' | 'min' | 'seconds' | 'sec' | 'milliseconds' | 'ms' | 'tz';
|
|
3
|
+
export interface IsDateOptions extends ValidationOptions {
|
|
4
|
+
trim?: Precision;
|
|
5
|
+
}
|
|
2
6
|
/**
|
|
3
|
-
* Validates if value is
|
|
4
|
-
*
|
|
7
|
+
* Validates if value is a "Date" instance or ISO 8601 formatted date string.
|
|
8
|
+
* if a `coerce` option is `true`, converts input value to Date instance
|
|
5
9
|
* @validator isDate
|
|
6
10
|
*/
|
|
7
|
-
export declare function isDate(options?:
|
|
11
|
+
export declare function isDate(options?: IsDateOptions): import("../../core/validator.js").Validator<Date, string | number | Date, import("../../core/types.js").ExecutionOptions>;
|
|
8
12
|
export interface IsDateStringOptions extends ValidationOptions {
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
precisionMin?: Precision;
|
|
14
|
+
precisionMax?: Precision;
|
|
15
|
+
trim?: Precision;
|
|
11
16
|
}
|
|
12
17
|
/**
|
|
13
18
|
* Validates if value is DFS (date formatted string).
|
|
14
|
-
* Converts input value to DFS if coerce option is set to 'true'.
|
|
19
|
+
* Converts input value to DFS if the "coerce" option is set to 'true'.
|
|
15
20
|
* @validator isDateString
|
|
16
21
|
*/
|
|
17
22
|
export declare function isDateString(options?: IsDateStringOptions): import("../../core/validator.js").Validator<string, string | number | Date, import("../../core/types.js").ExecutionOptions>;
|
|
18
|
-
export
|
|
19
|
-
interface Options extends ValidationOptions {
|
|
20
|
-
precision?: 'year' | 'month' | 'date' | 'time';
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
+
export {};
|