valgen 5.17.2 → 5.18.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 CHANGED
@@ -1,6 +1,22 @@
1
1
  ## Changelog
2
2
 
3
- ### [v5.17.2](https://github.com/panates/valgen/compare/v5.17.1...v5.17.2) -
3
+ ### [v5.18.0](https://github.com/panates/valgen/compare/v5.17.3...v5.18.0) -
4
+
5
+ #### 🚀 New Features
6
+
7
+ - feat: isTime accept Date instance @Eray Hanoğlu
8
+
9
+ #### 🪲 Fixes
10
+
11
+ - fix: Minor fix in isDate rule @Eray Hanoğlu
12
+
13
+ ### [v5.17.3](https://github.com/panates/valgen/compare/v5.17.2...v5.17.3) - 30 July 2025
14
+
15
+ #### 🚀 New Features
16
+
17
+ - feat: isDate can parse date string without signs @Eray Hanoğlu
18
+
19
+ ### [v5.17.2](https://github.com/panates/valgen/compare/v5.17.1...v5.17.2) - 29 July 2025
4
20
 
5
21
  #### 🛠 Refactoring and Updates
6
22
 
@@ -1,19 +1,50 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isTime = isTime;
4
- const tslib_1 = require("tslib");
5
- const validator_1 = tslib_1.__importDefault(require("@browsery/validator"));
6
4
  const index_js_1 = require("../../core/index.js");
5
+ const TIME_PATTERN = /^(\d{2}):?(\d{2})(?::?(\d{2})?(?:\.(\d{1,3}))?)?$/;
7
6
  /**
8
7
  * Validates if value is a time formatted string
9
8
  * @validator isTime
10
9
  */
