valgen 5.15.3 → 5.17.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/CHANGELOG.md +23 -1
- package/cjs/index.js +68 -64
- 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 +148 -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/index.js +34 -32
- 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 +147 -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 +2 -1
- package/types/index.d.cts +34 -32
- package/types/index.d.ts +34 -32
- 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.0](https://github.com/panates/valgen/compare/v5.16.0...v5.17.0) -
|
|
4
|
+
|
|
5
|
+
#### 🪲 Fixes
|
|
6
|
+
|
|
7
|
+
- refactor: Refactored isDate and isDateString validation rules @Eray Hanoğlu
|
|
8
|
+
|
|
9
|
+
#### 🛠 Refactoring and Updates
|
|
10
|
+
|
|
11
|
+
- refactor: Refactored isDate and isDateString validation rules @Eray Hanoğlu
|
|
12
|
+
|
|
13
|
+
### [v5.16.0](https://github.com/panates/valgen/compare/v5.15.4...v5.16.0) - 16 July 2025
|
|
14
|
+
|
|
15
|
+
#### 🛠 Refactoring and Updates
|
|
16
|
+
|
|
17
|
+
- refactor: Refactored isDate and isDateString validation rules @Eray Hanoğlu
|
|
18
|
+
|
|
19
|
+
### [v5.15.4](https://github.com/panates/valgen/compare/v5.15.3...v5.15.4) - 11 July 2025
|
|
20
|
+
|
|
21
|
+
#### 🛠 Refactoring and Updates
|
|
22
|
+
|
|
23
|
+
- refactor: Exported default isTime rule @Eray Hanoğlu
|
|
24
|
+
|
|
25
|
+
### [v5.15.3](https://github.com/panates/valgen/compare/v5.15.2...v5.15.3) - 20 June 2025
|
|
4
26
|
|
|
5
27
|
#### 🚀 New Features
|
|
6
28
|
|
package/cjs/index.js
CHANGED
|
@@ -1,50 +1,102 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.vg = exports.toString = exports.toNumber = exports.toInteger = exports.toDateString = exports.toDate = exports.toBoolean = exports.toBigint = exports.toArray = void 0;
|
|
3
|
+
exports.isUUID4 = exports.isUUID3 = exports.isUUID2 = exports.isUUID1 = exports.isUUID = exports.isURL = exports.isUppercase = exports.isUndefined = exports.isTime = exports.isSWIFT = exports.isString = exports.isPort = exports.isObjectId = exports.isObject = exports.isNumber = exports.isNullish = exports.isNull = exports.isNotNullish = exports.isNotNull = exports.isNotEmpty = exports.isMobilePhone = exports.isMACAddress = exports.isLowercase = exports.isJWT = exports.isISSN = exports.isIPRange = exports.isIP = exports.isInteger = exports.isIBAN = exports.isHexColor = exports.isHex = exports.isFQDN = exports.isETHAddress = exports.isEmpty = exports.isEmail = exports.isEAN = exports.isDefined = exports.isDecimal = exports.isDateString = exports.isDate = exports.isCreditCard = exports.isBtcAddress = exports.isBoolean = exports.isBigint = exports.isBase64 = exports.isAscii = exports.isArray = exports.isAny = exports.isAlphanumeric = exports.isAlpha = void 0;
|
|
4
|
+
exports.vg = exports.toTime = exports.toString = exports.toNumber = exports.toInteger = exports.toDateString = exports.toDate = exports.toBoolean = exports.toBigint = exports.toArray = exports.isUUID5 = void 0;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const vg = tslib_1.__importStar(require("./rules/index.js"));
|
|
7
7
|
exports.vg = vg;
|
|
8
8
|
tslib_1.__exportStar(require("./constants.js"), exports);
|
|
9
9
|
tslib_1.__exportStar(require("./core/index.js"), exports);
|
|
10
|
+
const isAlpha = vg.isAlpha();
|
|
11
|
+
exports.isAlpha = isAlpha;
|
|
12
|
+
const isAlphanumeric = vg.isAlphanumeric();
|
|
13
|
+
exports.isAlphanumeric = isAlphanumeric;
|
|
10
14
|
const isAny = vg.isAny();
|
|
11
15
|
exports.isAny = isAny;
|
|
12
16
|
const isArray = vg.isArray();
|
|
13
17
|
exports.isArray = isArray;
|
|
18
|
+
const isAscii = vg.isAscii();
|
|
19
|
+
exports.isAscii = isAscii;
|
|
20
|
+
const isBase64 = vg.isBase64();
|
|
21
|
+
exports.isBase64 = isBase64;
|
|
14
22
|
const isBigint = vg.isBigint();
|
|
15
23
|
exports.isBigint = isBigint;
|
|
16
24
|
const isBoolean = vg.isBoolean();
|
|
17
25
|
exports.isBoolean = isBoolean;
|
|
26
|
+
const isBtcAddress = vg.isBtcAddress();
|
|
27
|
+
exports.isBtcAddress = isBtcAddress;
|
|
28
|
+
const isCreditCard = vg.isCreditCard();
|
|
29
|
+
exports.isCreditCard = isCreditCard;
|
|
18
30
|
const isDate = vg.isDate();
|
|
19
31
|
exports.isDate = isDate;
|
|
20
32
|
const isDateString = vg.isDateString();
|
|
21
33
|
exports.isDateString = isDateString;
|
|
34
|
+
const isDecimal = vg.isDecimal();
|
|
35
|
+
exports.isDecimal = isDecimal;
|
|
36
|
+
const isDefined = vg.isDefined();
|
|
37
|
+
exports.isDefined = isDefined;
|
|
38
|
+
const isEAN = vg.isEAN();
|
|
39
|
+
exports.isEAN = isEAN;
|
|
40
|
+
const isEmail = vg.isEmail();
|
|
41
|
+
exports.isEmail = isEmail;
|
|
22
42
|
const isEmpty = vg.isEmpty();
|
|
23
43
|
exports.isEmpty = isEmpty;
|
|
24
|
-
const
|
|
25
|
-
exports.
|
|
44
|
+
const isETHAddress = vg.isETHAddress();
|
|
45
|
+
exports.isETHAddress = isETHAddress;
|
|
46
|
+
const isFQDN = vg.isFQDN();
|
|
47
|
+
exports.isFQDN = isFQDN;
|
|
48
|
+
const isHex = vg.isHex();
|
|
49
|
+
exports.isHex = isHex;
|
|
50
|
+
const isHexColor = vg.isHexColor();
|
|
51
|
+
exports.isHexColor = isHexColor;
|
|
52
|
+
const isIBAN = vg.isIBAN();
|
|
53
|
+
exports.isIBAN = isIBAN;
|
|
26
54
|
const isInteger = vg.isInteger();
|
|
27
55
|
exports.isInteger = isInteger;
|
|
28
|
-
const
|
|
29
|
-
exports.
|
|
56
|
+
const isIP = vg.isIP();
|
|
57
|
+
exports.isIP = isIP;
|
|
58
|
+
const isIPRange = vg.isIPRange();
|
|
59
|
+
exports.isIPRange = isIPRange;
|
|
60
|
+
const isISSN = vg.isISSN();
|
|
61
|
+
exports.isISSN = isISSN;
|
|
62
|
+
const isJWT = vg.isJWT();
|
|
63
|
+
exports.isJWT = isJWT;
|
|
64
|
+
const isLowercase = vg.isLowercase();
|
|
65
|
+
exports.isLowercase = isLowercase;
|
|
66
|
+
const isMACAddress = vg.isMACAddress();
|
|
67
|
+
exports.isMACAddress = isMACAddress;
|
|
68
|
+
const isMobilePhone = vg.isMobilePhone();
|
|
69
|
+
exports.isMobilePhone = isMobilePhone;
|
|
70
|
+
const isNotEmpty = vg.isNotEmpty();
|
|
71
|
+
exports.isNotEmpty = isNotEmpty;
|
|
30
72
|
const isNotNull = vg.isNotNull();
|
|
31
73
|
exports.isNotNull = isNotNull;
|
|
32
|
-
const isNullish = vg.isNullish();
|
|
33
|
-
exports.isNullish = isNullish;
|
|
34
74
|
const isNotNullish = vg.isNotNullish();
|
|
35
75
|
exports.isNotNullish = isNotNullish;
|
|
36
|
-
const
|
|
37
|
-
exports.
|
|
38
|
-
const
|
|
39
|
-
exports.
|
|
76
|
+
const isNull = vg.isNull();
|
|
77
|
+
exports.isNull = isNull;
|
|
78
|
+
const isNullish = vg.isNullish();
|
|
79
|
+
exports.isNullish = isNullish;
|
|
40
80
|
const isNumber = vg.isNumber();
|
|
41
81
|
exports.isNumber = isNumber;
|
|
42
82
|
const isObject = vg.isObject();
|
|
43
83
|
exports.isObject = isObject;
|
|
44
84
|
const isObjectId = vg.isObjectId();
|
|
45
85
|
exports.isObjectId = isObjectId;
|
|
86
|
+
const isPort = vg.isPort();
|
|
87
|
+
exports.isPort = isPort;
|
|
46
88
|
const isString = vg.isString();
|
|
47
89
|
exports.isString = isString;
|
|
90
|
+
const isSWIFT = vg.isSWIFT();
|
|
91
|
+
exports.isSWIFT = isSWIFT;
|
|
92
|
+
const isTime = vg.isTime();
|
|
93
|
+
exports.isTime = isTime;
|
|
94
|
+
const isUndefined = vg.isUndefined();
|
|
95
|
+
exports.isUndefined = isUndefined;
|
|
96
|
+
const isUppercase = vg.isUppercase();
|
|
97
|
+
exports.isUppercase = isUppercase;
|
|
98
|
+
const isURL = vg.isURL();
|
|
99
|
+
exports.isURL = isURL;
|
|
48
100
|
const isUUID = vg.isUUID();
|
|
49
101
|
exports.isUUID = isUUID;
|
|
50
102
|
const isUUID1 = vg.isUUID(1);
|
|
@@ -57,64 +109,14 @@ const isUUID4 = vg.isUUID(4);
|
|
|
57
109
|
exports.isUUID4 = isUUID4;
|
|
58
110
|
const isUUID5 = vg.isUUID(5);
|
|
59
111
|
exports.isUUID5 = isUUID5;
|
|
60
|
-
const isEmail = vg.isEmail();
|
|
61
|
-
exports.isEmail = isEmail;
|
|
62
|
-
const isMobilePhone = vg.isMobilePhone();
|
|
63
|
-
exports.isMobilePhone = isMobilePhone;
|
|
64
|
-
const isIP = vg.isIP();
|
|
65
|
-
exports.isIP = isIP;
|
|
66
|
-
const isIPRange = vg.isIPRange();
|
|
67
|
-
exports.isIPRange = isIPRange;
|
|
68
|
-
const isMACAddress = vg.isMACAddress();
|
|
69
|
-
exports.isMACAddress = isMACAddress;
|
|
70
|
-
const isPort = vg.isPort();
|
|
71
|
-
exports.isPort = isPort;
|
|
72
|
-
const isURL = vg.isURL();
|
|
73
|
-
exports.isURL = isURL;
|
|
74
|
-
const isBase64 = vg.isBase64();
|
|
75
|
-
exports.isBase64 = isBase64;
|
|
76
|
-
const isSWIFT = vg.isSWIFT();
|
|
77
|
-
exports.isSWIFT = isSWIFT;
|
|
78
|
-
const isCreditCard = vg.isCreditCard();
|
|
79
|
-
exports.isCreditCard = isCreditCard;
|
|
80
|
-
const isIBAN = vg.isIBAN();
|
|
81
|
-
exports.isIBAN = isIBAN;
|
|
82
|
-
const isEAN = vg.isEAN();
|
|
83
|
-
exports.isEAN = isEAN;
|
|
84
|
-
const isFQDN = vg.isFQDN();
|
|
85
|
-
exports.isFQDN = isFQDN;
|
|
86
|
-
const isISSN = vg.isISSN();
|
|
87
|
-
exports.isISSN = isISSN;
|
|
88
|
-
const isBtcAddress = vg.isBtcAddress();
|
|
89
|
-
exports.isBtcAddress = isBtcAddress;
|
|
90
|
-
const isETHAddress = vg.isETHAddress();
|
|
91
|
-
exports.isETHAddress = isETHAddress;
|
|
92
|
-
const isHexColor = vg.isHexColor();
|
|
93
|
-
exports.isHexColor = isHexColor;
|
|
94
|
-
const isJWT = vg.isJWT();
|
|
95
|
-
exports.isJWT = isJWT;
|
|
96
|
-
const isLowercase = vg.isLowercase();
|
|
97
|
-
exports.isLowercase = isLowercase;
|
|
98
|
-
const isUppercase = vg.isUppercase();
|
|
99
|
-
exports.isUppercase = isUppercase;
|
|
100
|
-
const isAlpha = vg.isAlpha();
|
|
101
|
-
exports.isAlpha = isAlpha;
|
|
102
|
-
const isAlphanumeric = vg.isAlphanumeric();
|
|
103
|
-
exports.isAlphanumeric = isAlphanumeric;
|
|
104
|
-
const isAscii = vg.isAscii();
|
|
105
|
-
exports.isAscii = isAscii;
|
|
106
|
-
const isDecimal = vg.isDecimal();
|
|
107
|
-
exports.isDecimal = isDecimal;
|
|
108
|
-
const isHex = vg.isHex();
|
|
109
|
-
exports.isHex = isHex;
|
|
110
112
|
const toArray = vg.isArray(isAny, { coerce: true });
|
|
111
113
|
exports.toArray = toArray;
|
|
114
|
+
const toBigint = vg.isBigint({ coerce: true });
|
|
115
|
+
exports.toBigint = toBigint;
|
|
112
116
|
const toBoolean = vg.isBoolean({ coerce: true });
|
|
113
117
|
exports.toBoolean = toBoolean;
|
|
114
118
|
const toDate = vg.isDate({ coerce: true });
|
|
115
119
|
exports.toDate = toDate;
|
|
116
|
-
const toBigint = vg.isBigint({ coerce: true });
|
|
117
|
-
exports.toBigint = toBigint;
|
|
118
120
|
const toDateString = vg.isDateString({ coerce: true });
|
|
119
121
|
exports.toDateString = toDateString;
|
|
120
122
|
const toInteger = vg.isInteger({ coerce: true });
|
|
@@ -123,3 +125,5 @@ const toNumber = vg.isNumber({ coerce: true });
|
|
|
123
125
|
exports.toNumber = toNumber;
|
|
124
126
|
const toString = vg.isString({ coerce: true });
|
|
125
127
|
exports.toString = toString;
|
|
128
|
+
const toTime = vg.isTime({ coerce: true });
|
|
129
|
+
exports.toTime = toTime;
|
|
@@ -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,180 @@
|
|
|
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
|
+
padString(d.getFullYear(), 4),
|
|
75
|
+
padString(d.getMonth() + 1, 2),
|
|
76
|
+
padString(d.getDate(), 2),
|
|
77
|
+
padString(d.getHours(), 2),
|
|
78
|
+
padString(d.getMinutes(), 2),
|
|
79
|
+
padString(d.getSeconds(), 2),
|
|
80
|
+
String(d.getMilliseconds()),
|
|
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
|
+
/**
|
|
135
|
+
* Pads a string to the specified length
|
|
136
|
+
* @param value - The string to pad
|
|
137
|
+
* @param len - The desired total length of the resulting string
|
|
138
|
+
* @param padChar - The character to pad with (defaults to '0')
|
|
139
|
+
* @returns The padded string
|
|
140
|
+
*/
|
|
141
|
+
function padString(value, len, padChar = '0') {
|
|
142
|
+
// Convert value to string if it's a number
|
|
143
|
+
const str = String(value);
|
|
144
|
+
// Return original string if it's already longer or equal to target length
|
|
145
|
+
if (str.length >= len) {
|
|
146
|
+
return str;
|
|
147
|
+
}
|
|
148
|
+
// Create padding and concatenate with original string
|
|
149
|
+
const padding = padChar.repeat(len - str.length);
|
|
150
|
+
return padding + str;
|
|
151
|
+
}
|
|
152
|
+
function setPrecision(d, precision) {
|
|
153
|
+
switch (precision) {
|
|
154
|
+
case 'year': {
|
|
155
|
+
d.setMonth(0, 1);
|
|
156
|
+
d.setHours(0, 0, 0, 0);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
case 'month': {
|
|
160
|
+
d.setDate(1);
|
|
161
|
+
d.setHours(0, 0, 0, 0);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
case 'day': {
|
|
165
|
+
d.setHours(0, 0, 0, 0);
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case 'hours': {
|
|
169
|
+
d.setMinutes(0, 0, 0);
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
case 'minutes': {
|
|
173
|
+
d.setSeconds(0, 0);
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
case 'seconds': {
|
|
177
|
+
d.setMilliseconds(0);
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -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;
|
package/esm/index.js
CHANGED
|
@@ -1,62 +1,64 @@
|
|
|
1
1
|
import * as vg from './rules/index.js';
|
|
2
2
|
export * from './constants.js';
|
|
3
3
|
export * from './core/index.js';
|
|
4
|
+
const isAlpha = vg.isAlpha();
|
|
5
|
+
const isAlphanumeric = vg.isAlphanumeric();
|
|
4
6
|
const isAny = vg.isAny();
|
|
5
7
|
const isArray = vg.isArray();
|
|
8
|
+
const isAscii = vg.isAscii();
|
|
9
|
+
const isBase64 = vg.isBase64();
|
|
6
10
|
const isBigint = vg.isBigint();
|
|
7
11
|
const isBoolean = vg.isBoolean();
|
|
12
|
+
const isBtcAddress = vg.isBtcAddress();
|
|
13
|
+
const isCreditCard = vg.isCreditCard();
|
|
8
14
|
const isDate = vg.isDate();
|
|
9
15
|
const isDateString = vg.isDateString();
|
|
16
|
+
const isDecimal = vg.isDecimal();
|
|
17
|
+
const isDefined = vg.isDefined();
|
|
18
|
+
const isEAN = vg.isEAN();
|
|
19
|
+
const isEmail = vg.isEmail();
|
|
10
20
|
const isEmpty = vg.isEmpty();
|
|
11
|
-
const
|
|
21
|
+
const isETHAddress = vg.isETHAddress();
|
|
22
|
+
const isFQDN = vg.isFQDN();
|
|
23
|
+
const isHex = vg.isHex();
|
|
24
|
+
const isHexColor = vg.isHexColor();
|
|
25
|
+
const isIBAN = vg.isIBAN();
|
|
12
26
|
const isInteger = vg.isInteger();
|
|
13
|
-
const
|
|
27
|
+
const isIP = vg.isIP();
|
|
28
|
+
const isIPRange = vg.isIPRange();
|
|
29
|
+
const isISSN = vg.isISSN();
|
|
30
|
+
const isJWT = vg.isJWT();
|
|
31
|
+
const isLowercase = vg.isLowercase();
|
|
32
|
+
const isMACAddress = vg.isMACAddress();
|
|
33
|
+
const isMobilePhone = vg.isMobilePhone();
|
|
34
|
+
const isNotEmpty = vg.isNotEmpty();
|
|
14
35
|
const isNotNull = vg.isNotNull();
|
|
15
|
-
const isNullish = vg.isNullish();
|
|
16
36
|
const isNotNullish = vg.isNotNullish();
|
|
17
|
-
const
|
|
18
|
-
const
|
|
37
|
+
const isNull = vg.isNull();
|
|
38
|
+
const isNullish = vg.isNullish();
|
|
19
39
|
const isNumber = vg.isNumber();
|
|
20
40
|
const isObject = vg.isObject();
|
|
21
41
|
const isObjectId = vg.isObjectId();
|
|
42
|
+
const isPort = vg.isPort();
|
|
22
43
|
const isString = vg.isString();
|
|
44
|
+
const isSWIFT = vg.isSWIFT();
|
|
45
|
+
const isTime = vg.isTime();
|
|
46
|
+
const isUndefined = vg.isUndefined();
|
|
47
|
+
const isUppercase = vg.isUppercase();
|
|
48
|
+
const isURL = vg.isURL();
|
|
23
49
|
const isUUID = vg.isUUID();
|
|
24
50
|
const isUUID1 = vg.isUUID(1);
|
|
25
51
|
const isUUID2 = vg.isUUID(2);
|
|
26
52
|
const isUUID3 = vg.isUUID(3);
|
|
27
53
|
const isUUID4 = vg.isUUID(4);
|
|
28
54
|
const isUUID5 = vg.isUUID(5);
|
|
29
|
-
const isEmail = vg.isEmail();
|
|
30
|
-
const isMobilePhone = vg.isMobilePhone();
|
|
31
|
-
const isIP = vg.isIP();
|
|
32
|
-
const isIPRange = vg.isIPRange();
|
|
33
|
-
const isMACAddress = vg.isMACAddress();
|
|
34
|
-
const isPort = vg.isPort();
|
|
35
|
-
const isURL = vg.isURL();
|
|
36
|
-
const isBase64 = vg.isBase64();
|
|
37
|
-
const isSWIFT = vg.isSWIFT();
|
|
38
|
-
const isCreditCard = vg.isCreditCard();
|
|
39
|
-
const isIBAN = vg.isIBAN();
|
|
40
|
-
const isEAN = vg.isEAN();
|
|
41
|
-
const isFQDN = vg.isFQDN();
|
|
42
|
-
const isISSN = vg.isISSN();
|
|
43
|
-
const isBtcAddress = vg.isBtcAddress();
|
|
44
|
-
const isETHAddress = vg.isETHAddress();
|
|
45
|
-
const isHexColor = vg.isHexColor();
|
|
46
|
-
const isJWT = vg.isJWT();
|
|
47
|
-
const isLowercase = vg.isLowercase();
|
|
48
|
-
const isUppercase = vg.isUppercase();
|
|
49
|
-
const isAlpha = vg.isAlpha();
|
|
50
|
-
const isAlphanumeric = vg.isAlphanumeric();
|
|
51
|
-
const isAscii = vg.isAscii();
|
|
52
|
-
const isDecimal = vg.isDecimal();
|
|
53
|
-
const isHex = vg.isHex();
|
|
54
55
|
const toArray = vg.isArray(isAny, { coerce: true });
|
|
56
|
+
const toBigint = vg.isBigint({ coerce: true });
|
|
55
57
|
const toBoolean = vg.isBoolean({ coerce: true });
|
|
56
58
|
const toDate = vg.isDate({ coerce: true });
|
|
57
|
-
const toBigint = vg.isBigint({ coerce: true });
|
|
58
59
|
const toDateString = vg.isDateString({ coerce: true });
|
|
59
60
|
const toInteger = vg.isInteger({ coerce: true });
|
|
60
61
|
const toNumber = vg.isNumber({ coerce: true });
|
|
61
62
|
const toString = vg.isString({ coerce: true });
|
|
62
|
-
|
|
63
|
+
const toTime = vg.isTime({ coerce: true });
|
|
64
|
+
export { isAlpha, isAlphanumeric, isAny, isArray, isAscii, isBase64, isBigint, isBoolean, isBtcAddress, isCreditCard, isDate, isDateString, isDecimal, isDefined, isEAN, isEmail, isEmpty, isETHAddress, isFQDN, isHex, isHexColor, isIBAN, isInteger, isIP, isIPRange, isISSN, isJWT, isLowercase, isMACAddress, isMobilePhone, isNotEmpty, isNotNull, isNotNullish, isNull, isNullish, isNumber, isObject, isObjectId, isPort, isString, isSWIFT, isTime, isUndefined, isUppercase, isURL, isUUID, isUUID1, isUUID2, isUUID3, isUUID4, isUUID5, toArray, toBigint, toBoolean, toDate, toDateString, toInteger, toNumber, toString, toTime, vg, };
|
|
@@ -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];
|