@finnair/v-validation-luxon 1.1.0-alpha.7 → 1.1.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
@@ -3,6 +3,33 @@
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
+ # [1.1.0](https://github.com/finnair/v-validation/compare/v1.0.1...v1.1.0) (2022-08-29)
7
+
8
+
9
+ ### Features
10
+
11
+ * Support for Luxon ([94b3806](https://github.com/finnair/v-validation/commit/94b38060e07feeb0abb8c81659d8bda537a4d9aa))
12
+
13
+
14
+
15
+
16
+
17
+ # [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)
18
+
19
+ **Note:** Version bump only for package @finnair/v-validation-luxon
20
+
21
+
22
+
23
+
24
+
25
+ # [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)
26
+
27
+ **Note:** Version bump only for package @finnair/v-validation-luxon
28
+
29
+
30
+
31
+
32
+
6
33
  # [1.1.0-alpha.7](https://github.com/finnair/v-validation/compare/v1.1.0-alpha.6...v1.1.0-alpha.7) (2022-08-18)
7
34
 
8
35
  **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 uses custom wrapper types for Luxon DateTime to support full JSON roundtrip with strict validation.
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
- `validateLuxon` functions can be used to build custom DateTime validators/converters
28
- by supplying a RegExp pattern and a wrapper class (subclass of LuxonDateTime).
29
- The wrapper class should guarantee proper `toJSON()` and other required properties
30
- for the type (e.g. timezone handling).
31
-
32
- Time zone 00:00 is serialized as `Z`.
33
-
34
- | Vluxon . | Format | Description |
35
- | ----------------- | -------------------------- | ------------------------------------------------------ |
36
- | date | `YYYY-MM-DD` | Local date. |
37
- | dateUtc | `YYYY-MM-DD` | Date in UTC time zone. |
38
- | dateTime | `YYYY-MM-DDTHH:mm:ssZ` | Date and time in local (parsed) time zone. |
39
- | dateTimeUtc | `YYYY-MM-DDTHH:mm:ssZ` | Date and time in UTC time zone. |
40
- | dateTimeMillis | `YYYY-MM-DDTHH:mm:ss.SSSZ` | Date and time with millis in local (parsed) time zone. |
41
- | dateTimeMillisUtc | `YYYY-MM-DDTHH:mm:ss.SSSZ` | Date and time with millis in UTC time zone. |
42
- | time | `HH:mm:ss` | Local time. |
43
- | duration | ISO 8601 Duration | Luxon `Duration` with pattern validation. |
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
- parser: (value: string) => DateTime;
12
+ proto?: any;
13
+ parser: (value: string, match: RegExpExecArray) => DateTime;
14
14
  }
15
- export declare function validateLuxon({ value, path, ctx, type, proto, pattern, parser }: ValidateLuxonParams): ValidationResult;
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: () => import("@finnair/v-validation").ValidatorFnWrapper;
21
- localTime: () => import("@finnair/v-validation").ValidatorFnWrapper;
22
- dateTime: () => import("@finnair/v-validation").ValidatorFnWrapper;
23
- dateTimeUtc: () => import("@finnair/v-validation").ValidatorFnWrapper;
24
- dateTimeMillis: () => import("@finnair/v-validation").ValidatorFnWrapper;
25
- dateTimeMillisUtc: () => import("@finnair/v-validation").ValidatorFnWrapper;
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,115 +9,187 @@ 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");
16
16
  function validateLuxon({ value, path, ctx, type, proto, pattern, parser }) {
17
- if ((0, v_validation_1.isNullOrUndefined)(value)) {
18
- return ctx.failure(v_validation_1.defaultViolations.notNull(path), value);
19
- }
20
- if (value instanceof proto) {
21
- return ctx.success(value);
22
- }
23
- else if (luxon_1.DateTime.isDateTime(value)) {
24
- if (value.isValid) {
25
- return ctx.success(new proto(value));
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ if ((0, v_validation_1.isNullOrUndefined)(value)) {
19
+ return ctx.failure(v_validation_1.defaultViolations.notNull(path), value);
26
20
  }
27
- }
28
- else if ((0, v_validation_1.isString)(value)) {
29
- if (pattern.test(value)) {
30
- const dateTime = parser(value);
31
- if (dateTime.isValid) {
32
- return ctx.success(new proto(dateTime));
21
+ if (proto && value instanceof proto) {
22
+ return ctx.success(value);
23
+ }
24
+ else if (luxon_1.DateTime.isDateTime(value)) {
25
+ if (value.isValid) {
26
+ return success(value);
33
27
  }
34
28
  }
35
- }
36
- return ctx.failure(v_validation_1.defaultViolations.date(value, path, type), value);
29
+ else if ((0, v_validation_1.isString)(value)) {
30
+ const match = pattern.exec(value);
31
+ if (match) {
32
+ const dateTime = parser(value, match);
33
+ if (dateTime.isValid) {
34
+ return success(dateTime);
35
+ }
36
+ }
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
+ }
42
+ });
37
43
  }
38
44
  exports.validateLuxon = validateLuxon;
39
45
  const datePattern = /^\d{4}-\d{2}-\d{2}$/;
40
- function localDateValidator(value, path, ctx) {
41
- return __awaiter(this, void 0, void 0, function* () {
42
- return validateLuxon({
43
- value,
44
- path,
45
- ctx,
46
- type: 'Date',
47
- proto: luxon_2.LocalDateLuxon,
48
- pattern: datePattern,
49
- parser: (value) => luxon_1.DateTime.fromISO(value, { zone: luxon_1.FixedOffsetZone.utcInstance }),
50
- });
51
- });
46
+ function localDate(options = { setZone: true }) {
47
+ return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
48
+ value,
49
+ path,
50
+ ctx,
51
+ type: 'Date',
52
+ proto: luxon_2.LocalDateLuxon,
53
+ pattern: datePattern,
54
+ parser: (value) => luxon_1.DateTime.fromISO(value, { zone: luxon_1.FixedOffsetZone.utcInstance }),
55
+ }));
52
56
  }
