@finnair/v-validation-luxon 3.0.0-alpha.2 → 3.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 CHANGED
@@ -3,6 +3,14 @@
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
+ # [3.0.0](https://github.com/finnair/v-validation/compare/v2.0.0...v3.0.0) (2022-10-03)
7
+
8
+ **Note:** Version bump only for package @finnair/v-validation-luxon
9
+
10
+
11
+
12
+
13
+
6
14
  # [3.0.0-alpha.2](https://github.com/finnair/v-validation/compare/v2.0.0...v3.0.0-alpha.2) (2022-09-28)
7
15
 
8
16
 
package/dist/Vluxon.d.ts CHANGED
@@ -4,25 +4,26 @@ 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 {
7
- value: any;
8
- path: Path;
9
- ctx: ValidationContext;
10
7
  type: string;
11
8
  pattern: RegExp;
12
9
  proto?: any;
13
10
  parser: (value: string, match: RegExpExecArray) => DateTime;
14
11
  }
15
- export declare function validateLuxon({ value, path, ctx, type, proto, pattern, parser }: ValidateLuxonParams): PromiseLike<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;
12
+ export declare class LuxonValidator extends Validator {
13
+ params: ValidateLuxonParams;
14
+ constructor(params: ValidateLuxonParams);
15
+ validatePath(value: any, path: Path, ctx: ValidationContext): PromiseLike<ValidationResult>;
16
+ }
17
+ declare function localDate(options?: DateTimeOptions): LuxonValidator;
18
+ declare function localTime(options?: DateTimeOptions): LuxonValidator;
19
+ declare function dateTime(options?: DateTimeOptions): LuxonValidator;
20
+ declare function dateTimeUtc(options?: DateTimeOptions): LuxonValidator;
21
+ declare function dateTimeMillis(options?: DateTimeOptions): LuxonValidator;
22
+ declare function dateTimeMillisUtc(options?: DateTimeOptions): LuxonValidator;
23
+ declare function dateTimeFromISO(options?: DateTimeOptions): LuxonValidator;
24
+ declare function dateTimeFromRFC2822(options?: DateTimeOptions): LuxonValidator;
25
+ declare function dateTimeFromHTTP(options?: DateTimeOptions): LuxonValidator;
26
+ declare function dateTimeFromSQL(options?: DateTimeOptions): LuxonValidator;
26
27
  export interface ValidateLuxonNumberParams {
27
28
  value: any;
28
29
  path: Path;
package/dist/Vluxon.js CHANGED
@@ -9,146 +9,125 @@ 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.validateLuxonNumber = exports.validateLuxon = void 0;
12
+ exports.Vluxon = exports.DurationValidator = exports.validateLuxonNumber = exports.LuxonValidator = 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
- function validateLuxon({ value, path, ctx, type, proto, pattern, parser }) {
17
- if ((0, v_validation_1.isNullOrUndefined)(value)) {
18
- return ctx.failurePromise(v_validation_1.defaultViolations.notNull(path), value);
16
+ class LuxonValidator extends v_validation_1.Validator {
17
+ constructor(params) {
18
+ super();
19
+ this.params = params;
20
+ Object.freeze(params);
21
+ Object.freeze(this);
19
22
  }
20
- if (proto && value instanceof proto) {
21
- return ctx.successPromise(value);
22
- }
23
- else if (luxon_1.DateTime.isDateTime(value)) {
24
- if (value.isValid) {
25
- return success(value);
23
+ validatePath(value, path, ctx) {
24
+ const params = this.params;
25
+ if ((0, v_validation_1.isNullOrUndefined)(value)) {
26
+ return ctx.failurePromise(v_validation_1.defaultViolations.notNull(path), value);
26
27
  }
27
- }
28
- else if ((0, v_validation_1.isString)(value)) {
29
- const match = pattern.exec(value);
30
- if (match) {
31
- const dateTime = parser(value, match);
32
- if (dateTime.isValid) {
33
- return success(dateTime);
28
+ if (params.proto && value instanceof params.proto) {
29
+ return ctx.successPromise(value);
30
+ }
31
+ else if (luxon_1.DateTime.isDateTime(value)) {
32
+ if (value.isValid) {
33
+ return success(value);
34
34
  }
35
35
  }
36
- }
37
- return ctx.failurePromise(v_validation_1.defaultViolations.date(value, path, type), value);
38
- function success(dateTime) {
39
- return ctx.successPromise(proto ? new proto(dateTime) : dateTime);
36
+ else if ((0, v_validation_1.isString)(value)) {
37
+ const match = params.pattern.exec(value);
38
+ if (match) {
39
+ const dateTime = params.parser(value, match);
40
+ if (dateTime.isValid) {
41
+ return success(dateTime);
42
+ }
43
+ }
44
+ }
45
+ return ctx.failurePromise(v_validation_1.defaultViolations.date(value, path, params.type), value);
46
+ function success(dateTime) {
47
+ return ctx.successPromise(params.proto ? new params.proto(dateTime) : dateTime);
48
+ }
40
49
  }
41
50
  }
42
- exports.validateLuxon = validateLuxon;
51
+ exports.LuxonValidator = LuxonValidator;
43
52
  const datePattern = /^\d{4}-\d{2}-\d{2}$/;
44
53
  function localDate(options = { setZone: true }) {
45
- return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
46
- value,
47
- path,
48
- ctx,
54
+ return new LuxonValidator({
49
55
  type: 'Date',
50
56
  proto: luxon_2.LocalDateLuxon,
51
57
  pattern: datePattern,
52
58
  parser: (value) => luxon_1.DateTime.fromISO(value, { zone: luxon_1.FixedOffsetZone.utcInstance }),
53
- }));
59
+ });
54
60
  }
55
61
  const timePattern = /^\d{2}:\d{2}:\d{2}$/;
56
62
  function localTime(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
57
- return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
58
- value,
59
- path,
60
- ctx,
63
+ return new LuxonValidator({
61
64
  type: 'Time',
62
65
  proto: luxon_2.LocalTimeLuxon,
63
66
  pattern: timePattern,
64
67
  parser: (value) => luxon_1.DateTime.fromISO(value, options),
65
- }));
68
+ });
66
69
  }
67
70
  const dateTimePattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:Z|[+-]\d{2}(?::?\d{2})?)$/;
