@finnair/v-validation-luxon 1.1.0-alpha.8 → 2.0.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 +41 -0
- package/README.md +66 -18
- package/dist/Vluxon.d.ts +36 -9
- package/dist/Vluxon.js +124 -34
- package/dist/index.js +5 -1
- package/dist/luxon.d.ts +1 -0
- package/dist/luxon.js +3 -0
- package/dist/test-luxon.js +4 -1
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,47 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [2.0.0](https://github.com/finnair/v-validation/compare/v1.1.0...v2.0.0) (2022-09-08)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
* Upgrade All Dependencies (#80) ([fb6309c](https://github.com/finnair/v-validation/commit/fb6309cc1d9fd90f3e8c5ba79798fae1450b66a6)), closes [#80](https://github.com/finnair/v-validation/issues/80)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
* Implement toString in Luxon wrapper types ([#79](https://github.com/finnair/v-validation/issues/79)) ([3f3f3e8](https://github.com/finnair/v-validation/commit/3f3f3e8a4172c7803a7be4809f06aba554f7090f))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### BREAKING CHANGES
|
|
18
|
+
|
|
19
|
+
* Drop Node 12 support
|
|
20
|
+
* Add .nvmrc
|
|
21
|
+
* Introduce CI build for Node 18
|
|
22
|
+
* Upgrade All Dependencies
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# [1.1.0](https://github.com/finnair/v-validation/compare/v1.0.1...v1.1.0) (2022-08-29)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Features
|
|
32
|
+
|
|
33
|
+
* Support for Luxon ([94b3806](https://github.com/finnair/v-validation/commit/94b38060e07feeb0abb8c81659d8bda537a4d9aa))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# [1.1.0-alpha.9](https://github.com/finnair/v-validation/compare/v1.1.0-alpha.8...v1.1.0-alpha.9) (2022-08-19)
|
|
40
|
+
|
|
41
|
+
**Note:** Version bump only for package @finnair/v-validation-luxon
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
6
47
|
# [1.1.0-alpha.8](https://github.com/finnair/v-validation/compare/v1.1.0-alpha.7...v1.1.0-alpha.8) (2022-08-19)
|
|
7
48
|
|
|
8
49
|
**Note:** Version bump only for package @finnair/v-validation-luxon
|
package/README.md
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
`@finnair/v-validation-luxon` is an extension to `@finnair/v-validation`.
|
|
6
6
|
|
|
7
|
-
`Vluxon` extension
|
|
7
|
+
`Vluxon` extension provides custom wrapper types for Luxon DateTime to support full JSON roundtrip with strict validation.
|
|
8
|
+
Also plain DateTime validators are provided and custom formats are easy to define (see the code for examples).
|
|
8
9
|
|
|
9
10
|
[Documentation for `v-validation`](https://github.com/finnair/v-validation).
|
|
10
11
|
|
|
@@ -24,20 +25,67 @@ npm install @finnair/v-validation-luxon
|
|
|
24
25
|
|
|
25
26
|
## Vluxon
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
|
37
|
-
|
|
|
38
|
-
|
|
|
39
|
-
|
|
|
40
|
-
|
|
|
41
|
-
|
|
|
42
|
-
|
|
|
43
|
-
|
|
|
28
|
+
Vluxon contains both plain DateTime validators and also validators that return
|
|
29
|
+
DateTime wrappers that guarantee some normalizations (zone, date and/or time) and
|
|
30
|
+
especially JSON serialization in the given format. The wrappers are immutable and
|
|
31
|
+
they allow easy access to the DateTime instance for further processing.
|
|
32
|
+
`validateLuxon` function can be used to build custom DateTime validators/converters
|
|
33
|
+
by supplying a RegExp pattern and a parser function.
|
|
34
|
+
|
|
35
|
+
## Supported DateTime Wrapper Types
|
|
36
|
+
|
|
37
|
+
| Class | Description |
|
|
38
|
+
| ---------------------- | ----------------------------------------------------------------------------------------------------------- |
|
|
39
|
+
| LuxonDateTime | Abstract base class for the wrappers. |
|
|
40
|
+
| LocalDateLuxon | Input and JSON output in `yyyy-MM-dd` format. Time normalized to midnight UTC. |
|
|
41
|
+
| LocalTimeLuxon | Input and JSON output in `HH:mm:ss` format. Date normalized to 1970-01-01 UTC (Unix Epoch). |
|
|
42
|
+
| DateTimeLuxon | Input and JSON output in `yyyy-MM-ddTHH:mm:ssZ` format in local/given/parsed zone with milliseconds zeroed. |
|
|
43
|
+
| DateTimeUtcLuxon | Input and JSON output in `yyyy-MM-ddTHH:mm:ssZ` format in UTC zone with milliseconds zeroed. |
|
|
44
|
+
| DateTimeMillisLuxon | Input and JSON output in `yyyy-MM-ddTHH:mm:ss.SSSZ` format in local/given/parsed zone. |
|
|
45
|
+
| DateTimeMillisUtcLuxon | Input and JSON output in `yyyy-MM-ddTHH:mm:ss.SSSZ` format in UTC zone. |
|
|
46
|
+
|
|
47
|
+
### Constructors
|
|
48
|
+
|
|
49
|
+
Wrapper types may be constructed with `new` from DateTime instance, but there are also shortcuts:
|
|
50
|
+
|
|
51
|
+
| Static Method | Description |
|
|
52
|
+
| ---------------------------------------------------------------------- | --------------------------------------------------------------- |
|
|
53
|
+
| `now()` | Current time in `DateTime*` types. |
|
|
54
|
+
| `nowUtc()` | Current UTC time in `Local*` types. |
|
|
55
|
+
| `nowLocal(options?: DateTimeJSOptions)` | Current local time in `Local*` types (defaults to system zone). |
|
|
56
|
+
| `fromISO(value: string, options?: DateTimeOptions)` | Parse from ISO format (see Luxon `DateTime.fromISO`). |
|
|
57
|
+
| `fromFormat(value: string, format: string, options?: DateTimeOptions)` | Parse from custom format (see Luxon `DateTime.fromFormat`). |
|
|
58
|
+
| `fromJSDate(date: Date, options?: { zone?: string \| Zone })` | From JavaScript Date object (see Luxon `DateTime.fromJSDate`). |
|
|
59
|
+
| `fromMillis(millis: number, options?: DateTimeJSOptions)` | From Unix millis timestamp (see Luxon `DateTime.fromMillis`). |
|
|
60
|
+
|
|
61
|
+
### Instance Methods
|
|
62
|
+
|
|
63
|
+
Wrappers are meant to be as thin as possible with most of the DateTime functionality accessed directly from the wrapped
|
|
64
|
+
DateTime instance which is public readonly fiedl. However there are a few convenience methods for working with the wrapper types:
|
|
65
|
+
|
|
66
|
+
| Method | Description |
|
|
67
|
+
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
|
|
68
|
+
| `as(type)` | Conversion to the given type that extends LuxonDateTime. Note that the conversion is not guaranted to be lossless. |
|
|
69
|
+
| `wrap(fn: (dateTime: DateTime) => DateTime)` | Executes the given function on the wrapped DateTime and rewraps the result. |
|
|
70
|
+
| `apply<R>(fn: (dateTime: DateTime) => R): R` | Executes the given function on the wrapped DateTime and returns the result as it is. |
|
|
71
|
+
| `valueOf()` | Conversion to millis. This allows comparing wrapper types directly using `==`, `<`, `>`, `<=` or `>=`. |
|
|
72
|
+
| `equals(other: any)` | Type-aware equality. |
|
|
73
|
+
| `toJSON()` | Type-specific serialization (string). |
|
|
74
|
+
|
|
75
|
+
## Build-in Validators
|
|
76
|
+
|
|
77
|
+
| Vluxon. | Format | Description |
|
|
78
|
+
| ------------------- | -------------------------- | --------------------------------------------------------- |
|
|
79
|
+
| localDate | `yyyy-MM-dd` | Local date (time normalized to midninght UTC). |
|
|
80
|
+
| localTime | `HH:mm:ss` | Local time (date normalized to 1970-01-01). |
|
|
81
|
+
| dateTime | `yyyy-MM-ddTHH:mm:ssZ` | Date and time in local (parsed) time zone. |
|
|
82
|
+
| dateTimeUtc | `yyyy-MM-ddTHH:mm:ssZ` | Date and time in UTC time zone. |
|
|
83
|
+
| dateTimeMillis | `yyyy-MM-ddTHH:mm:ss.SSSZ` | Date and time with millis in local (parsed) time zone. |
|
|
84
|
+
| dateTimeMillisUtc | `yyyy-MM-ddTHH:mm:ss.SSSZ` | Date and time with millis in UTC time zone. |
|
|
85
|
+
| dateTimeFromISO | Any ISO | Plain Luxon DateTime from ISO format. |
|
|
86
|
+
| dateTimeFromRFC2822 | RFC2822 | Plain Luxon DateTime from RFC2822 format. |
|
|
87
|
+
| dateTimeFromHTTP | HTTP date-time | Plain Luxon DateTime from HTTP format. |
|
|
88
|
+
| dateTimeFromSQL | SQL date-time | Plain Luxon DateTime from SQL format. |
|
|
89
|
+
| dateTimeFromSeconds | Unix timestamp (number) | Plain Luxon DateTime from Unix timestamp in seconds. |
|
|
90
|
+
| dateTimeFromMillis | Unix timestamp (number) | Plain Luxon DateTime from Unix timestamp in milliseconds. |
|
|
91
|
+
| duration | ISO 8601 Duration | Luxon `Duration` with pattern validation. |
|
package/dist/Vluxon.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ValidationContext, Validator, ValidationResult } from '@finnair/v-validation';
|
|
2
2
|
import { Path } from '@finnair/path';
|
|
3
|
-
import { DateTime } from 'luxon';
|
|
3
|
+
import { DateTime, DateTimeJSOptions, DateTimeOptions } from 'luxon';
|
|
4
4
|
import { LuxonDateTime } from './luxon';
|
|
5
5
|
export declare type LuxonInput = string | DateTime | LuxonDateTime;
|
|
6
6
|
export interface ValidateLuxonParams {
|
|
@@ -8,20 +8,47 @@ export interface ValidateLuxonParams {
|
|
|
8
8
|
path: Path;
|
|
9
9
|
ctx: ValidationContext;
|
|
10
10
|
type: string;
|
|
11
|
-
proto: any;
|
|
12
11
|
pattern: RegExp;
|
|
13
|
-
|
|
12
|
+
proto?: any;
|
|
13
|
+
parser: (value: string, match: RegExpExecArray) => DateTime;
|
|
14
14
|
}
|
|
15
15
|
export declare function validateLuxon({ value, path, ctx, type, proto, pattern, parser }: ValidateLuxonParams): Promise<ValidationResult>;
|
|
16
|
+
declare function localDate(options?: DateTimeOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
17
|
+
declare function localTime(options?: DateTimeOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
18
|
+
declare function dateTime(options?: DateTimeOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
19
|
+
declare function dateTimeUtc(options?: DateTimeOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
20
|
+
declare function dateTimeMillis(options?: DateTimeOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
21
|
+
declare function dateTimeMillisUtc(options?: DateTimeOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
22
|
+
declare function dateTimeFromISO(options?: DateTimeOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
23
|
+
declare function dateTimeFromRFC2822(options?: DateTimeOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
24
|
+
declare function dateTimeFromHTTP(options?: DateTimeOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
25
|
+
declare function dateTimeFromSQL(options?: DateTimeOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
26
|
+
export interface ValidateLuxonNumberParams {
|
|
27
|
+
value: any;
|
|
28
|
+
path: Path;
|
|
29
|
+
ctx: ValidationContext;
|
|
30
|
+
type: string;
|
|
31
|
+
parser: (value: number) => DateTime;
|
|
32
|
+
}
|
|
33
|
+
export declare function validateLuxonNumber({ value, path, ctx, type, parser }: ValidateLuxonNumberParams): Promise<ValidationResult>;
|
|
34
|
+
declare function dateTimeFromMillis(options?: DateTimeJSOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
35
|
+
declare function dateTimeFromSeconds(options?: DateTimeJSOptions): import("@finnair/v-validation").ValidatorFnWrapper;
|
|
16
36
|
export declare class DurationValidator extends Validator {
|
|
17
37
|
validatePath(value: any, path: Path, ctx: ValidationContext): Promise<ValidationResult>;
|
|
18
38
|
}
|
|
19
39
|
export declare const Vluxon: {
|
|
20
|
-
localDate:
|
|
21
|
-
localTime:
|
|
22
|
-
dateTime:
|
|
23
|
-
dateTimeUtc:
|
|
24
|
-
dateTimeMillis:
|
|
25
|
-
dateTimeMillisUtc:
|
|
40
|
+
localDate: typeof localDate;
|
|
41
|
+
localTime: typeof localTime;
|
|
42
|
+
dateTime: typeof dateTime;
|
|
43
|
+
dateTimeUtc: typeof dateTimeUtc;
|
|
44
|
+
dateTimeMillis: typeof dateTimeMillis;
|
|
45
|
+
dateTimeMillisUtc: typeof dateTimeMillisUtc;
|
|
46
|
+
dateTimeFromISO: typeof dateTimeFromISO;
|
|
47
|
+
dateTimeFromRFC2822: typeof dateTimeFromRFC2822;
|
|
48
|
+
dateTimeFromHTTP: typeof dateTimeFromHTTP;
|
|
49
|
+
dateTimeFromSQL: typeof dateTimeFromSQL;
|
|
50
|
+
dateTimeFromSeconds: typeof dateTimeFromSeconds;
|
|
51
|
+
dateTimeFromMillis: typeof dateTimeFromMillis;
|
|
26
52
|
duration: () => DurationValidator;
|
|
27
53
|
};
|
|
54
|
+
export {};
|
package/dist/Vluxon.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Vluxon = exports.DurationValidator = exports.validateLuxon = void 0;
|
|
12
|
+
exports.Vluxon = exports.DurationValidator = exports.validateLuxonNumber = exports.validateLuxon = void 0;
|
|
13
13
|
const v_validation_1 = require("@finnair/v-validation");
|
|
14
14
|
const luxon_1 = require("luxon");
|
|
15
15
|
const luxon_2 = require("./luxon");
|
|
@@ -18,29 +18,33 @@ function validateLuxon({ value, path, ctx, type, proto, pattern, parser }) {
|
|
|
18
18
|
if ((0, v_validation_1.isNullOrUndefined)(value)) {
|
|
19
19
|
return ctx.failure(v_validation_1.defaultViolations.notNull(path), value);
|
|
20
20
|
}
|
|
21
|
-
if (value instanceof proto) {
|
|
21
|
+
if (proto && value instanceof proto) {
|
|
22
22
|
return ctx.success(value);
|
|
23
23
|
}
|
|
24
24
|
else if (luxon_1.DateTime.isDateTime(value)) {
|
|
25
25
|
if (value.isValid) {
|
|
26
|
-
return
|
|
26
|
+
return success(value);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
else if ((0, v_validation_1.isString)(value)) {
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const match = pattern.exec(value);
|
|
31
|
+
if (match) {
|
|
32
|
+
const dateTime = parser(value, match);
|
|
32
33
|
if (dateTime.isValid) {
|
|
33
|
-
return
|
|
34
|
+
return success(dateTime);
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
38
|
return ctx.failure(v_validation_1.defaultViolations.date(value, path, type), value);
|
|
39
|
+
function success(dateTime) {
|
|
40
|
+
return ctx.success(proto ? new proto(dateTime) : dateTime);
|
|
41
|
+
}
|
|
38
42
|
});
|
|
39
43
|
}
|
|
40
44
|
exports.validateLuxon = validateLuxon;
|
|
41
45
|
const datePattern = /^\d{4}-\d{2}-\d{2}$/;
|
|
42
|
-
function
|
|
43
|
-
return validateLuxon({
|
|
46
|
+
function localDate(options = { setZone: true }) {
|
|
47
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
|
|
44
48
|
value,
|
|
45
49
|
path,
|
|
46
50
|
ctx,
|
|
@@ -48,66 +52,144 @@ function localDateValidator(value, path, ctx) {
|
|
|
48
52
|
proto: luxon_2.LocalDateLuxon,
|
|
49
53
|
pattern: datePattern,
|
|
50
54
|
parser: (value) => luxon_1.DateTime.fromISO(value, { zone: luxon_1.FixedOffsetZone.utcInstance }),
|
|
51
|
-
});
|
|
55
|
+
}));
|
|
52
56
|
}
|
|
53
57
|
const timePattern = /^\d{2}:\d{2}:\d{2}$/;
|
|
54
|
-
function
|
|
55
|
-
return validateLuxon({
|
|
58
|
+
function localTime(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
|
|
59
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
|
|
56
60
|
value,
|
|
57
61
|
path,
|
|
58
62
|
ctx,
|
|
59
63
|
type: 'Time',
|
|
60
64
|
proto: luxon_2.LocalTimeLuxon,
|
|
61
65
|
pattern: timePattern,
|
|
62
|
-
parser: (value) => luxon_1.DateTime.fromISO(value,
|
|
63
|
-
});
|
|
66
|
+
parser: (value) => luxon_1.DateTime.fromISO(value, options),
|
|
67
|
+
}));
|
|
64
68
|
}
|
|
65
69
|
const dateTimePattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:Z|[+-]\d{2}(?::?\d{2})?)$/;
|
|
66
|
-
function
|
|
67
|
-
return validateLuxon({
|
|
70
|
+
function dateTime(options = { setZone: true }) {
|
|
71
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
|
|
68
72
|
value,
|
|
69
73
|
path,
|
|
70
74
|
ctx,
|
|
71
75
|
type: 'DateTime',
|
|
72
76
|
proto: luxon_2.DateTimeLuxon,
|
|
73
77
|
pattern: dateTimePattern,
|
|
74
|
-
parser: (value) => luxon_1.DateTime.fromISO(value,
|
|
75
|
-
});
|
|
78
|
+
parser: (value) => luxon_1.DateTime.fromISO(value, options),
|
|
79
|
+
}));
|
|
76
80
|
}
|
|
77
|
-
function
|
|
78
|
-
return validateLuxon({
|
|
81
|
+
function dateTimeUtc(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
|
|
82
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
|
|
79
83
|
value,
|
|
80
84
|
path,
|
|
81
85
|
ctx,
|
|
82
86
|
type: 'DateTime',
|
|
83
87
|
proto: luxon_2.DateTimeUtcLuxon,
|
|
84
88
|
pattern: dateTimePattern,
|
|
85
|
-
parser: (value) => luxon_1.DateTime.fromISO(value,
|
|
86
|
-
});
|
|
89
|
+
parser: (value) => luxon_1.DateTime.fromISO(value, options),
|
|
90
|
+
}));
|
|
87
91
|
}
|
|
88
92
|
const dateTimeMillisPattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(?:Z|[+-]\d{2}(?::?\d{2})?)$/;
|
|
89
|
-
function
|
|
90
|
-
return validateLuxon({
|
|
93
|
+
function dateTimeMillis(options = { setZone: true }) {
|
|
94
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
|
|
91
95
|
value,
|
|
92
96
|
path,
|
|
93
97
|
ctx,
|
|
94
98
|
type: 'DateTimeMillis',
|
|
95
99
|
proto: luxon_2.DateTimeMillisLuxon,
|
|
96
100
|
pattern: dateTimeMillisPattern,
|
|
97
|
-
parser: (value) => luxon_1.DateTime.fromISO(value,
|
|
98
|
-
});
|
|
101
|
+
parser: (value) => luxon_1.DateTime.fromISO(value, options),
|
|
102
|
+
}));
|
|
99
103
|
}
|
|
100
|
-
function
|
|
101
|
-
return validateLuxon({
|
|
104
|
+
function dateTimeMillisUtc(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
|
|
105
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
|
|
102
106
|
value,
|
|
103
107
|
path,
|
|
104
108
|
ctx,
|
|
105
109
|
type: 'DateTimeMillis',
|
|
106
110
|
proto: luxon_2.DateTimeMillisUtcLuxon,
|
|
107
111
|
pattern: dateTimeMillisPattern,
|
|
108
|
-
parser: (value) => luxon_1.DateTime.fromISO(value,
|
|
112
|
+
parser: (value) => luxon_1.DateTime.fromISO(value, options),
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
function dateTimeFromISO(options = { setZone: true }) {
|
|
116
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
|
|
117
|
+
value,
|
|
118
|
+
path,
|
|
119
|
+
ctx,
|
|
120
|
+
type: 'ISODateTime',
|
|
121
|
+
pattern: /./,
|
|
122
|
+
parser: (value) => luxon_1.DateTime.fromISO(value, options),
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
function dateTimeFromRFC2822(options = { setZone: true }) {
|
|
126
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
|
|
127
|
+
value,
|
|
128
|
+
path,
|
|
129
|
+
ctx,
|
|
130
|
+
type: 'RFC2822DateTime',
|
|
131
|
+
pattern: /./,
|
|
132
|
+
parser: (value) => luxon_1.DateTime.fromRFC2822(value, options),
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
function dateTimeFromHTTP(options = { setZone: true }) {
|
|
136
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
|
|
137
|
+
value,
|
|
138
|
+
path,
|
|
139
|
+
ctx,
|
|
140
|
+
type: 'HTTPDateTime',
|
|
141
|
+
pattern: /./,
|
|
142
|
+
parser: (value) => luxon_1.DateTime.fromHTTP(value, options),
|
|
143
|
+
}));
|
|
144
|
+
}
|
|
145
|
+
function dateTimeFromSQL(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
|
|
146
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
|
|
147
|
+
value,
|
|
148
|
+
path,
|
|
149
|
+
ctx,
|
|
150
|
+
type: 'SQLDateTime',
|
|
151
|
+
pattern: /./,
|
|
152
|
+
parser: (value) => luxon_1.DateTime.fromSQL(value, options),
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
function validateLuxonNumber({ value, path, ctx, type, parser }) {
|
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
if ((0, v_validation_1.isNullOrUndefined)(value)) {
|
|
158
|
+
return ctx.failure(v_validation_1.defaultViolations.notNull(path), value);
|
|
159
|
+
}
|
|
160
|
+
else if (luxon_1.DateTime.isDateTime(value)) {
|
|
161
|
+
if (value.isValid) {
|
|
162
|
+
return ctx.success(value);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else if (typeof value === 'number' && !Number.isNaN(value)) {
|
|
166
|
+
const dateTime = parser(value);
|
|
167
|
+
if (dateTime.isValid) {
|
|
168
|
+
return ctx.success(dateTime);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return ctx.failure(v_validation_1.defaultViolations.date(value, path, type), value);
|
|
109
172
|
});
|
|
110
173
|
}
|
|
174
|
+
exports.validateLuxonNumber = validateLuxonNumber;
|
|
175
|
+
function dateTimeFromMillis(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
|
|
176
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxonNumber({
|
|
177
|
+
value,
|
|
178
|
+
path,
|
|
179
|
+
ctx,
|
|
180
|
+
type: 'MillisDateTime',
|
|
181
|
+
parser: value => luxon_1.DateTime.fromMillis(value, options),
|
|
182
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
function dateTimeFromSeconds(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
|
|
185
|
+
return v_validation_1.V.fn((value, path, ctx) => validateLuxonNumber({
|
|
186
|
+
value,
|
|
187
|
+
path,
|
|
188
|
+
ctx,
|
|
189
|
+
type: 'SecondsDateTime',
|
|
190
|
+
parser: value => luxon_1.DateTime.fromSeconds(value, options),
|
|
191
|
+
}));
|
|
192
|
+
}
|
|
111
193
|
const durationPattern = /^P(?!$)(\d+(?:\.\d+)?Y)?(\d+(?:\.\d+)?M)?(\d+(?:\.\d+)?W)?(\d+(?:\.\d+)?D)?(T(?=\d)(\d+(?:\.\d+)?H)?(\d+(?:\.\d+)?M)?(\d+(?:\.\d+)?S)?)?$/;
|
|
112
194
|
class DurationValidator extends v_validation_1.Validator {
|
|
113
195
|
validatePath(value, path, ctx) {
|
|
@@ -130,11 +212,19 @@ class DurationValidator extends v_validation_1.Validator {
|
|
|
130
212
|
}
|
|
131
213
|
exports.DurationValidator = DurationValidator;
|
|
132
214
|
exports.Vluxon = {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
215
|
+
// DateTime wrapper validators
|
|
216
|
+
localDate,
|
|
217
|
+
localTime,
|
|
218
|
+
dateTime,
|
|
219
|
+
dateTimeUtc,
|
|
220
|
+
dateTimeMillis,
|
|
221
|
+
dateTimeMillisUtc,
|
|
222
|
+
// Plain DateTime validators
|
|
223
|
+
dateTimeFromISO,
|
|
224
|
+
dateTimeFromRFC2822,
|
|
225
|
+
dateTimeFromHTTP,
|
|
226
|
+
dateTimeFromSQL,
|
|
227
|
+
dateTimeFromSeconds,
|
|
228
|
+
dateTimeFromMillis,
|
|
139
229
|
duration: () => new DurationValidator(),
|
|
140
230
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
package/dist/luxon.d.ts
CHANGED
package/dist/luxon.js
CHANGED
package/dist/test-luxon.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var _a, _b;
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
const luxon_1 = require("luxon");
|
|
4
|
-
console.log(luxon_1.DateTime.
|
|
5
|
+
console.log(luxon_1.DateTime.fromMillis(luxon_1.DateTime.fromISO('2022-08-30T23:00:00Z').toMillis(), { zone: luxon_1.FixedOffsetZone.instance(180) }).toJSON());
|
|
6
|
+
let o = { foo: 1 };
|
|
7
|
+
console.log((_b = (_a = o.foo) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : 'not found');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@finnair/v-validation-luxon",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Luxon validators",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"build": "tsc -b ."
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@finnair/path": "^
|
|
29
|
-
"@finnair/v-validation": "^
|
|
30
|
-
"luxon": "3.0.
|
|
28
|
+
"@finnair/path": "^2.0.0",
|
|
29
|
+
"@finnair/v-validation": "^2.0.0",
|
|
30
|
+
"luxon": "3.0.3"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@types/luxon": "3.0.
|
|
33
|
+
"@types/luxon": "3.0.1"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "432759665b664859d5ed80e4b573432abdb23afc"
|
|
36
36
|
}
|