53
57
  const timePattern = /^\d{2}:\d{2}:\d{2}$/;
54
- function localTimeValidator(value, path, ctx) {
55
- return __awaiter(this, void 0, void 0, function* () {
56
- return validateLuxon({
57
- value,
58
- path,
59
- ctx,
60
- type: 'Time',
61
- proto: luxon_2.LocalTimeLuxon,
62
- pattern: timePattern,
63
- parser: (value) => luxon_1.DateTime.fromISO(value, { zone: luxon_1.FixedOffsetZone.utcInstance }),
64
- });
65
- });
58
+ function localTime(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
59
+ return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
60
+ value,
61
+ path,
62
+ ctx,
63
+ type: 'Time',
64
+ proto: luxon_2.LocalTimeLuxon,
65
+ pattern: timePattern,
66
+ parser: (value) => luxon_1.DateTime.fromISO(value, options),
67
+ }));
66
68
  }
67
69
  const dateTimePattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:Z|[+-]\d{2}(?::?\d{2})?)$/;
68
- function dateTimeValidator(value, path, ctx) {
69
- return __awaiter(this, void 0, void 0, function* () {
70
- return validateLuxon({
71
- value,
72
- path,
73
- ctx,
74
- type: 'DateTime',
75
- proto: luxon_2.DateTimeLuxon,
76
- pattern: dateTimePattern,
77
- parser: (value) => luxon_1.DateTime.fromISO(value, { setZone: true }),
78
- });
79
- });
70
+ function dateTime(options = { setZone: true }) {
71
+ return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
72
+ value,
73
+ path,
74
+ ctx,
75
+ type: 'DateTime',
76
+ proto: luxon_2.DateTimeLuxon,
77
+ pattern: dateTimePattern,
78
+ parser: (value) => luxon_1.DateTime.fromISO(value, options),
79
+ }));
80
80
  }
81
- function dateTimeUtcValidator(value, path, ctx) {
82
- return __awaiter(this, void 0, void 0, function* () {
83
- return validateLuxon({
84
- value,
85
- path,
86
- ctx,
87
- type: 'DateTime',
88
- proto: luxon_2.DateTimeUtcLuxon,
89
- pattern: dateTimePattern,
90
- parser: (value) => luxon_1.DateTime.fromISO(value, { zone: luxon_1.FixedOffsetZone.utcInstance }),
91
- });
92
- });
81
+ function dateTimeUtc(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
82
+ return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
83
+ value,
84
+ path,
85
+ ctx,
86
+ type: 'DateTime',
87
+ proto: luxon_2.DateTimeUtcLuxon,
88
+ pattern: dateTimePattern,
89
+ parser: (value) => luxon_1.DateTime.fromISO(value, options),
90
+ }));
93
91
  }