68
71
  function dateTime(options = { setZone: true }) {
69
- return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
70
- value,
71
- path,
72
- ctx,
72
+ return new LuxonValidator({
73
73
  type: 'DateTime',
74
74
  proto: luxon_2.DateTimeLuxon,
75
75
  pattern: dateTimePattern,
76
76
  parser: (value) => luxon_1.DateTime.fromISO(value, options),
77
- }));
77
+ });
78
78
  }
79
79
  function dateTimeUtc(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
80
- return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
81
- value,
82
- path,
83
- ctx,
80
+ return new LuxonValidator({
84
81
  type: 'DateTime',
85
82
  proto: luxon_2.DateTimeUtcLuxon,
86
83
  pattern: dateTimePattern,
87
84
  parser: (value) => luxon_1.DateTime.fromISO(value, options),
88
- }));
85
+ });
89
86
  }
90
87
  const dateTimeMillisPattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(?:Z|[+-]\d{2}(?::?\d{2})?)$/;
91
88
  function dateTimeMillis(options = { setZone: true }) {
92
- return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
93
- value,
94
- path,
95
- ctx,
89
+ return new LuxonValidator({
96
90
  type: 'DateTimeMillis',
97
91
  proto: luxon_2.DateTimeMillisLuxon,
98
92
  pattern: dateTimeMillisPattern,
99
93
  parser: (value) => luxon_1.DateTime.fromISO(value, options),
100
- }));
94
+ });
101
95
  }
102
96
  function dateTimeMillisUtc(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
103
- return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
104
- value,
105
- path,
106
- ctx,
97
+ return new LuxonValidator({
107
98
  type: 'DateTimeMillis',
108
99
  proto: luxon_2.DateTimeMillisUtcLuxon,
109
100
  pattern: dateTimeMillisPattern,
110
101
  parser: (value) => luxon_1.DateTime.fromISO(value, options),
111
- }));
102
+ });
112
103
  }
113
104
  function dateTimeFromISO(options = { setZone: true }) {
114
- return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
115
- value,
116
- path,
117
- ctx,
105
+ return new LuxonValidator({
118
106
  type: 'ISODateTime',
119
107
  pattern: /./,
120
108
  parser: (value) => luxon_1.DateTime.fromISO(value, options),
121
- }));
109
+ });
122
110
  }
123
111
  function dateTimeFromRFC2822(options = { setZone: true }) {
124
- return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
125
- value,
126
- path,
127
- ctx,
112
+ return new LuxonValidator({
128
113
  type: 'RFC2822DateTime',
129
114
  pattern: /./,
130
115
  parser: (value) => luxon_1.DateTime.fromRFC2822(value, options),
131
- }));
116
+ });
132
117
  }
133
118
  function dateTimeFromHTTP(options = { setZone: true }) {
134
- return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
135
- value,
136
- path,
137
- ctx,
119
+ return new LuxonValidator({
138
120
  type: 'HTTPDateTime',
139
121
  pattern: /./,
140
122
  parser: (value) => luxon_1.DateTime.fromHTTP(value, options),
141
- }));
123
+ });
142
124
  }
143
125
  function dateTimeFromSQL(options = { zone: luxon_1.FixedOffsetZone.utcInstance }) {
144
- return v_validation_1.V.fn((value, path, ctx) => validateLuxon({
145
- value,
146
- path,
147
- ctx,
126
+ return new LuxonValidator({
148
127
  type: 'SQLDateTime',
149
128
  pattern: /./,
150
129
  parser: (value) => luxon_1.DateTime.fromSQL(value, options),
151
- }));
130
+ });
152
131
  }
153
132
  function validateLuxonNumber({ value, path, ctx, type, parser }) {
154
133
  return __awaiter(this, void 0, void 0, function* () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@finnair/v-validation-luxon",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0",
4
4
  "private": false,
5
5
  "description": "Luxon validators",
6
6
  "main": "dist/index.js",
@@ -25,12 +25,11 @@
25
25
  "build": "tsc -b ."
26
26
  },
27
27
  "dependencies": {
28
- "@finnair/path": "^3.0.0-alpha.2",
29
- "@finnair/v-validation": "^3.0.0-alpha.2",
30
- "luxon": "3.0.3"
28
+ "@finnair/path": "^3.0.0",
29
+ "@finnair/v-validation": "^3.0.0"
31
30
  },
32
- "devDependencies": {
33
- "@types/luxon": "3.0.1"
31
+ "peerDependencies": {
32
+ "luxon": "^3.0.0"
34
33
  },
35
- "gitHead": "d42bd7583bb684429e48ce8e5db3938cb95ffd5f"
34
+ "gitHead": "597b85f1fcef99722bccf47c6d30260973d6d88c"
36
35
  }