valgen 5.0.1 → 5.2.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 +14 -0
- package/cjs/core/context.js +1 -8
- package/cjs/rules/is-date.js +23 -30
- package/esm/core/context.js +1 -8
- package/esm/rules/is-date.js +23 -27
- package/package.json +2 -2
- package/typings/core/context.d.ts +1 -0
- package/typings/core/types.d.ts +1 -0
- package/typings/core/validator.d.ts +1 -1
- package/typings/rules/is-tuple.d.ts +1 -1
- package/typings/rules/pipe.d.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# v5.2.0
|
|
2
|
+
[2024-01-04]
|
|
3
|
+
|
|
4
|
+
### Changes
|
|
5
|
+
|
|
6
|
+
* Now validator options persist in context ([`418901f`](https://github.com/panates/valgen/commit/418901f74ee266d84e50ee82ba5a7221bd2be627))
|
|
7
|
+
|
|
8
|
+
# v5.1.0
|
|
9
|
+
[2023-12-23]
|
|
10
|
+
|
|
11
|
+
### Changes
|
|
12
|
+
|
|
13
|
+
* Moved from "dayjs" to "date-fns" ([`fd240af`](https://github.com/panates/valgen/commit/fd240afe3cdbb7485695d204aa25b9bf62ad315a))
|
|
14
|
+
|
|
1
15
|
# v5.0.1
|
|
2
16
|
[2023-12-23]
|
|
3
17
|
|
package/cjs/core/context.js
CHANGED
|
@@ -9,14 +9,7 @@ const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
|
|
|
9
9
|
class Context {
|
|
10
10
|
constructor(options) {
|
|
11
11
|
this.errors = [];
|
|
12
|
-
|
|
13
|
-
this.maxErrors = options.maxErrors;
|
|
14
|
-
if (typeof options?.onFail === 'function')
|
|
15
|
-
this.onFail = options.onFail;
|
|
16
|
-
if (options?.coerce != null)
|
|
17
|
-
this.coerce = options?.coerce;
|
|
18
|
-
if (options?.label != null)
|
|
19
|
-
this.label = options?.label;
|
|
12
|
+
Object.assign(this, options);
|
|
20
13
|
}
|
|
21
14
|
fail(rule, message, value, details) {
|
|
22
15
|
const issue = (0, omit_undefined_js_1.omitUndefined)({
|
package/cjs/rules/is-date.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.isDateString = exports.isDate = void 0;
|
|
7
|
-
const
|
|
8
|
-
const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFormat"));
|
|
4
|
+
const date_fns_1 = require("date-fns");
|
|
9
5
|
const index_js_1 = require("../core/index.js");
|
|
10
|
-
dayjs_1.default.extend(customParseFormat_1.default);
|
|
11
6
|
/* eslint-disable-next-line max-len */ // noinspection RegExpUnnecessaryNonCapturingGroup
|
|
12
7
|
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)?)?$/;
|
|
13
8
|
/**
|
|
@@ -18,27 +13,28 @@ const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[
|
|
|
18
13
|
function isDate(options) {
|
|
19
14
|
const precision = options?.precision;
|
|
20
15
|
return (0, index_js_1.validator)('isDate', function (input, context, _this) {
|
|
21
|
-
|
|
16
|
+
let d;
|
|
17
|
+
if (input instanceof Date)
|
|
18
|
+
d = input;
|
|
19
|
+
else if (input != null && context.coerce) {
|
|
22
20
|
if (typeof input === 'string' && context.coerce) {
|
|
23
|
-
|
|
24
|
-
if (d.isValid())
|
|
25
|
-
input = d.toDate();
|
|
21
|
+
d = (0, date_fns_1.parseISO)(input);
|
|
26
22
|
}
|
|
27
23
|
else if (typeof input === 'number')
|
|
28
|
-
|
|
24
|
+
d = new Date(input);
|
|
29
25
|
}
|
|
30
|
-
if (
|
|
26
|
+
if (d && !isNaN(d.getTime())) {
|
|
31
27
|
if (precision === 'year') {
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
d.setHours(0, 0, 0, 0);
|
|
29
|
+
d.setMonth(0, 1);
|
|
34
30
|
}
|
|
35
31
|
if (precision === 'month') {
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
d.setHours(0, 0, 0, 0);
|
|
33
|
+
d.setDate(1);
|
|
38
34
|
}
|
|
39
35
|
if (precision === 'date')
|
|
40
|
-
|
|
41
|
-
return
|
|
36
|
+
d.setHours(0, 0, 0, 0);
|
|
37
|
+
return d;
|
|
42
38
|
}
|
|
43
39
|
context.fail(_this, `{{label}} must be a Date instance or a date string`, input);
|
|
44
40
|
}, options);
|
|
@@ -54,10 +50,10 @@ function isDateString(options) {
|
|
|
54
50
|
const trim = options?.trim;
|
|
55
51
|
return (0, index_js_1.validator)('isDateString', function (input, context, _this) {
|
|
56
52
|
if (typeof input === 'string') {
|
|
57
|
-
const
|
|
58
|
-
if (
|
|
59
|
-
const
|
|
60
|
-
if (
|
|
53
|
+
const m = DATE_PATTERN.exec(input);
|
|
54
|
+
if (m) {
|
|
55
|
+
const d = (0, date_fns_1.parseISO)(input);
|
|
56
|
+
if (d && !isNaN(d.getTime())) {
|
|
61
57
|
if (!precision ||
|
|
62
58
|
precision === 'year' ||
|
|
63
59
|
(precision === 'month' && m[2]) ||
|
|
@@ -76,7 +72,7 @@ function isDateString(options) {
|
|
|
76
72
|
return s;
|
|
77
73
|
if (trim === 'date' || !m[4])
|
|
78
74
|
return s;
|
|
79
|
-
s += 'T' + m[4];
|
|
75
|
+
s += 'T' + m[4].substring(0, 8);
|
|
80
76
|
if (trim === 'time')
|
|
81
77
|
return s;
|
|
82
78
|
if (m[9])
|
|
@@ -86,13 +82,10 @@ function isDateString(options) {
|
|
|
86
82
|
}
|
|
87
83
|
}
|
|
88
84
|
}
|
|
89
|
-
else if (input
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return d.format('YYYY-MM-DD');
|
|
94
|
-
return d.millisecond() ? d.format('YYYY-MM-DDTHH:mm:ss.SSS') : d.format('YYYY-MM-DDTHH:mm:ss');
|
|
95
|
-
}
|
|
85
|
+
else if (input instanceof Date) {
|
|
86
|
+
return trim === 'date'
|
|
87
|
+
? (0, date_fns_1.formatISO)(input, { representation: 'date' })
|
|
88
|
+
: (0, date_fns_1.formatISO)(input).substring(0, 19);
|
|
96
89
|
}
|
|
97
90
|
context.fail(_this, `{{label}} is not a valid date string`, input, { ...options });
|
|
98
91
|
}, options);
|
package/esm/core/context.js
CHANGED
|
@@ -6,14 +6,7 @@ const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
|
|
|
6
6
|
export class Context {
|
|
7
7
|
constructor(options) {
|
|
8
8
|
this.errors = [];
|
|
9
|
-
|
|
10
|
-
this.maxErrors = options.maxErrors;
|
|
11
|
-
if (typeof options?.onFail === 'function')
|
|
12
|
-
this.onFail = options.onFail;
|
|
13
|
-
if (options?.coerce != null)
|
|
14
|
-
this.coerce = options?.coerce;
|
|
15
|
-
if (options?.label != null)
|
|
16
|
-
this.label = options?.label;
|
|
9
|
+
Object.assign(this, options);
|
|
17
10
|
}
|
|
18
11
|
fail(rule, message, value, details) {
|
|
19
12
|
const issue = omitUndefined({
|
package/esm/rules/is-date.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import customParseFormat from 'dayjs/plugin/customParseFormat';
|
|
1
|
+
import { formatISO, parseISO } from 'date-fns';
|
|
3
2
|
import { validator } from '../core/index.js';
|
|
4
|
-
dayjs.extend(customParseFormat);
|
|
5
3
|
/* eslint-disable-next-line max-len */ // noinspection RegExpUnnecessaryNonCapturingGroup
|
|
6
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)?)?$/;
|
|
7
5
|
/**
|
|
@@ -12,27 +10,28 @@ const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[
|
|
|
12
10
|
export function isDate(options) {
|
|
13
11
|
const precision = options?.precision;
|
|
14
12
|
return validator('isDate', function (input, context, _this) {
|
|
15
|
-
|
|
13
|
+
let d;
|
|
14
|
+
if (input instanceof Date)
|
|
15
|
+
d = input;
|
|
16
|
+
else if (input != null && context.coerce) {
|
|
16
17
|
if (typeof input === 'string' && context.coerce) {
|
|
17
|
-
|
|
18
|
-
if (d.isValid())
|
|
19
|
-
input = d.toDate();
|
|
18
|
+
d = parseISO(input);
|
|
20
19
|
}
|
|
21
20
|
else if (typeof input === 'number')
|
|
22
|
-
|
|
21
|
+
d = new Date(input);
|
|
23
22
|
}
|
|
24
|
-
if (
|
|
23
|
+
if (d && !isNaN(d.getTime())) {
|
|
25
24
|
if (precision === 'year') {
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
d.setHours(0, 0, 0, 0);
|
|
26
|
+
d.setMonth(0, 1);
|
|
28
27
|
}
|
|
29
28
|
if (precision === 'month') {
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
d.setHours(0, 0, 0, 0);
|
|
30
|
+
d.setDate(1);
|
|
32
31
|
}
|
|
33
32
|
if (precision === 'date')
|
|
34
|
-
|
|
35
|
-
return
|
|
33
|
+
d.setHours(0, 0, 0, 0);
|
|
34
|
+
return d;
|
|
36
35
|
}
|
|
37
36
|
context.fail(_this, `{{label}} must be a Date instance or a date string`, input);
|
|
38
37
|
}, options);
|
|
@@ -47,10 +46,10 @@ export function isDateString(options) {
|
|
|
47
46
|
const trim = options?.trim;
|
|
48
47
|
return validator('isDateString', function (input, context, _this) {
|
|
49
48
|
if (typeof input === 'string') {
|
|
50
|
-
const
|
|
51
|
-
if (
|
|
52
|
-
const
|
|
53
|
-
if (
|
|
49
|
+
const m = DATE_PATTERN.exec(input);
|
|
50
|
+
if (m) {
|
|
51
|
+
const d = parseISO(input);
|
|
52
|
+
if (d && !isNaN(d.getTime())) {
|
|
54
53
|
if (!precision ||
|
|
55
54
|
precision === 'year' ||
|
|
56
55
|
(precision === 'month' && m[2]) ||
|
|
@@ -69,7 +68,7 @@ export function isDateString(options) {
|
|
|
69
68
|
return s;
|
|
70
69
|
if (trim === 'date' || !m[4])
|
|
71
70
|
return s;
|
|
72
|
-
s += 'T' + m[4];
|
|
71
|
+
s += 'T' + m[4].substring(0, 8);
|
|
73
72
|
if (trim === 'time')
|
|
74
73
|
return s;
|
|
75
74
|
if (m[9])
|
|
@@ -79,13 +78,10 @@ export function isDateString(options) {
|
|
|
79
78
|
}
|
|
80
79
|
}
|
|
81
80
|
}
|
|
82
|
-
else if (input
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return d.format('YYYY-MM-DD');
|
|
87
|
-
return d.millisecond() ? d.format('YYYY-MM-DDTHH:mm:ss.SSS') : d.format('YYYY-MM-DDTHH:mm:ss');
|
|
88
|
-
}
|
|
81
|
+
else if (input instanceof Date) {
|
|
82
|
+
return trim === 'date'
|
|
83
|
+
? formatISO(input, { representation: 'date' })
|
|
84
|
+
: formatISO(input).substring(0, 19);
|
|
89
85
|
}
|
|
90
86
|
context.fail(_this, `{{label}} is not a valid date string`, input, { ...options });
|
|
91
87
|
}, options);
|
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.0
|
|
4
|
+
"version": "5.2.0",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"@types/validator": "^13.11.7",
|
|
16
|
-
"
|
|
16
|
+
"date-fns": "^3.0.6",
|
|
17
17
|
"ts-gems": "^3.1.0",
|
|
18
18
|
"validator": "^13.11.0"
|
|
19
19
|
},
|
|
@@ -12,6 +12,7 @@ export declare class Context implements ExecutionOptions {
|
|
|
12
12
|
property?: string;
|
|
13
13
|
index?: number;
|
|
14
14
|
label?: string;
|
|
15
|
+
[key: string]: any;
|
|
15
16
|
constructor(options?: ExecutionOptions);
|
|
16
17
|
fail(rule: Validator, message: string | Error, value: any, details?: Record<string, any>): void;
|
|
17
18
|
extend(options?: ExecutionOptions): Context;
|
package/typings/core/types.d.ts
CHANGED
|
@@ -15,4 +15,4 @@ export interface Validator<T = any, I = any, O extends ExecutionOptions = Execut
|
|
|
15
15
|
}
|
|
16
16
|
export declare function validator<T, I = T, O extends ExecutionOptions = ExecutionOptions>(fn: ValidateFunction<T, I>, validatorOptions?: ValidationOptions): Validator<T, I, O>;
|
|
17
17
|
export declare function validator<T, I = T, O extends ExecutionOptions = ExecutionOptions>(id: string, fn: ValidateFunction<T, I>, validatorOptions?: ValidationOptions): Validator<T, I, O>;
|
|
18
|
-
export declare function isValidator(x: any): x is Validator
|
|
18
|
+
export declare function isValidator(x: any): x is Validator;
|
|
@@ -23,5 +23,5 @@ export declare function isTuple<T1, I1, T2, I2, T3, I3, T4, I4, T5, I5, T6, I6>(
|
|
|
23
23
|
Validator<T4, I4>,
|
|
24
24
|
Validator<T5, I5>,
|
|
25
25
|
Validator<T6, I6>,
|
|
26
|
-
...Validator
|
|
26
|
+
...Validator[]
|
|
27
27
|
], options?: ValidationOptions): Validator<[T1, T2, T3, T4, T5, T6, ...any[]], [I1, I2, I3, I4, I5, I5, ...any[]]>;
|
package/typings/rules/pipe.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { Validator } from '../core/index.js';
|
|
|
3
3
|
*
|
|
4
4
|
* @validator pipe
|
|
5
5
|
*/
|
|
6
|
-
export declare function pipe<T>(...rules: [...Validator
|
|
6
|
+
export declare function pipe<T>(...rules: [...Validator[], Validator<T, any>]): Validator<T, any, import("../core/types.js").ExecutionOptions>;
|
|
7
7
|
/**
|
|
8
8
|
* Test given value against to all codecs and returns original input
|
|
9
9
|
* @validator allOf
|
|
10
10
|
*/
|
|
11
|
-
export declare function allOf(...rules: Validator
|
|
11
|
+
export declare function allOf(...rules: Validator[]): Validator<any, any, import("../core/types.js").ExecutionOptions>;
|