11
10
  function isTime(options) {
12
- return (0, index_js_1.validator)('isURL', (input, context, _this) => {
13
- if (input != null &&
14
- typeof input === 'string' &&
15
- validator_1.default.isTime(input, options)) {
16
- return input;
11
+ return (0, index_js_1.validator)('isTime', (input, context, _this) => {
12
+ if (input != null) {
13
+ const coerce = options?.coerce ?? context.coerce;
14
+ let str;
15
+ if (input instanceof Date && coerce) {
16
+ str =
17
+ String(input.getHours()).padStart(2, '0') +
18
+ ':' +
19
+ String(input.getMinutes()).padStart(2, '0') +
20
+ ':' +
21
+ String(input.getSeconds()).padStart(2, '0');
22
+ if (input.getMilliseconds()) {
23
+ str += '.' + String(input.getMilliseconds()).padStart(3, '0');
24
+ }
25
+ return str;
26
+ }
27
+ else
28
+ str = input;
29
+ if (str && typeof str === 'string') {
30
+ const m = TIME_PATTERN.exec(str);
31
+ if (m) {
32
+ if (parseInt(m[1]) <= 23 &&
33
+ parseInt(m[2]) <= 59 &&
34
+ (!m[3] || parseInt(m[3]) <= 59)) {
35
+ if (!coerce)
36
+ return str;
37
+ let out = m[1];
38
+ if (m[2])
39
+ out += ':' + m[2];
40
+ if (m[3])
41
+ out += ':' + m[3];
42
+ if (m[4])
43
+ out += '.' + m[4];
44
+ return out;
45
+ }
46
+ }
47
+ }
17
48
  }
18
49
  context.fail(_this, `Value must be a valid Time`, input);
19
50
  }, options);
@@ -65,6 +65,7 @@ function isDateString(options) {
65
65
  }
66
66
  // noinspection RegExpUnnecessaryNonCapturingGroup
67
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
+ const DATE_PATTERN2 = /^(\d{4,14})(?:\.(\d{1,3}))?(?:(?:([+-])(0[0-9]|1[0-2])(\d{2})?)|Z)?$/;
68
69
  function coerceDateString(input, precision) {
69
70
  let dateParts;
70
71
  let outPrecision = 0;
@@ -83,17 +84,32 @@ function coerceDateString(input, precision) {
83
84
  }
84
85
  else if (typeof input === 'string') {
85
86
  const d = datefns.parseISO(input);
86
- const m = DATE_PATTERN.exec(input);
87
+ let m = DATE_PATTERN.exec(input);
87
88
  if (m && datefns.isValid(d)) {
88
89
  m.shift();
89
90
  dateParts = m;
90
91
  }
92
+ else {
93
+ m = DATE_PATTERN2.exec(input);
94
+ if (m) {
95
+ dateParts = [
96
+ m[1].substring(0, 4),
97
+ m[1].substring(4, 6),
98
+ m[1].substring(6, 8),
99
+ m[1].substring(8, 10),
100
+ m[1].substring(10, 12),
101
+ m[1].substring(12, 14),
102
+ m[2],
103
+ m[3] ? m[3] + m[4] + (m[5] ? ':' + m[5] : '') : '',
104
+ ];
105
+ }
106
+ }
91
107
  }
92
108
  if (!dateParts)
93
109
  return;
94
110
  outPrecision = dateParts.findIndex(v => !v);
95
111
  if (outPrecision === -1)
96
- outPrecision = dateParts.length + 1;
112
+ outPrecision = dateParts.length;
97
113
  const precisionIndex = (precision ? PRECISION_INDEX[precision] : 9) || 9;
98
114
  let value = dateParts[0] || '0000';
99
115
  if (precisionIndex > 1)
@@ -106,7 +122,7 @@ function coerceDateString(input, precision) {
106
122
  value += ':' + (dateParts[4] || '00');
107
123
  if (precisionIndex > 5)
108
124
  value += ':' + (dateParts[5] || '00');
109
- if (precisionIndex > 6)
125
+ if (precisionIndex > 5)
110
126
  value += dateParts[6] ? '.' + dateParts[6] : '';
111
127
  if (precisionIndex > 7)
112
128
  value += dateParts[7] || '';
@@ -1,15 +1,47 @@
1
- import validatorJS from '@browsery/validator';
2
1
  import { validator, } from '../../core/index.js';
2
+ const TIME_PATTERN = /^(\d{2}):?(\d{2})(?::?(\d{2})?(?:\.(\d{1,3}))?)?$/;
3
3
  /**
4
4
  * Validates if value is a time formatted string
5
5
  * @validator isTime
6
6
  */
7
7
  export function isTime(options) {
8
- return validator('isURL', (input, context, _this) => {
9
- if (input != null &&
10
- typeof input === 'string' &&
11
- validatorJS.isTime(input, options)) {
12
- return input;
8
+ return validator('isTime', (input, context, _this) => {
9
+ if (input != null) {
10
+ const coerce = options?.coerce ?? context.coerce;
11
+ let str;
12
+ if (input instanceof Date && coerce) {
13
+ str =
14
+ String(input.getHours()).padStart(2, '0') +
15
+ ':' +
16
+ String(input.getMinutes()).padStart(2, '0') +
17
+ ':' +
18
+ String(input.getSeconds()).padStart(2, '0');
19
+ if (input.getMilliseconds()) {
20
+ str += '.' + String(input.getMilliseconds()).padStart(3, '0');
21
+ }
22
+ return str;
23
+ }
24
+ else
25
+ str = input;
26
+ if (str && typeof str === 'string') {
27
+ const m = TIME_PATTERN.exec(str);
28
+ if (m) {
29
+ if (parseInt(m[1]) <= 23 &&
30
+ parseInt(m[2]) <= 59 &&
31
+ (!m[3] || parseInt(m[3]) <= 59)) {
32
+ if (!coerce)
33
+ return str;
34
+ let out = m[1];
35
+ if (m[2])
36
+ out += ':' + m[2];
37
+ if (m[3])
38
+ out += ':' + m[3];
39
+ if (m[4])
40
+ out += '.' + m[4];
41
+ return out;
42
+ }
43
+ }
44
+ }
13
45
  }
14
46
  context.fail(_this, `Value must be a valid Time`, input);
15
47
  }, options);
@@ -60,6 +60,7 @@ export function isDateString(options) {
60
60
  }
61
61
  // noinspection RegExpUnnecessaryNonCapturingGroup
62
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
+ const DATE_PATTERN2 = /^(\d{4,14})(?:\.(\d{1,3}))?(?:(?:([+-])(0[0-9]|1[0-2])(\d{2})?)|Z)?$/;
63
64
  function coerceDateString(input, precision) {
64
65
  let dateParts;
65
66
  let outPrecision = 0;
@@ -78,17 +79,32 @@ function coerceDateString(input, precision) {
78
79
  }
79
80
  else if (typeof input === 'string') {
80
81
  const d = datefns.parseISO(input);
81
- const m = DATE_PATTERN.exec(input);
82
+ let m = DATE_PATTERN.exec(input);
82
83
  if (m && datefns.isValid(d)) {
83
84
  m.shift();
84
85
  dateParts = m;
85
86
  }
87
+ else {
88
+ m = DATE_PATTERN2.exec(input);
89
+ if (m) {
90
+ dateParts = [
91
+ m[1].substring(0, 4),
92
+ m[1].substring(4, 6),
93
+ m[1].substring(6, 8),
94
+ m[1].substring(8, 10),
95
+ m[1].substring(10, 12),
96
+ m[1].substring(12, 14),
97
+ m[2],
98
+ m[3] ? m[3] + m[4] + (m[5] ? ':' + m[5] : '') : '',
99
+ ];
100
+ }
101
+ }
86
102
  }
87
103
  if (!dateParts)
88
104
  return;
89
105
  outPrecision = dateParts.findIndex(v => !v);
90
106
  if (outPrecision === -1)
91
- outPrecision = dateParts.length + 1;
107
+ outPrecision = dateParts.length;
92
108
  const precisionIndex = (precision ? PRECISION_INDEX[precision] : 9) || 9;
93
109
  let value = dateParts[0] || '0000';
94
110
  if (precisionIndex > 1)
@@ -101,7 +117,7 @@ function coerceDateString(input, precision) {
101
117
  value += ':' + (dateParts[4] || '00');
102
118
  if (precisionIndex > 5)
103
119
  value += ':' + (dateParts[5] || '00');
104
- if (precisionIndex > 6)
120
+ if (precisionIndex > 5)
105
121
  value += dateParts[6] ? '.' + dateParts[6] : '';
106
122
  if (precisionIndex > 7)
107
123
  value += dateParts[7] || '';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "valgen",
3
3
  "description": "Fast runtime type validator, converter and io (encoding/decoding) library",
4
- "version": "5.17.2",
4
+ "version": "5.18.0",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
package/types/index.d.cts CHANGED
@@ -43,7 +43,7 @@ declare const isObjectId: import("./core/validator.js").Validator<string | vg.Ob
43
43
  declare const isPort: import("./core/validator.js").Validator<number, string | number, import("./core/types.js").ExecutionOptions>;
44
44
  declare const isString: import("./core/validator.js").Validator<string, unknown, import("./core/types.js").ExecutionOptions>;
45
45
  declare const isSWIFT: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
46
- declare const isTime: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
46
+ declare const isTime: import("./core/validator.js").Validator<string, string | Date, import("./core/types.js").ExecutionOptions>;
47
47
  declare const isUndefined: import("./core/validator.js").Validator<any, unknown, import("./core/types.js").ExecutionOptions>;
48
48
  declare const isUppercase: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
49
49
  declare const isURL: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
@@ -61,5 +61,5 @@ declare const toDateString: import("./core/validator.js").Validator<string, stri
61
61
  declare const toInteger: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
62
62
  declare const toNumber: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
63
63
  declare const toString: import("./core/validator.js").Validator<string, unknown, import("./core/types.js").ExecutionOptions>;
64
- declare const toTime: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
64
+ declare const toTime: import("./core/validator.js").Validator<string, string | Date, import("./core/types.js").ExecutionOptions>;
65
65
  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, };
package/types/index.d.ts CHANGED
@@ -43,7 +43,7 @@ declare const isObjectId: import("./core/validator.js").Validator<string | vg.Ob
43
43
  declare const isPort: import("./core/validator.js").Validator<number, string | number, import("./core/types.js").ExecutionOptions>;
44
44
  declare const isString: import("./core/validator.js").Validator<string, unknown, import("./core/types.js").ExecutionOptions>;
45
45
  declare const isSWIFT: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
46
- declare const isTime: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
46
+ declare const isTime: import("./core/validator.js").Validator<string, string | Date, import("./core/types.js").ExecutionOptions>;
47
47
  declare const isUndefined: import("./core/validator.js").Validator<any, unknown, import("./core/types.js").ExecutionOptions>;
48
48
  declare const isUppercase: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
49
49
  declare const isURL: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
@@ -61,5 +61,5 @@ declare const toDateString: import("./core/validator.js").Validator<string, stri
61
61
  declare const toInteger: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
62
62
  declare const toNumber: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
63
63
  declare const toString: import("./core/validator.js").Validator<string, unknown, import("./core/types.js").ExecutionOptions>;
64
- declare const toTime: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
64
+ declare const toTime: import("./core/validator.js").Validator<string, string | Date, import("./core/types.js").ExecutionOptions>;
65
65
  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, };
@@ -1,9 +1,8 @@
1
- import validatorJS from '@browsery/validator';
2
1
  import { type ValidationOptions } from '../../core/index.js';
3
- export interface IsTimeOptions extends ValidationOptions, validatorJS.IsTimeOptions {
2
+ export interface IsTimeOptions extends ValidationOptions {
4
3
  }
5
4
  /**
6
5
  * Validates if value is a time formatted string
7
6
  * @validator isTime
8
7
  */
9
- export declare function isTime(options?: IsTimeOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
8
+ export declare function isTime(options?: IsTimeOptions): import("../../core/validator.js").Validator<string, string | Date, import("../../core/types.js").ExecutionOptions>;