@availity/yup 3.2.0 → 4.1.1-alpha.2
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 +38 -0
- package/lib/date.js +7 -7
- package/lib/dateRange.js +58 -80
- package/lib/index.js +9 -9
- package/package.json +4 -4
- package/src/date.js +6 -6
- package/src/dateRange.js +28 -43
- package/src/index.js +8 -8
- package/types/date.d.ts +15 -17
- package/types/dateRange.d.ts +31 -33
- package/types/index.d.ts +10 -7
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,44 @@
|
|
|
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
|
+
# 4.1.0 (2021-11-18)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **yup:** allow custom type error message for dateRange ([cc36cde](https://github.com/Availity/sdk-js/commit/cc36cde7d403f9787343fcecbf1d0fd9d8ac6bf7))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# 4.0.0 (2021-11-02)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* **yup:** package change for major version update ([9ad040f](https://github.com/Availity/sdk-js/commit/9ad040ff7a4f7cbe7d5fdd37932d953287db805c)), closes [/github.com/jquense/yup/blob/375f1b9ed41f5043e123ea87a01a2dfe333c3927/CHANGELOG.md#0320-2020-12-03](https://github.com//github.com/jquense/yup/blob/375f1b9ed41f5043e123ea87a01a2dfe333c3927/CHANGELOG.md/issues/0320-2020-12-03)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### BREAKING CHANGES
|
|
26
|
+
|
|
27
|
+
* **yup:** the version of yup this package requires contains breaking changes from 0.32.0 onward
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
## [3.2.1](https://github.com/Availity/sdk-js/compare/@availity/yup@3.2.0...@availity/yup@3.2.1) (2021-10-29)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
### Bug Fixes
|
|
37
|
+
|
|
38
|
+
* **yup:** fixes start date before end date logic ([6cc0ff7](https://github.com/Availity/sdk-js/commit/6cc0ff736358bc5aaf738a84d8abb258e85f57f4))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
6
44
|
# 3.2.0 (2021-10-22)
|
|
7
45
|
|
|
8
46
|
|
package/lib/date.js
CHANGED
|
@@ -34,8 +34,8 @@ var defaultOpts = {
|
|
|
34
34
|
};
|
|
35
35
|
var formats = ['YYYY-MM-DD', 'MMDDYYYY', 'YYYYMMDD', 'MM-DD-YYYY'];
|
|
36
36
|
|
|
37
|
-
var AvDateSchema = /*#__PURE__*/function (
|
|
38
|
-
(0, _inherits2["default"])(AvDateSchema,
|
|
37
|
+
var AvDateSchema = /*#__PURE__*/function (_MixedSchema) {
|
|
38
|
+
(0, _inherits2["default"])(AvDateSchema, _MixedSchema);
|
|
39
39
|
|
|
40
40
|
var _super = (0, _createSuper2["default"])(AvDateSchema);
|
|
41
41
|
|
|
@@ -53,8 +53,8 @@ var AvDateSchema = /*#__PURE__*/function (_mixed) {
|
|
|
53
53
|
_this.format = format;
|
|
54
54
|
_this.getValidDate = _this.getValidDate.bind((0, _assertThisInitialized2["default"])(_this));
|
|
55
55
|
|
|
56
|
-
_this.withMutation(function () {
|
|
57
|
-
if (!
|
|
56
|
+
_this.withMutation(function (schema) {
|
|
57
|
+
if (!schema.tests.some(function (test) {
|
|
58
58
|
var _test$OPTIONS;
|
|
59
59
|
|
|
60
60
|
return (test === null || test === void 0 ? void 0 : (_test$OPTIONS = test.OPTIONS) === null || _test$OPTIONS === void 0 ? void 0 : _test$OPTIONS.name) === 'typeError';
|
|
@@ -62,8 +62,8 @@ var AvDateSchema = /*#__PURE__*/function (_mixed) {
|
|
|
62
62
|
(0, _get2["default"])((_thisSuper = (0, _assertThisInitialized2["default"])(_this), (0, _getPrototypeOf2["default"])(AvDateSchema.prototype)), "typeError", _thisSuper).call(_thisSuper, 'Date is invalid.');
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
return
|
|
65
|
+
schema.transform(function mutate(value) {
|
|
66
|
+
return schema.getValidDate(value);
|
|
67
67
|
});
|
|
68
68
|
});
|
|
69
69
|
|
|
@@ -167,6 +167,6 @@ var AvDateSchema = /*#__PURE__*/function (_mixed) {
|
|
|
167
167
|
}
|
|
168
168
|
}]);
|
|
169
169
|
return AvDateSchema;
|
|
170
|
-
}(_yup.
|
|
170
|
+
}(_yup.MixedSchema);
|
|
171
171
|
|
|
172
172
|
exports["default"] = AvDateSchema;
|
package/lib/dateRange.js
CHANGED
|
@@ -13,32 +13,28 @@ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/cl
|
|
|
13
13
|
|
|
14
14
|
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
15
15
|
|
|
16
|
-
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
|
17
|
-
|
|
18
|
-
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
|
|
19
|
-
|
|
20
16
|
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
|
21
17
|
|
|
22
18
|
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
|
|
23
19
|
|
|
24
|
-
var _moment = _interopRequireDefault(require("moment"));
|
|
25
|
-
|
|
26
20
|
var _yup = require("yup");
|
|
27
21
|
|
|
22
|
+
var _moment = _interopRequireDefault(require("moment"));
|
|
23
|
+
|
|
28
24
|
var _get = _interopRequireDefault(require("lodash/get"));
|
|
29
25
|
|
|
30
26
|
var _merge2 = _interopRequireDefault(require("lodash/merge"));
|
|
31
27
|
|
|
32
28
|
var defaultOptions = {
|
|
33
29
|
startKey: 'startDate',
|
|
34
|
-
endKey: 'endDate'
|
|
35
|
-
format: 'MM/DD/YYYY'
|
|
30
|
+
endKey: 'endDate'
|
|
36
31
|
};
|
|
37
32
|
var defaultValue = {};
|
|
38
|
-
var
|
|
33
|
+
var displayFormat = 'MM/DD/YYYY';
|
|
34
|
+
var formats = [displayFormat, 'YYYY-MM-DD', 'MMDDYYYY', 'YYYYMMDD'];
|
|
39
35
|
|
|
40
|
-
var DateRangeSchema = /*#__PURE__*/function (
|
|
41
|
-
(0, _inherits2["default"])(DateRangeSchema,
|
|
36
|
+
var DateRangeSchema = /*#__PURE__*/function (_MixedSchema) {
|
|
37
|
+
(0, _inherits2["default"])(DateRangeSchema, _MixedSchema);
|
|
42
38
|
|
|
43
39
|
var _super = (0, _createSuper2["default"])(DateRangeSchema);
|
|
44
40
|
|
|
@@ -52,27 +48,24 @@ var DateRangeSchema = /*#__PURE__*/function (_mixed) {
|
|
|
52
48
|
|
|
53
49
|
var _merge = (0, _merge2["default"])({}, defaultOptions, options),
|
|
54
50
|
startKey = _merge.startKey,
|
|
55
|
-
endKey = _merge.endKey
|
|
56
|
-
format = _merge.format;
|
|
51
|
+
endKey = _merge.endKey;
|
|
57
52
|
|
|
58
53
|
_this.startKey = startKey;
|
|
59
|
-
_this.endKey = endKey;
|
|
60
|
-
_this.format = format;
|
|
61
|
-
_this.getValidDate = _this.getValidDate.bind((0, _assertThisInitialized2["default"])(_this));
|
|
54
|
+
_this.endKey = endKey; // this.format = format;
|
|
62
55
|
|
|
63
|
-
_this.withMutation(function () {
|
|
64
|
-
|
|
56
|
+
_this.withMutation(function (schema) {
|
|
57
|
+
schema.transform(function mutate(value) {
|
|
65
58
|
var start = (0, _get["default"])(value, startKey);
|
|
66
59
|
var end = (0, _get["default"])(value, endKey);
|
|
67
60
|
var startDate;
|
|
68
61
|
var endDate;
|
|
69
62
|
|
|
70
63
|
if (start) {
|
|
71
|
-
startDate =
|
|
64
|
+
startDate = schema.getValidDate(start);
|
|
72
65
|
}
|
|
73
66
|
|
|
74
67
|
if (end) {
|
|
75
|
-
endDate =
|
|
68
|
+
endDate = schema.getValidDate(end);
|
|
76
69
|
}
|
|
77
70
|
|
|
78
71
|
return {
|
|
@@ -82,53 +75,38 @@ var DateRangeSchema = /*#__PURE__*/function (_mixed) {
|
|
|
82
75
|
});
|
|
83
76
|
});
|
|
84
77
|
|
|
85
|
-
return
|
|
86
|
-
message: 'Start date must come before end date.',
|
|
87
|
-
name: 'startBeforeEnd',
|
|
88
|
-
exclusive: true,
|
|
89
|
-
test: function test() {
|
|
90
|
-
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
91
|
-
startDate = _ref.startDate,
|
|
92
|
-
endDate = _ref.endDate;
|
|
93
|
-
|
|
94
|
-
if (!startDate || !endDate) {
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return startDate.isSameOrBefore(endDate);
|
|
99
|
-
}
|
|
100
|
-
}));
|
|
78
|
+
return _this;
|
|
101
79
|
}
|
|
102
80
|
|
|
103
81
|
(0, _createClass2["default"])(DateRangeSchema, [{
|
|
104
82
|
key: "getValidDate",
|
|
105
83
|
value: function getValidDate(value) {
|
|
106
|
-
return (0, _moment["default"])(value,
|
|
84
|
+
return (0, _moment["default"])(value, formats, true);
|
|
107
85
|
}
|
|
108
86
|
}, {
|
|
109
87
|
key: "distance",
|
|
110
88
|
value: function distance() {
|
|
111
|
-
var
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
var minValue =
|
|
116
|
-
|
|
117
|
-
minUnits =
|
|
118
|
-
minErrorMessage =
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
var maxValue =
|
|
122
|
-
|
|
123
|
-
maxUnits =
|
|
124
|
-
maxErrorMessage =
|
|
89
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
90
|
+
_ref$min = _ref.min;
|
|
91
|
+
|
|
92
|
+
_ref$min = _ref$min === void 0 ? {} : _ref$min;
|
|
93
|
+
var minValue = _ref$min.value,
|
|
94
|
+
_ref$min$units = _ref$min.units,
|
|
95
|
+
minUnits = _ref$min$units === void 0 ? 'day' : _ref$min$units,
|
|
96
|
+
minErrorMessage = _ref$min.errorMessage,
|
|
97
|
+
_ref$max = _ref.max;
|
|
98
|
+
_ref$max = _ref$max === void 0 ? {} : _ref$max;
|
|
99
|
+
var maxValue = _ref$max.value,
|
|
100
|
+
_ref$max$units = _ref$max.units,
|
|
101
|
+
maxUnits = _ref$max$units === void 0 ? 'day' : _ref$max$units,
|
|
102
|
+
maxErrorMessage = _ref$max.errorMessage;
|
|
125
103
|
return this.test({
|
|
126
104
|
name: 'distance',
|
|
127
105
|
exclusive: true,
|
|
128
106
|
test: function test() {
|
|
129
|
-
var
|
|
130
|
-
endDate =
|
|
131
|
-
startDate =
|
|
107
|
+
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
108
|
+
endDate = _ref2.endDate,
|
|
109
|
+
startDate = _ref2.startDate;
|
|
132
110
|
|
|
133
111
|
if (!minValue && !maxValue || !startDate || !endDate) return true;
|
|
134
112
|
|
|
@@ -153,56 +131,51 @@ var DateRangeSchema = /*#__PURE__*/function (_mixed) {
|
|
|
153
131
|
}, {
|
|
154
132
|
key: "min",
|
|
155
133
|
value: function min(_min, message) {
|
|
156
|
-
|
|
134
|
+
// it works for date, but not daterange. maybe that can tell us more about what is going on
|
|
157
135
|
var minDate = this.getValidDate(_min);
|
|
158
136
|
return this.test({
|
|
159
|
-
message: message || "Date Range must start on or after ".concat(minDate.format(
|
|
137
|
+
message: message || "Date Range must start on or after ".concat(minDate.format(displayFormat)),
|
|
160
138
|
name: 'min',
|
|
161
139
|
exclusive: true,
|
|
162
140
|
params: {
|
|
163
141
|
min: _min
|
|
164
142
|
},
|
|
165
143
|
test: function test() {
|
|
166
|
-
var
|
|
167
|
-
startDate =
|
|
168
|
-
|
|
169
|
-
if (!startDate || !_min) {
|
|
170
|
-
return true;
|
|
171
|
-
}
|
|
144
|
+
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
145
|
+
startDate = _ref3.startDate;
|
|
172
146
|
|
|
173
|
-
|
|
147
|
+
// is minDate valid and is it before the given date
|
|
148
|
+
return !startDate || !_min ? true : minDate.isValid() && minDate.isSameOrBefore(startDate);
|
|
174
149
|
}
|
|
175
150
|
});
|
|
176
151
|
}
|
|
177
152
|
}, {
|
|
178
153
|
key: "max",
|
|
179
154
|
value: function max(_max, message) {
|
|
180
|
-
var format = this.format;
|
|
181
155
|
var maxDate = this.getValidDate(_max);
|
|
182
156
|
return this.test({
|
|
183
|
-
message: message || "Date Range must end on or before ".concat(maxDate.format(
|
|
157
|
+
message: message || "Date Range must end on or before ".concat(maxDate.format(displayFormat)),
|
|
184
158
|
name: 'max',
|
|
185
159
|
exclusive: true,
|
|
186
160
|
params: {
|
|
187
161
|
max: _max
|
|
188
162
|
},
|
|
189
163
|
test: function test() {
|
|
190
|
-
var
|
|
191
|
-
endDate =
|
|
164
|
+
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
165
|
+
endDate = _ref4.endDate;
|
|
192
166
|
|
|
193
|
-
|
|
194
|
-
return maxDate.isValid() && maxDate.isSameOrAfter(endDate);
|
|
167
|
+
// is maxDate valid and is it after the given date
|
|
168
|
+
return !endDate || !_max ? true : maxDate.isValid() && maxDate.isSameOrAfter(endDate);
|
|
195
169
|
}
|
|
196
170
|
});
|
|
197
171
|
}
|
|
198
172
|
}, {
|
|
199
173
|
key: "between",
|
|
200
174
|
value: function between(min, max, message) {
|
|
201
|
-
var format = this.format;
|
|
202
175
|
var minDate = this.getValidDate(min);
|
|
203
176
|
var maxDate = this.getValidDate(max);
|
|
204
177
|
return this.test({
|
|
205
|
-
message: message || "Date Range must be between ".concat(minDate.format(
|
|
178
|
+
message: message || "Date Range must be between ".concat(minDate.format(displayFormat), " and ").concat(maxDate.format(displayFormat)),
|
|
206
179
|
name: 'between',
|
|
207
180
|
exclusive: true,
|
|
208
181
|
params: {
|
|
@@ -210,9 +183,9 @@ var DateRangeSchema = /*#__PURE__*/function (_mixed) {
|
|
|
210
183
|
max: max
|
|
211
184
|
},
|
|
212
185
|
test: function test() {
|
|
213
|
-
var
|
|
214
|
-
startDate =
|
|
215
|
-
endDate =
|
|
186
|
+
var _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
187
|
+
startDate = _ref5.startDate,
|
|
188
|
+
endDate = _ref5.endDate;
|
|
216
189
|
|
|
217
190
|
if (!startDate || !endDate || !min || !max) return true;
|
|
218
191
|
return maxDate.isValid() && minDate.isValid() && maxDate.isSameOrAfter(endDate) && minDate.isSameOrBefore(startDate);
|
|
@@ -230,9 +203,9 @@ var DateRangeSchema = /*#__PURE__*/function (_mixed) {
|
|
|
230
203
|
exclusive: true,
|
|
231
204
|
message: msg || 'This field is required.',
|
|
232
205
|
test: function test() {
|
|
233
|
-
var
|
|
234
|
-
startDate =
|
|
235
|
-
endDate =
|
|
206
|
+
var _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
207
|
+
startDate = _ref6.startDate,
|
|
208
|
+
endDate = _ref6.endDate;
|
|
236
209
|
|
|
237
210
|
return !_isRequired || startDate && endDate;
|
|
238
211
|
}
|
|
@@ -240,7 +213,8 @@ var DateRangeSchema = /*#__PURE__*/function (_mixed) {
|
|
|
240
213
|
}
|
|
241
214
|
}, {
|
|
242
215
|
key: "typeError",
|
|
243
|
-
value: function typeError() {
|
|
216
|
+
value: function typeError(_ref7) {
|
|
217
|
+
var message = _ref7.message;
|
|
244
218
|
return this.test({
|
|
245
219
|
name: 'typeError',
|
|
246
220
|
exclusive: true,
|
|
@@ -252,7 +226,11 @@ var DateRangeSchema = /*#__PURE__*/function (_mixed) {
|
|
|
252
226
|
var errors = [];
|
|
253
227
|
|
|
254
228
|
if ((!startDate || !endDate) && (startDate || endDate)) {
|
|
255
|
-
errors.push('Start and End Date are required.');
|
|
229
|
+
errors.push(message || 'Start and End Date are required.');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (startDate && endDate && !startDate.isSameOrBefore(endDate)) {
|
|
233
|
+
errors.push('Start date must come before end date.');
|
|
256
234
|
}
|
|
257
235
|
|
|
258
236
|
if (startDate && !startDate.isValid()) {
|
|
@@ -281,6 +259,6 @@ var DateRangeSchema = /*#__PURE__*/function (_mixed) {
|
|
|
281
259
|
}
|
|
282
260
|
}]);
|
|
283
261
|
return DateRangeSchema;
|
|
284
|
-
}(_yup.
|
|
262
|
+
}(_yup.MixedSchema);
|
|
285
263
|
|
|
286
264
|
exports["default"] = DateRangeSchema;
|
package/lib/index.js
CHANGED
|
@@ -9,16 +9,16 @@ exports.dateRange = exports.avDate = void 0;
|
|
|
9
9
|
|
|
10
10
|
var _yup = require("yup");
|
|
11
11
|
|
|
12
|
+
var _date = _interopRequireDefault(require("./date"));
|
|
13
|
+
|
|
14
|
+
var _dateRange = _interopRequireDefault(require("./dateRange"));
|
|
15
|
+
|
|
12
16
|
var _isRequired = _interopRequireDefault(require("./isRequired"));
|
|
13
17
|
|
|
14
18
|
var _npi = _interopRequireDefault(require("./npi"));
|
|
15
19
|
|
|
16
20
|
var _phone = _interopRequireDefault(require("./phone"));
|
|
17
21
|
|
|
18
|
-
var _date = _interopRequireDefault(require("./date"));
|
|
19
|
-
|
|
20
|
-
var _dateRange = _interopRequireDefault(require("./dateRange"));
|
|
21
|
-
|
|
22
22
|
var avDate = function avDate(opts) {
|
|
23
23
|
return new _date["default"](opts);
|
|
24
24
|
};
|
|
@@ -30,11 +30,11 @@ var dateRange = function dateRange(opts) {
|
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
exports.dateRange = dateRange;
|
|
33
|
-
(0, _yup.addMethod)(_yup.string, 'isRequired', _isRequired["default"]);
|
|
34
|
-
(0, _yup.addMethod)(_yup.number, 'isRequired', _isRequired["default"]);
|
|
35
33
|
(0, _yup.addMethod)(_yup.array, 'isRequired', _isRequired["default"]);
|
|
34
|
+
(0, _yup.addMethod)(_yup.number, 'isRequired', _isRequired["default"]);
|
|
36
35
|
(0, _yup.addMethod)(_yup.object, 'isRequired', _isRequired["default"]);
|
|
37
|
-
(0, _yup.addMethod)(_yup.string, '
|
|
36
|
+
(0, _yup.addMethod)(_yup.string, 'isRequired', _isRequired["default"]);
|
|
38
37
|
(0, _yup.addMethod)(_yup.number, 'npi', _npi["default"]);
|
|
39
|
-
(0, _yup.addMethod)(_yup.string, '
|
|
40
|
-
(0, _yup.addMethod)(_yup.number, 'phone', _phone["default"]);
|
|
38
|
+
(0, _yup.addMethod)(_yup.string, 'npi', _npi["default"]);
|
|
39
|
+
(0, _yup.addMethod)(_yup.number, 'phone', _phone["default"]);
|
|
40
|
+
(0, _yup.addMethod)(_yup.string, 'phone', _phone["default"]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@availity/yup",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.1.1-alpha.2+3679957",
|
|
4
4
|
"description": "Additional Methods for Yup validation library",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "src/index.js",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"moment": "^2.24.0",
|
|
24
|
-
"yup": "^0.
|
|
24
|
+
"yup": "^0.32.9"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"moment": "^2.24.0",
|
|
28
|
-
"yup": "^0.
|
|
28
|
+
"yup": "^0.32.9"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "36799574f86674e1a41228c10bfba8d303958e02"
|
|
31
31
|
}
|
package/src/date.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MixedSchema } from 'yup';
|
|
2
2
|
import moment from 'moment';
|
|
3
3
|
|
|
4
4
|
const defaultOpts = {
|
|
@@ -7,7 +7,7 @@ const defaultOpts = {
|
|
|
7
7
|
|
|
8
8
|
const formats = ['YYYY-MM-DD', 'MMDDYYYY', 'YYYYMMDD', 'MM-DD-YYYY'];
|
|
9
9
|
|
|
10
|
-
export default class AvDateSchema extends
|
|
10
|
+
export default class AvDateSchema extends MixedSchema {
|
|
11
11
|
constructor({ format = 'MM/DD/YYYY' } = defaultOpts) {
|
|
12
12
|
super({
|
|
13
13
|
type: 'avDate',
|
|
@@ -16,12 +16,12 @@ export default class AvDateSchema extends mixed {
|
|
|
16
16
|
this.format = format;
|
|
17
17
|
this.getValidDate = this.getValidDate.bind(this);
|
|
18
18
|
|
|
19
|
-
this.withMutation(() => {
|
|
20
|
-
if (!
|
|
19
|
+
this.withMutation((schema) => {
|
|
20
|
+
if (!schema.tests.some((test) => test?.OPTIONS?.name === 'typeError')) {
|
|
21
21
|
super.typeError('Date is invalid.');
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
return
|
|
23
|
+
schema.transform(function mutate(value) {
|
|
24
|
+
return schema.getValidDate(value);
|
|
25
25
|
});
|
|
26
26
|
});
|
|
27
27
|
}
|
package/src/dateRange.js
CHANGED
|
@@ -1,66 +1,52 @@
|
|
|
1
|
+
import { MixedSchema, ValidationError } from 'yup';
|
|
1
2
|
import moment from 'moment';
|
|
2
|
-
import { mixed, ValidationError } from 'yup';
|
|
3
3
|
import get from 'lodash/get';
|
|
4
4
|
import merge from 'lodash/merge';
|
|
5
5
|
|
|
6
6
|
const defaultOptions = {
|
|
7
7
|
startKey: 'startDate',
|
|
8
8
|
endKey: 'endDate',
|
|
9
|
-
format: 'MM/DD/YYYY',
|
|
10
9
|
};
|
|
11
10
|
|
|
12
11
|
const defaultValue = {};
|
|
13
12
|
|
|
14
|
-
const
|
|
13
|
+
const displayFormat = 'MM/DD/YYYY';
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
const formats = [displayFormat, 'YYYY-MM-DD', 'MMDDYYYY', 'YYYYMMDD'];
|
|
16
|
+
|
|
17
|
+
export default class DateRangeSchema extends MixedSchema {
|
|
17
18
|
constructor(options) {
|
|
18
19
|
super({
|
|
19
20
|
type: 'dateRange',
|
|
20
21
|
});
|
|
21
22
|
|
|
22
|
-
const { startKey, endKey
|
|
23
|
+
const { startKey, endKey } = merge({}, defaultOptions, options);
|
|
23
24
|
|
|
24
25
|
this.startKey = startKey;
|
|
25
26
|
this.endKey = endKey;
|
|
26
|
-
this.format = format;
|
|
27
|
-
this.getValidDate = this.getValidDate.bind(this);
|
|
27
|
+
// this.format = format;
|
|
28
28
|
|
|
29
|
-
this.withMutation(() => {
|
|
30
|
-
|
|
29
|
+
this.withMutation((schema) => {
|
|
30
|
+
schema.transform(function mutate(value) {
|
|
31
31
|
const start = get(value, startKey);
|
|
32
32
|
const end = get(value, endKey);
|
|
33
33
|
|
|
34
34
|
let startDate;
|
|
35
35
|
let endDate;
|
|
36
36
|
if (start) {
|
|
37
|
-
startDate =
|
|
37
|
+
startDate = schema.getValidDate(start);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
if (end) {
|
|
41
|
-
endDate =
|
|
41
|
+
endDate = schema.getValidDate(end);
|
|
42
42
|
}
|
|
43
|
-
|
|
44
43
|
return { startDate, endDate };
|
|
45
44
|
});
|
|
46
45
|
});
|
|
47
|
-
|
|
48
|
-
return this.test({
|
|
49
|
-
message: 'Start date must come before end date.',
|
|
50
|
-
name: 'startBeforeEnd',
|
|
51
|
-
exclusive: true,
|
|
52
|
-
test({ startDate, endDate } = defaultValue) {
|
|
53
|
-
if (!startDate || !endDate) {
|
|
54
|
-
return true;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return startDate.isSameOrBefore(endDate);
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
46
|
}
|
|
61
47
|
|
|
62
48
|
getValidDate(value) {
|
|
63
|
-
return moment(value,
|
|
49
|
+
return moment(value, formats, true);
|
|
64
50
|
}
|
|
65
51
|
|
|
66
52
|
distance({
|
|
@@ -99,48 +85,43 @@ export default class DateRangeSchema extends mixed {
|
|
|
99
85
|
}
|
|
100
86
|
|
|
101
87
|
min(min, message) {
|
|
102
|
-
|
|
103
|
-
|
|
88
|
+
// it works for date, but not daterange. maybe that can tell us more about what is going on
|
|
104
89
|
const minDate = this.getValidDate(min);
|
|
90
|
+
|
|
105
91
|
return this.test({
|
|
106
|
-
message: message || `Date Range must start on or after ${minDate.format(
|
|
92
|
+
message: message || `Date Range must start on or after ${minDate.format(displayFormat)}`,
|
|
107
93
|
name: 'min',
|
|
108
94
|
exclusive: true,
|
|
109
95
|
params: { min },
|
|
110
96
|
test({ startDate } = defaultValue) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
return minDate.isValid() && minDate.isSameOrBefore(startDate);
|
|
97
|
+
// is minDate valid and is it before the given date
|
|
98
|
+
return !startDate || !min ? true : minDate.isValid() && minDate.isSameOrBefore(startDate);
|
|
115
99
|
},
|
|
116
100
|
});
|
|
117
101
|
}
|
|
118
102
|
|
|
119
103
|
max(max, message) {
|
|
120
|
-
const { format } = this;
|
|
121
|
-
|
|
122
104
|
const maxDate = this.getValidDate(max);
|
|
123
105
|
|
|
124
106
|
return this.test({
|
|
125
|
-
message: message || `Date Range must end on or before ${maxDate.format(
|
|
107
|
+
message: message || `Date Range must end on or before ${maxDate.format(displayFormat)}`,
|
|
126
108
|
name: 'max',
|
|
127
109
|
exclusive: true,
|
|
128
110
|
params: { max },
|
|
129
111
|
test({ endDate } = defaultValue) {
|
|
130
|
-
|
|
131
|
-
return maxDate.isValid() && maxDate.isSameOrAfter(endDate);
|
|
112
|
+
// is maxDate valid and is it after the given date
|
|
113
|
+
return !endDate || !max ? true : maxDate.isValid() && maxDate.isSameOrAfter(endDate);
|
|
132
114
|
},
|
|
133
115
|
});
|
|
134
116
|
}
|
|
135
117
|
|
|
136
118
|
between(min, max, message) {
|
|
137
|
-
const { format } = this;
|
|
138
|
-
|
|
139
119
|
const minDate = this.getValidDate(min);
|
|
140
120
|
const maxDate = this.getValidDate(max);
|
|
141
121
|
|
|
142
122
|
return this.test({
|
|
143
|
-
message:
|
|
123
|
+
message:
|
|
124
|
+
message || `Date Range must be between ${minDate.format(displayFormat)} and ${maxDate.format(displayFormat)}`,
|
|
144
125
|
name: 'between',
|
|
145
126
|
exclusive: true,
|
|
146
127
|
params: { min, max },
|
|
@@ -164,7 +145,7 @@ export default class DateRangeSchema extends mixed {
|
|
|
164
145
|
});
|
|
165
146
|
}
|
|
166
147
|
|
|
167
|
-
typeError() {
|
|
148
|
+
typeError({ message }) {
|
|
168
149
|
return this.test({
|
|
169
150
|
name: 'typeError',
|
|
170
151
|
exclusive: true,
|
|
@@ -172,7 +153,11 @@ export default class DateRangeSchema extends mixed {
|
|
|
172
153
|
const errors = [];
|
|
173
154
|
|
|
174
155
|
if ((!startDate || !endDate) && (startDate || endDate)) {
|
|
175
|
-
errors.push('Start and End Date are required.');
|
|
156
|
+
errors.push(message || 'Start and End Date are required.');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (startDate && endDate && !startDate.isSameOrBefore(endDate)) {
|
|
160
|
+
errors.push('Start date must come before end date.');
|
|
176
161
|
}
|
|
177
162
|
|
|
178
163
|
if (startDate && !startDate.isValid()) {
|
package/src/index.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { addMethod, array, number, object, string } from 'yup';
|
|
2
|
+
import AvDateSchema from './date';
|
|
3
|
+
import DateRangeSchema from './dateRange';
|
|
2
4
|
import isRequired from './isRequired';
|
|
3
5
|
import npi from './npi';
|
|
4
6
|
import phone from './phone';
|
|
5
|
-
import AvDate from './date';
|
|
6
|
-
import DateRange from './dateRange';
|
|
7
7
|
|
|
8
|
-
export const avDate = (opts) => new
|
|
9
|
-
export const dateRange = (opts) => new
|
|
8
|
+
export const avDate = (opts) => new AvDateSchema(opts);
|
|
9
|
+
export const dateRange = (opts) => new DateRangeSchema(opts);
|
|
10
10
|
|
|
11
|
-
addMethod(string, 'isRequired', isRequired);
|
|
12
|
-
addMethod(number, 'isRequired', isRequired);
|
|
13
11
|
addMethod(array, 'isRequired', isRequired);
|
|
12
|
+
addMethod(number, 'isRequired', isRequired);
|
|
14
13
|
addMethod(object, 'isRequired', isRequired);
|
|
14
|
+
addMethod(string, 'isRequired', isRequired);
|
|
15
15
|
|
|
16
|
-
addMethod(string, 'npi', npi);
|
|
17
16
|
addMethod(number, 'npi', npi);
|
|
17
|
+
addMethod(string, 'npi', npi);
|
|
18
18
|
|
|
19
|
-
addMethod(string, 'phone', phone);
|
|
20
19
|
addMethod(number, 'phone', phone);
|
|
20
|
+
addMethod(string, 'phone', phone);
|
package/types/date.d.ts
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { MixedSchema } from 'yup';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
interface AvDateSchema<T extends string | null | undefined = string> extends MixedSchema<T> {
|
|
9
|
-
min(date: string, errorMessage?: string): DateRangeSchema<T>;
|
|
10
|
-
max(date: string, errorMessage?: string): DateRangeSchema<T>;
|
|
11
|
-
between(minDate: string, maxDate: string, errorMessage?: string): AvDateSchema<T>;
|
|
12
|
-
isRequired(required?: boolean, errorMessage?: string): AvDateSchema<T>;
|
|
13
|
-
}
|
|
3
|
+
interface DateOpts {
|
|
4
|
+
format?: string | 'MM/DD/YYYY';
|
|
5
|
+
}
|
|
14
6
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
7
|
+
interface AvDateSchema<T extends string | null | undefined = string> extends MixedSchema<T> {
|
|
8
|
+
min(date: string, errorMessage?: string): DateRangeSchema<T>;
|
|
9
|
+
max(date: string, errorMessage?: string): DateRangeSchema<T>;
|
|
10
|
+
between(minDate: string, maxDate: string, errorMessage?: string): AvDateSchema<T>;
|
|
11
|
+
isRequired(required?: boolean, errorMessage?: string): AvDateSchema<T>;
|
|
12
|
+
}
|
|
19
13
|
|
|
20
|
-
|
|
14
|
+
export interface AvDateSchemaConstructor {
|
|
15
|
+
(opts?: DateOpts): AvDateSchema;
|
|
16
|
+
new (opts?: DateOpts): AvDateSchema;
|
|
21
17
|
}
|
|
18
|
+
|
|
19
|
+
export const avDate: AvDateSchemaConstructor;
|
package/types/dateRange.d.ts
CHANGED
|
@@ -1,40 +1,38 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { MixedSchema } from 'yup';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
3
|
+
type DateRangeObject = {
|
|
4
|
+
startDate: string;
|
|
5
|
+
endDate: string;
|
|
6
|
+
};
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
type DistanceValue = {
|
|
16
|
-
value: number;
|
|
17
|
-
units?: string;
|
|
18
|
-
errorMessage?: string;
|
|
19
|
-
};
|
|
8
|
+
interface DateRangeOpts {
|
|
9
|
+
startKey?: string | 'startDate';
|
|
10
|
+
endKey?: string | 'endKey';
|
|
11
|
+
format?: string | 'MM/DD/YYYY';
|
|
12
|
+
}
|
|
20
13
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
type DistanceValue = {
|
|
15
|
+
value: number;
|
|
16
|
+
units?: string;
|
|
17
|
+
errorMessage?: string;
|
|
18
|
+
};
|
|
25
19
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
isRequired(required?: boolean, errorMessage?: string): DateRangeSchema<T>;
|
|
31
|
-
distance(distanceOpts: DistanceOpts): DateRangeSchema<T>;
|
|
32
|
-
}
|
|
20
|
+
type DistanceOpts = {
|
|
21
|
+
min?: DistanceValue;
|
|
22
|
+
max?: DistanceValue;
|
|
23
|
+
};
|
|
33
24
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
interface DateRangeSchema<T extends DateRangeObject | null | undefined = DateRangeObject> extends MixedSchema<T> {
|
|
26
|
+
min(date: string, errorMessage?: string): DateRangeSchema<T>;
|
|
27
|
+
max(date: string, errorMessage?: string): DateRangeSchema<T>;
|
|
28
|
+
between(minDate: string, maxDate: string, errorMessage?: string): DateRangeSchema<T>;
|
|
29
|
+
isRequired(required?: boolean, errorMessage?: string): DateRangeSchema<T>;
|
|
30
|
+
distance(distanceOpts: DistanceOpts): DateRangeSchema<T>;
|
|
31
|
+
}
|
|
38
32
|
|
|
39
|
-
|
|
33
|
+
export interface DateRangeSchemaConstructor {
|
|
34
|
+
(opts?: DateRangeOpts): DateRangeSchema;
|
|
35
|
+
new (opts?: DateRangeOpts): DateRangeSchema;
|
|
40
36
|
}
|
|
37
|
+
|
|
38
|
+
export const dateRange: DateRangeSchemaConstructor;
|
package/types/index.d.ts
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
1
1
|
import * as yup from 'yup';
|
|
2
|
-
|
|
3
|
-
import './
|
|
2
|
+
|
|
3
|
+
import { dateRange } from './dateRange';
|
|
4
|
+
import { avDate } from './date';
|
|
5
|
+
|
|
6
|
+
export { avDate, dateRange };
|
|
4
7
|
|
|
5
8
|
declare module 'yup' {
|
|
6
|
-
interface StringSchema<T extends string | null | undefined = string> extends
|
|
9
|
+
interface StringSchema<T extends string | null | undefined = string> extends yup.StringSchema<T> {
|
|
7
10
|
isRequired(required?: boolean, errorMessage?: string): StringSchema<T>;
|
|
8
11
|
npi(errorMessage?: string): StringSchema<T>;
|
|
9
12
|
phone(errorMessage?: string): StringSchema<T>;
|
|
10
13
|
}
|
|
11
14
|
|
|
12
|
-
interface ArraySchema<T> extends
|
|
15
|
+
interface ArraySchema<T> extends yup.ArraySchema<T[]> {
|
|
13
16
|
isRequired(required?: boolean, errorMessage?: string): ArraySchema<T>;
|
|
14
17
|
}
|
|
15
18
|
|
|
16
|
-
interface NumberSchema<T extends number | null | undefined = number> extends
|
|
19
|
+
interface NumberSchema<T extends number | null | undefined = number> extends yup.NumberSchema<T> {
|
|
17
20
|
isRequired(required?: boolean, errorMessage?: string): NumberSchema<T>;
|
|
18
21
|
npi(errorMessage?: string): NumberSchema<T>;
|
|
19
22
|
phone(errorMessage?: string): NumberSchema<T>;
|
|
20
23
|
}
|
|
21
24
|
|
|
22
|
-
interface ObjectSchema<
|
|
23
|
-
isRequired(required?: boolean, errorMessage?: string): ObjectSchema<
|
|
25
|
+
interface ObjectSchema<TShape extends Record<string, any>> extends yup.ObjectSchema<TShape> {
|
|
26
|
+
isRequired(required?: boolean, errorMessage?: string): ObjectSchema<TShape>;
|
|
24
27
|
}
|
|
25
28
|
}
|