94
92
  const dateTimeMillisPattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(?:Z|[+-]\d{2}(?::?\d{2})?)$/;
95
- function dateTimeMillisValidator(value, path, ctx) {
96
- return __awaiter(this, void 0, void 0, function* () {
97
- return validateLuxon({
98
- value,
99
- path,
100
- ctx,
101
- type: 'DateTimeMillis',
102
- proto: luxon_2.DateTimeMillisLuxon,
103
- pattern: dateTimeMillisPattern,
104
- parser: (value) => luxon_1.DateTime.fromISO(value, { setZone: true }),
105
- });
106
- });
93
+ function dateTimeMillis(options = { setZone: true }) {
94
+ return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
95
+ value,
96
+ path,
97
+ ctx,
98
+ type: 'DateTimeMillis',
99
+ proto: luxon_2.DateTimeMillisLuxon,
100
+ pattern: dateTimeMillisPattern,
101
+ parser: (value) => luxon_1.DateTime.fromISO(value, options),
102
+ }));
103
+ }
104
+ function dateTimeMillisUtc(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
105
+ return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
106
+ value,
107
+ path,
108
+ ctx,
109
+ type: 'DateTimeMillis',
110
+ proto: luxon_2.DateTimeMillisUtcLuxon,
111
+ pattern: dateTimeMillisPattern,
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
+ }));
107
154
  }
108
- function dateTimeMillisUtcValidator(value, path, ctx) {
155
+ function validateLuxonNumber({ value, path, ctx, type, parser }) {
109
156
  return __awaiter(this, void 0, void 0, function* () {
110
- return validateLuxon({
111
- value,
112
- path,
113
- ctx,
114
- type: 'DateTimeMillis',
115
- proto: luxon_2.DateTimeMillisUtcLuxon,
116
- pattern: dateTimeMillisPattern,
117
- parser: (value) => luxon_1.DateTime.fromISO(value, { zone: luxon_1.FixedOffsetZone.utcInstance }),
118
- });
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);
119
172
  });
120
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
+ }
121
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)?)?$/;
122
194
  class DurationValidator extends v_validation_1.Validator {
123
195
  validatePath(value, path, ctx) {
@@ -140,11 +212,19 @@ class DurationValidator extends v_validation_1.Validator {
140
212
  }
141
213
  exports.DurationValidator = DurationValidator;
142
214
  exports.Vluxon = {
143
- localDate: () => v_validation_1.V.fn(localDateValidator),
144
- localTime: () => v_validation_1.V.fn(localTimeValidator),
145
- dateTime: () => v_validation_1.V.fn(dateTimeValidator),
146
- dateTimeUtc: () => v_validation_1.V.fn(dateTimeUtcValidator),
147
- dateTimeMillis: () => v_validation_1.V.fn(dateTimeMillisValidator),
148
- dateTimeMillisUtc: () => v_validation_1.V.fn(dateTimeMillisUtcValidator),
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,
149
229
  duration: () => new DurationValidator(),
150
230
  };
package/dist/luxon.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { DateTime, DateTimeJSOptions, DateTimeOptions } from 'luxon';
1
+ import { DateTime, DateTimeJSOptions, DateTimeOptions, Zone } from 'luxon';
2
2
  export declare type LuxonDateTimeInput = DateTime | LuxonDateTime;
3
3
  export declare abstract class LuxonDateTime {
4
4
  readonly dateTime: DateTime;
@@ -19,8 +19,11 @@ export declare class LocalDateLuxon extends LuxonDateTime {
19
19
  static nowLocal(options?: DateTimeJSOptions): LocalDateLuxon;
20
20
  static nowUtc(): LocalDateLuxon;
21
21
  static fromISO(value: string, options?: DateTimeOptions): LocalDateLuxon;
22
- static fromJSDate(date: Date, options?: DateTimeOptions): LocalDateLuxon;
23
- static fromMillis(millis: number, options?: DateTimeOptions): LocalDateLuxon;
22
+ static fromFormat(value: string, format: string, options?: DateTimeOptions): LocalDateLuxon;
23
+ static fromJSDate(date: Date, options?: {
24
+ zone?: string | Zone;
25
+ }): LocalDateLuxon;
26
+ static fromMillis(millis: number, options?: DateTimeJSOptions): LocalDateLuxon;
24
27
  protected normalize(dateTime: DateTime): DateTime;
25
28
  wrap(fn: (dateTime: DateTime) => DateTime): LocalDateLuxon;
26
29
  toJSON(): string;
@@ -31,8 +34,11 @@ export declare class LocalTimeLuxon extends LuxonDateTime {
31
34
  static nowLocal(options?: DateTimeJSOptions): LocalTimeLuxon;
32
35
  static nowUtc(): LocalTimeLuxon;
33
36
  static fromISO(value: string, options?: DateTimeOptions): LocalTimeLuxon;
34
- static fromJSDate(date: Date, options?: DateTimeOptions): LocalTimeLuxon;
35
- static fromMillis(millis: number, options?: DateTimeOptions): LocalTimeLuxon;
37
+ static fromFormat(value: string, format: string, options?: DateTimeOptions): LocalTimeLuxon;
38
+ static fromJSDate(date: Date, options?: {
39
+ zone?: string | Zone;
40
+ }): LocalTimeLuxon;
41
+ static fromMillis(millis: number, options?: DateTimeJSOptions): LocalTimeLuxon;
36
42
  protected normalize(dateTime: DateTime): DateTime;
37
43
  wrap(fn: (dateTime: DateTime) => DateTime): LocalTimeLuxon;
38
44
  toJSON(): string;
@@ -43,8 +49,11 @@ export declare class DateTimeLuxon extends LuxonDateTime {
43
49
  constructor(input: LuxonDateTimeInput);
44
50
  static now(options?: DateTimeJSOptions): DateTimeLuxon;
45
51
  static fromISO(value: string, options?: DateTimeOptions): DateTimeLuxon;
46
- static fromJSDate(date: Date, options?: DateTimeOptions): DateTimeLuxon;
47
- static fromMillis(millis: number, options?: DateTimeOptions): DateTimeLuxon;
52
+ static fromFormat(value: string, format: string, options?: DateTimeOptions): DateTimeLuxon;
53
+ static fromJSDate(date: Date, options?: {
54
+ zone?: string | Zone;
55
+ }): DateTimeLuxon;
56
+ static fromMillis(millis: number, options?: DateTimeJSOptions): DateTimeLuxon;
48
57
  protected normalize(dateTime: DateTime): DateTime;
49
58
  wrap(fn: (dateTime: DateTime) => DateTime): DateTimeLuxon;
50
59
  toJSON(): string;
@@ -53,8 +62,11 @@ export declare class DateTimeUtcLuxon extends LuxonDateTime {
53
62
  constructor(input: LuxonDateTimeInput);
54
63
  static now(): DateTimeUtcLuxon;
55
64
  static fromISO(value: string, options?: DateTimeOptions): DateTimeUtcLuxon;
56
- static fromJSDate(date: Date, options?: DateTimeOptions): DateTimeUtcLuxon;
57
- static fromMillis(millis: number, options?: DateTimeOptions): DateTimeUtcLuxon;
65
+ static fromFormat(value: string, format: string, options?: DateTimeOptions): DateTimeUtcLuxon;
66
+ static fromJSDate(date: Date, options?: {
67
+ zone?: string | Zone;
68
+ }): DateTimeUtcLuxon;
69
+ static fromMillis(millis: number, options?: DateTimeJSOptions): DateTimeUtcLuxon;
58
70
  protected normalize(dateTime: DateTime): DateTime;
59
71
  wrap(fn: (dateTime: DateTime) => DateTime): DateTimeUtcLuxon;
60
72
  toJSON(): string;
@@ -65,8 +77,11 @@ export declare class DateTimeMillisLuxon extends LuxonDateTime {
65
77
  constructor(input: LuxonDateTimeInput);
66
78
  static now(options?: DateTimeJSOptions): DateTimeMillisLuxon;
67
79
  static fromISO(value: string, options?: DateTimeOptions): DateTimeMillisLuxon;
68
- static fromJSDate(date: Date, options?: DateTimeOptions): DateTimeMillisLuxon;
69
- static fromMillis(millis: number, options?: DateTimeOptions): DateTimeMillisLuxon;
80
+ static fromFormat(value: string, format: string, options?: DateTimeOptions): DateTimeMillisLuxon;
81
+ static fromJSDate(date: Date, options?: {
82
+ zone?: string | Zone;
83
+ }): DateTimeMillisLuxon;
84
+ static fromMillis(millis: number, options?: DateTimeJSOptions): DateTimeMillisLuxon;
70
85
  protected normalize(dateTime: DateTime): DateTime;
71
86
  wrap(fn: (dateTime: DateTime) => DateTime): DateTimeMillisLuxon;
72
87
  toJSON(): string;
@@ -75,8 +90,11 @@ export declare class DateTimeMillisUtcLuxon extends LuxonDateTime {
75
90
  constructor(input: LuxonDateTimeInput);
76
91
  static now(): DateTimeMillisUtcLuxon;
77
92
  static fromISO(value: string, options?: DateTimeOptions): DateTimeMillisUtcLuxon;
78
- static fromJSDate(date: Date, options?: DateTimeOptions): DateTimeMillisUtcLuxon;
79
- static fromMillis(millis: number, options?: DateTimeOptions): DateTimeMillisUtcLuxon;
93
+ static fromFormat(value: string, format: string, options?: DateTimeOptions): DateTimeMillisUtcLuxon;
94
+ static fromJSDate(date: Date, options?: {
95
+ zone?: string | Zone;
96
+ }): DateTimeMillisUtcLuxon;
97
+ static fromMillis(millis: number, options?: DateTimeJSOptions): DateTimeMillisUtcLuxon;
80
98
  protected normalize(dateTime: DateTime): DateTime;
81
99
  wrap(fn: (dateTime: DateTime) => DateTime): DateTimeMillisUtcLuxon;
82
100
  toJSON(): string;
package/dist/luxon.js CHANGED
@@ -46,13 +46,16 @@ class LocalDateLuxon extends LuxonDateTime {
46
46
  return new LocalDateLuxon(luxon_1.DateTime.utc());
47
47
  }
48
48
  static fromISO(value, options) {
49
- return new LocalDateLuxon(luxon_1.DateTime.fromISO(value, options));
49
+ return new LocalDateLuxon(luxon_1.DateTime.fromISO(value, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
50
+ }
51
+ static fromFormat(value, format, options) {
52
+ return new LocalDateLuxon(luxon_1.DateTime.fromFormat(value, format, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
50
53
  }
51
54
  static fromJSDate(date, options) {
52
- return new LocalDateLuxon(luxon_1.DateTime.fromJSDate(date, options));
55
+ return new LocalDateLuxon(luxon_1.DateTime.fromJSDate(date, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
53
56
  }
54
57
  static fromMillis(millis, options) {
55
- return new LocalDateLuxon(luxon_1.DateTime.fromMillis(millis, options));
58
+ return new LocalDateLuxon(luxon_1.DateTime.fromMillis(millis, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
56
59
  }
57
60
  normalize(dateTime) {
58
61
  return luxon_1.DateTime.utc(dateTime.year, dateTime.month, dateTime.day);
@@ -77,13 +80,16 @@ class LocalTimeLuxon extends LuxonDateTime {
77
80
  return new LocalTimeLuxon(luxon_1.DateTime.utc());
78
81
  }
79
82
  static fromISO(value, options) {
80
- return new LocalTimeLuxon(luxon_1.DateTime.fromISO(value, options));
83
+ return new LocalTimeLuxon(luxon_1.DateTime.fromISO(value, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
84
+ }
85
+ static fromFormat(value, format, options) {
86
+ return new LocalTimeLuxon(luxon_1.DateTime.fromFormat(value, format, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
81
87
  }
82
88
  static fromJSDate(date, options) {
83
- return new LocalTimeLuxon(luxon_1.DateTime.fromJSDate(date, options));
89
+ return new LocalTimeLuxon(luxon_1.DateTime.fromJSDate(date, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
84
90
  }
85
91
  static fromMillis(millis, options) {
86
- return new LocalTimeLuxon(luxon_1.DateTime.fromMillis(millis, options));
92
+ return new LocalTimeLuxon(luxon_1.DateTime.fromMillis(millis, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
87
93
  }
88
94
  normalize(dateTime) {
89
95
  return luxon_1.DateTime.utc(1970, 1, 1, dateTime.hour, dateTime.minute, dateTime.second);
@@ -107,6 +113,9 @@ class DateTimeLuxon extends LuxonDateTime {
107
113
  static fromISO(value, options) {
108
114
  return new DateTimeLuxon(luxon_1.DateTime.fromISO(value, Object.assign({ setZone: true }, options)));
109
115
  }
116
+ static fromFormat(value, format, options) {
117
+ return new DateTimeLuxon(luxon_1.DateTime.fromFormat(value, format, Object.assign({ setZone: true }, options)));
118
+ }
110
119
  static fromJSDate(date, options) {
111
120
  return new DateTimeLuxon(luxon_1.DateTime.fromJSDate(date, options));
112
121
  }
@@ -140,6 +149,9 @@ class DateTimeUtcLuxon extends LuxonDateTime {
140
149
  static fromISO(value, options) {
141
150
  return new DateTimeUtcLuxon(luxon_1.DateTime.fromISO(value, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
142
151
  }
152
+ static fromFormat(value, format, options) {
153
+ return new DateTimeUtcLuxon(luxon_1.DateTime.fromFormat(value, format, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
154
+ }
143
155
  static fromJSDate(date, options) {
144
156
  return new DateTimeUtcLuxon(luxon_1.DateTime.fromJSDate(date, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
145
157
  }
@@ -167,6 +179,9 @@ class DateTimeMillisLuxon extends LuxonDateTime {
167
179
  static fromISO(value, options) {
168
180
  return new DateTimeMillisLuxon(luxon_1.DateTime.fromISO(value, Object.assign({ setZone: true }, options)));
169
181
  }
182
+ static fromFormat(value, format, options) {
183
+ return new DateTimeMillisLuxon(luxon_1.DateTime.fromFormat(value, format, Object.assign({ setZone: true }, options)));
184
+ }
170
185
  static fromJSDate(date, options) {
171
186
  return new DateTimeMillisLuxon(luxon_1.DateTime.fromJSDate(date, options));
172
187
  }
@@ -200,6 +215,9 @@ class DateTimeMillisUtcLuxon extends LuxonDateTime {
200
215
  static fromISO(value, options) {
201
216
  return new DateTimeMillisUtcLuxon(luxon_1.DateTime.fromISO(value, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
202
217
  }
218
+ static fromFormat(value, format, options) {
219
+ return new DateTimeMillisUtcLuxon(luxon_1.DateTime.fromFormat(value, format, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
220
+ }
203
221
  static fromJSDate(date, options) {
204
222
  return new DateTimeMillisUtcLuxon(luxon_1.DateTime.fromJSDate(date, Object.assign({ zone: luxon_1.FixedOffsetZone.utcInstance }, options)));
205
223
  }
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const luxon_1 = require("luxon");
4
- console.log(luxon_1.DateTime.fromISO('2022-02-27').set({ day: 31 }).day);
4
+ console.log(luxon_1.DateTime.fromSQL('09:24:15').toJSON());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@finnair/v-validation-luxon",
3
- "version": "1.1.0-alpha.7",
3
+ "version": "1.1.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": "^1.1.0-alpha.6",
29
- "@finnair/v-validation": "^1.1.0-alpha.6",
28
+ "@finnair/path": "^1.1.0",
29
+ "@finnair/v-validation": "^1.1.0",
30
30
  "luxon": "3.0.1"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/luxon": "3.0.0"
34
34
  },
35
- "gitHead": "b270437f1a403fbb012297653f05d7d6d732fa76"
35
+ "gitHead": "860263e814c178790766b48e22d8a8995e33ae40"
36
36
  }