@availity/yup 4.1.0 → 5.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/.eslintrc.yml +2 -0
- package/CHANGELOG.md +30 -3
- package/README.md +9 -3
- package/dist/index.d.ts +88 -0
- package/dist/index.js +353 -0
- package/dist/index.mjs +325 -0
- package/jest.config.js +11 -0
- package/package.json +34 -17
- package/project.json +23 -0
- package/src/{date.js → date.ts} +18 -16
- package/src/dateRange.ts +215 -0
- package/src/index.ts +67 -0
- package/src/isRequired.ts +26 -0
- package/src/{npi.js → npi.ts} +5 -1
- package/src/{phone.js → phone.ts} +3 -1
- package/tsconfig.json +5 -0
- package/tsconfig.spec.json +10 -0
- package/LICENSE +0 -21
- package/lib/date.js +0 -172
- package/lib/dateRange.js +0 -274
- package/lib/index.js +0 -40
- package/lib/isRequired.js +0 -35
- package/lib/npi.js +0 -62
- package/lib/phone.js +0 -22
- package/src/dateRange.js +0 -184
- package/src/index.js +0 -20
- package/src/isRequired.js +0 -22
- package/types/date.d.ts +0 -21
- package/types/dateRange.d.ts +0 -40
- package/types/index.d.ts +0 -25
package/lib/dateRange.js
DELETED
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports["default"] = void 0;
|
|
9
|
-
|
|
10
|
-
require("core-js/modules/es.array.concat.js");
|
|
11
|
-
|
|
12
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
13
|
-
|
|
14
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
15
|
-
|
|
16
|
-
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
|
|
17
|
-
|
|
18
|
-
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
|
19
|
-
|
|
20
|
-
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
|
|
21
|
-
|
|
22
|
-
var _yup = require("yup");
|
|
23
|
-
|
|
24
|
-
var _moment = _interopRequireDefault(require("moment"));
|
|
25
|
-
|
|
26
|
-
var _get = _interopRequireDefault(require("lodash/get"));
|
|
27
|
-
|
|
28
|
-
var _merge2 = _interopRequireDefault(require("lodash/merge"));
|
|
29
|
-
|
|
30
|
-
var defaultOptions = {
|
|
31
|
-
startKey: 'startDate',
|
|
32
|
-
endKey: 'endDate',
|
|
33
|
-
format: 'MM/DD/YYYY'
|
|
34
|
-
};
|
|
35
|
-
var defaultValue = {};
|
|
36
|
-
var formats = ['YYYY-MM-DD', 'MMDDYYYY', 'YYYYMMDD'];
|
|
37
|
-
|
|
38
|
-
var DateRangeSchema = /*#__PURE__*/function (_MixedSchema) {
|
|
39
|
-
(0, _inherits2["default"])(DateRangeSchema, _MixedSchema);
|
|
40
|
-
|
|
41
|
-
var _super = (0, _createSuper2["default"])(DateRangeSchema);
|
|
42
|
-
|
|
43
|
-
function DateRangeSchema(options) {
|
|
44
|
-
var _this;
|
|
45
|
-
|
|
46
|
-
(0, _classCallCheck2["default"])(this, DateRangeSchema);
|
|
47
|
-
_this = _super.call(this, {
|
|
48
|
-
type: 'dateRange'
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
var _merge = (0, _merge2["default"])({}, defaultOptions, options),
|
|
52
|
-
startKey = _merge.startKey,
|
|
53
|
-
endKey = _merge.endKey,
|
|
54
|
-
format = _merge.format;
|
|
55
|
-
|
|
56
|
-
_this.startKey = startKey;
|
|
57
|
-
_this.endKey = endKey;
|
|
58
|
-
_this.format = format;
|
|
59
|
-
_this.getValidDate = _this.getValidDate.bind((0, _assertThisInitialized2["default"])(_this));
|
|
60
|
-
|
|
61
|
-
_this.withMutation(function (schema) {
|
|
62
|
-
schema.transform(function mutate(value) {
|
|
63
|
-
var start = (0, _get["default"])(value, startKey);
|
|
64
|
-
var end = (0, _get["default"])(value, endKey);
|
|
65
|
-
var startDate;
|
|
66
|
-
var endDate;
|
|
67
|
-
|
|
68
|
-
if (start) {
|
|
69
|
-
startDate = schema.getValidDate(start);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (end) {
|
|
73
|
-
endDate = schema.getValidDate(end);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
startDate: startDate,
|
|
78
|
-
endDate: endDate
|
|
79
|
-
};
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
return _this;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
(0, _createClass2["default"])(DateRangeSchema, [{
|
|
87
|
-
key: "getValidDate",
|
|
88
|
-
value: function getValidDate(value) {
|
|
89
|
-
return (0, _moment["default"])(value, [this.format].concat(formats), true);
|
|
90
|
-
}
|
|
91
|
-
}, {
|
|
92
|
-
key: "distance",
|
|
93
|
-
value: function distance() {
|
|
94
|
-
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
95
|
-
_ref$min = _ref.min;
|
|
96
|
-
|
|
97
|
-
_ref$min = _ref$min === void 0 ? {} : _ref$min;
|
|
98
|
-
var minValue = _ref$min.value,
|
|
99
|
-
_ref$min$units = _ref$min.units,
|
|
100
|
-
minUnits = _ref$min$units === void 0 ? 'day' : _ref$min$units,
|
|
101
|
-
minErrorMessage = _ref$min.errorMessage,
|
|
102
|
-
_ref$max = _ref.max;
|
|
103
|
-
_ref$max = _ref$max === void 0 ? {} : _ref$max;
|
|
104
|
-
var maxValue = _ref$max.value,
|
|
105
|
-
_ref$max$units = _ref$max.units,
|
|
106
|
-
maxUnits = _ref$max$units === void 0 ? 'day' : _ref$max$units,
|
|
107
|
-
maxErrorMessage = _ref$max.errorMessage;
|
|
108
|
-
return this.test({
|
|
109
|
-
name: 'distance',
|
|
110
|
-
exclusive: true,
|
|
111
|
-
test: function test() {
|
|
112
|
-
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
113
|
-
endDate = _ref2.endDate,
|
|
114
|
-
startDate = _ref2.startDate;
|
|
115
|
-
|
|
116
|
-
if (!minValue && !maxValue || !startDate || !endDate) return true;
|
|
117
|
-
|
|
118
|
-
if (maxValue && endDate.isAfter(startDate.add(maxValue, maxUnits), 'day')) {
|
|
119
|
-
return new _yup.ValidationError(maxErrorMessage || "The end date must be within ".concat(maxValue, " ").concat(maxUnits).concat(maxValue > 1 ? 's' : '', " of the start date"), {
|
|
120
|
-
startDate: startDate,
|
|
121
|
-
endDate: endDate
|
|
122
|
-
}, this.path);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (minValue && endDate.isBefore(startDate.add(minValue, minUnits), 'day')) {
|
|
126
|
-
return new _yup.ValidationError(minErrorMessage || "The end date must be greater than ".concat(minValue, " ").concat(minUnits).concat(minValue > 1 ? 's' : '', " of the start date"), {
|
|
127
|
-
startDate: startDate,
|
|
128
|
-
endDate: endDate
|
|
129
|
-
}, this.path);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return true;
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
}, {
|
|
137
|
-
key: "min",
|
|
138
|
-
value: function min(_min, message) {
|
|
139
|
-
var format = this.format;
|
|
140
|
-
var minDate = this.getValidDate(_min);
|
|
141
|
-
return this.test({
|
|
142
|
-
message: message || "Date Range must start on or after ".concat(minDate.format(format)),
|
|
143
|
-
name: 'min',
|
|
144
|
-
exclusive: true,
|
|
145
|
-
params: {
|
|
146
|
-
min: _min
|
|
147
|
-
},
|
|
148
|
-
test: function test() {
|
|
149
|
-
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
150
|
-
startDate = _ref3.startDate;
|
|
151
|
-
|
|
152
|
-
if (!startDate || !_min) {
|
|
153
|
-
return true;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return minDate.isValid() && minDate.isSameOrBefore(startDate);
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
}, {
|
|
161
|
-
key: "max",
|
|
162
|
-
value: function max(_max, message) {
|
|
163
|
-
var format = this.format;
|
|
164
|
-
var maxDate = this.getValidDate(_max);
|
|
165
|
-
return this.test({
|
|
166
|
-
message: message || "Date Range must end on or before ".concat(maxDate.format(format)),
|
|
167
|
-
name: 'max',
|
|
168
|
-
exclusive: true,
|
|
169
|
-
params: {
|
|
170
|
-
max: _max
|
|
171
|
-
},
|
|
172
|
-
test: function test() {
|
|
173
|
-
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
174
|
-
endDate = _ref4.endDate;
|
|
175
|
-
|
|
176
|
-
if (!endDate || !_max) return true;
|
|
177
|
-
return maxDate.isValid() && maxDate.isSameOrAfter(endDate);
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
}, {
|
|
182
|
-
key: "between",
|
|
183
|
-
value: function between(min, max, message) {
|
|
184
|
-
var format = this.format;
|
|
185
|
-
var minDate = this.getValidDate(min);
|
|
186
|
-
var maxDate = this.getValidDate(max);
|
|
187
|
-
return this.test({
|
|
188
|
-
message: message || "Date Range must be between ".concat(minDate.format(format), " and ").concat(maxDate.format(format)),
|
|
189
|
-
name: 'between',
|
|
190
|
-
exclusive: true,
|
|
191
|
-
params: {
|
|
192
|
-
min: min,
|
|
193
|
-
max: max
|
|
194
|
-
},
|
|
195
|
-
test: function test() {
|
|
196
|
-
var _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
197
|
-
startDate = _ref5.startDate,
|
|
198
|
-
endDate = _ref5.endDate;
|
|
199
|
-
|
|
200
|
-
if (!startDate || !endDate || !min || !max) return true;
|
|
201
|
-
return maxDate.isValid() && minDate.isValid() && maxDate.isSameOrAfter(endDate) && minDate.isSameOrBefore(startDate);
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
}, {
|
|
206
|
-
key: "isRequired",
|
|
207
|
-
value: function isRequired() {
|
|
208
|
-
var _isRequired = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
209
|
-
|
|
210
|
-
var msg = arguments.length > 1 ? arguments[1] : undefined;
|
|
211
|
-
return this.test({
|
|
212
|
-
name: 'isRequired',
|
|
213
|
-
exclusive: true,
|
|
214
|
-
message: msg || 'This field is required.',
|
|
215
|
-
test: function test() {
|
|
216
|
-
var _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
217
|
-
startDate = _ref6.startDate,
|
|
218
|
-
endDate = _ref6.endDate;
|
|
219
|
-
|
|
220
|
-
return !_isRequired || startDate && endDate;
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
}, {
|
|
225
|
-
key: "typeError",
|
|
226
|
-
value: function typeError(_ref7) {
|
|
227
|
-
var message = _ref7.message;
|
|
228
|
-
return this.test({
|
|
229
|
-
name: 'typeError',
|
|
230
|
-
exclusive: true,
|
|
231
|
-
test: function test() {
|
|
232
|
-
var _ref8 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
233
|
-
startDate = _ref8.startDate,
|
|
234
|
-
endDate = _ref8.endDate;
|
|
235
|
-
|
|
236
|
-
var errors = [];
|
|
237
|
-
|
|
238
|
-
if ((!startDate || !endDate) && (startDate || endDate)) {
|
|
239
|
-
errors.push(message || 'Start and End Date are required.');
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (startDate && endDate && !startDate.isSameOrBefore(endDate)) {
|
|
243
|
-
errors.push('Start date must come before end date.');
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (startDate && !startDate.isValid()) {
|
|
247
|
-
errors.push('Start Date is invalid.');
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
if (endDate && !endDate.isValid()) {
|
|
251
|
-
errors.push('End Date is invalid.');
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
return errors.length > 0 ? new _yup.ValidationError(errors, {
|
|
255
|
-
startDate: startDate,
|
|
256
|
-
endDate: endDate
|
|
257
|
-
}, this.path) : true;
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
}, {
|
|
262
|
-
key: "_typeCheck",
|
|
263
|
-
value: function _typeCheck() {
|
|
264
|
-
var _ref9 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultValue,
|
|
265
|
-
startDate = _ref9.startDate,
|
|
266
|
-
endDate = _ref9.endDate;
|
|
267
|
-
|
|
268
|
-
return startDate && endDate && startDate.isValid() && endDate.isValid();
|
|
269
|
-
}
|
|
270
|
-
}]);
|
|
271
|
-
return DateRangeSchema;
|
|
272
|
-
}(_yup.MixedSchema);
|
|
273
|
-
|
|
274
|
-
exports["default"] = DateRangeSchema;
|
package/lib/index.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.dateRange = exports.avDate = void 0;
|
|
9
|
-
|
|
10
|
-
var _yup = require("yup");
|
|
11
|
-
|
|
12
|
-
var _date = _interopRequireDefault(require("./date"));
|
|
13
|
-
|
|
14
|
-
var _dateRange = _interopRequireDefault(require("./dateRange"));
|
|
15
|
-
|
|
16
|
-
var _isRequired = _interopRequireDefault(require("./isRequired"));
|
|
17
|
-
|
|
18
|
-
var _npi = _interopRequireDefault(require("./npi"));
|
|
19
|
-
|
|
20
|
-
var _phone = _interopRequireDefault(require("./phone"));
|
|
21
|
-
|
|
22
|
-
var avDate = function avDate(opts) {
|
|
23
|
-
return new _date["default"](opts);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
exports.avDate = avDate;
|
|
27
|
-
|
|
28
|
-
var dateRange = function dateRange(opts) {
|
|
29
|
-
return new _dateRange["default"](opts);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
exports.dateRange = dateRange;
|
|
33
|
-
(0, _yup.addMethod)(_yup.array, 'isRequired', _isRequired["default"]);
|
|
34
|
-
(0, _yup.addMethod)(_yup.number, 'isRequired', _isRequired["default"]);
|
|
35
|
-
(0, _yup.addMethod)(_yup.object, 'isRequired', _isRequired["default"]);
|
|
36
|
-
(0, _yup.addMethod)(_yup.string, 'isRequired', _isRequired["default"]);
|
|
37
|
-
(0, _yup.addMethod)(_yup.number, 'npi', _npi["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/lib/isRequired.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
|
|
8
|
-
function isRequired() {
|
|
9
|
-
var isRequired = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
10
|
-
var msg = arguments.length > 1 ? arguments[1] : undefined;
|
|
11
|
-
return this.test({
|
|
12
|
-
name: 'isRequired',
|
|
13
|
-
exclusive: true,
|
|
14
|
-
message: msg || 'This field is required.',
|
|
15
|
-
test: function test(value) {
|
|
16
|
-
if (isRequired) {
|
|
17
|
-
if (typeof value === 'number') {
|
|
18
|
-
return value !== undefined;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (Array.isArray(value)) {
|
|
22
|
-
return value.length > 0;
|
|
23
|
-
} // String ( If you want to check for null add nullable )
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return value !== undefined && value !== '';
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return true;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
var _default = isRequired;
|
|
35
|
-
exports["default"] = _default;
|
package/lib/npi.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
require("core-js/modules/es.number.parse-int");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports["default"] = void 0;
|
|
9
|
-
|
|
10
|
-
require("core-js/modules/es.number.constructor.js");
|
|
11
|
-
|
|
12
|
-
var INTEGER_REGEX = /^\d*$/;
|
|
13
|
-
|
|
14
|
-
function npi(msg) {
|
|
15
|
-
return this.test({
|
|
16
|
-
name: 'npi',
|
|
17
|
-
exclusive: true,
|
|
18
|
-
message: msg || 'This field is invalid.',
|
|
19
|
-
test: function test(value) {
|
|
20
|
-
if (!value) return true;
|
|
21
|
-
value += '';
|
|
22
|
-
|
|
23
|
-
if (!INTEGER_REGEX.test(value) || value.length !== 10) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
var firstDigit = value.charAt(0);
|
|
28
|
-
|
|
29
|
-
if (['1', '2', '3', '4'].indexOf(firstDigit) < 0) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
var digit = Number.parseInt(value.charAt(9), 10);
|
|
34
|
-
value = value.substring(0, 9);
|
|
35
|
-
value = "80840".concat(value);
|
|
36
|
-
var alternate = true;
|
|
37
|
-
var total = 0;
|
|
38
|
-
|
|
39
|
-
for (var i = value.length; i > 0; i--) {
|
|
40
|
-
var next = Number.parseInt(value.charAt(i - 1), 10);
|
|
41
|
-
|
|
42
|
-
if (alternate) {
|
|
43
|
-
next *= 2;
|
|
44
|
-
|
|
45
|
-
if (next > 9) {
|
|
46
|
-
next = next % 10 + 1;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
total += next;
|
|
51
|
-
alternate = !alternate;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
var roundUp = Math.ceil(total / 10) * 10;
|
|
55
|
-
var calculatedCheck = roundUp - total;
|
|
56
|
-
return calculatedCheck === digit;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
var _default = npi;
|
|
62
|
-
exports["default"] = _default;
|
package/lib/phone.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
var NANP_REGEXP = /^(\+?1[\s.-]?)?\(?[2-9]\d{2}[\s).-]?\s?[2-9]\d{2}[\s.-]?\d{4}$/;
|
|
8
|
-
|
|
9
|
-
function phone(msg) {
|
|
10
|
-
return this.test({
|
|
11
|
-
name: 'phone',
|
|
12
|
-
exclusive: true,
|
|
13
|
-
message: msg || 'This field is invalid',
|
|
14
|
-
test: function test(value) {
|
|
15
|
-
if (!value) return true;
|
|
16
|
-
return NANP_REGEXP.test(value);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
var _default = phone;
|
|
22
|
-
exports["default"] = _default;
|
package/src/dateRange.js
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import { MixedSchema, ValidationError } from 'yup';
|
|
2
|
-
import moment from 'moment';
|
|
3
|
-
import get from 'lodash/get';
|
|
4
|
-
import merge from 'lodash/merge';
|
|
5
|
-
|
|
6
|
-
const defaultOptions = {
|
|
7
|
-
startKey: 'startDate',
|
|
8
|
-
endKey: 'endDate',
|
|
9
|
-
format: 'MM/DD/YYYY',
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const defaultValue = {};
|
|
13
|
-
|
|
14
|
-
const formats = ['YYYY-MM-DD', 'MMDDYYYY', 'YYYYMMDD'];
|
|
15
|
-
|
|
16
|
-
export default class DateRangeSchema extends MixedSchema {
|
|
17
|
-
constructor(options) {
|
|
18
|
-
super({
|
|
19
|
-
type: 'dateRange',
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
const { startKey, endKey, format } = merge({}, defaultOptions, options);
|
|
23
|
-
|
|
24
|
-
this.startKey = startKey;
|
|
25
|
-
this.endKey = endKey;
|
|
26
|
-
this.format = format;
|
|
27
|
-
this.getValidDate = this.getValidDate.bind(this);
|
|
28
|
-
|
|
29
|
-
this.withMutation((schema) => {
|
|
30
|
-
schema.transform(function mutate(value) {
|
|
31
|
-
const start = get(value, startKey);
|
|
32
|
-
const end = get(value, endKey);
|
|
33
|
-
|
|
34
|
-
let startDate;
|
|
35
|
-
let endDate;
|
|
36
|
-
if (start) {
|
|
37
|
-
startDate = schema.getValidDate(start);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (end) {
|
|
41
|
-
endDate = schema.getValidDate(end);
|
|
42
|
-
}
|
|
43
|
-
return { startDate, endDate };
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
getValidDate(value) {
|
|
49
|
-
return moment(value, [this.format, ...formats], true);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
distance({
|
|
53
|
-
min: { value: minValue, units: minUnits = 'day', errorMessage: minErrorMessage } = {},
|
|
54
|
-
max: { value: maxValue, units: maxUnits = 'day', errorMessage: maxErrorMessage } = {},
|
|
55
|
-
} = defaultValue) {
|
|
56
|
-
return this.test({
|
|
57
|
-
name: 'distance',
|
|
58
|
-
exclusive: true,
|
|
59
|
-
test({ endDate, startDate } = defaultValue) {
|
|
60
|
-
if ((!minValue && !maxValue) || !startDate || !endDate) return true;
|
|
61
|
-
|
|
62
|
-
if (maxValue && endDate.isAfter(startDate.add(maxValue, maxUnits), 'day')) {
|
|
63
|
-
return new ValidationError(
|
|
64
|
-
maxErrorMessage ||
|
|
65
|
-
`The end date must be within ${maxValue} ${maxUnits}${maxValue > 1 ? 's' : ''} of the start date`,
|
|
66
|
-
{
|
|
67
|
-
startDate,
|
|
68
|
-
endDate,
|
|
69
|
-
},
|
|
70
|
-
this.path
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
if (minValue && endDate.isBefore(startDate.add(minValue, minUnits), 'day')) {
|
|
74
|
-
return new ValidationError(
|
|
75
|
-
minErrorMessage ||
|
|
76
|
-
`The end date must be greater than ${minValue} ${minUnits}${minValue > 1 ? 's' : ''} of the start date`,
|
|
77
|
-
{ startDate, endDate },
|
|
78
|
-
this.path
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return true;
|
|
83
|
-
},
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
min(min, message) {
|
|
88
|
-
const { format } = this;
|
|
89
|
-
|
|
90
|
-
const minDate = this.getValidDate(min);
|
|
91
|
-
return this.test({
|
|
92
|
-
message: message || `Date Range must start on or after ${minDate.format(format)}`,
|
|
93
|
-
name: 'min',
|
|
94
|
-
exclusive: true,
|
|
95
|
-
params: { min },
|
|
96
|
-
test({ startDate } = defaultValue) {
|
|
97
|
-
if (!startDate || !min) {
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
return minDate.isValid() && minDate.isSameOrBefore(startDate);
|
|
101
|
-
},
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
max(max, message) {
|
|
106
|
-
const { format } = this;
|
|
107
|
-
|
|
108
|
-
const maxDate = this.getValidDate(max);
|
|
109
|
-
|
|
110
|
-
return this.test({
|
|
111
|
-
message: message || `Date Range must end on or before ${maxDate.format(format)}`,
|
|
112
|
-
name: 'max',
|
|
113
|
-
exclusive: true,
|
|
114
|
-
params: { max },
|
|
115
|
-
test({ endDate } = defaultValue) {
|
|
116
|
-
if (!endDate || !max) return true;
|
|
117
|
-
return maxDate.isValid() && maxDate.isSameOrAfter(endDate);
|
|
118
|
-
},
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
between(min, max, message) {
|
|
123
|
-
const { format } = this;
|
|
124
|
-
|
|
125
|
-
const minDate = this.getValidDate(min);
|
|
126
|
-
const maxDate = this.getValidDate(max);
|
|
127
|
-
|
|
128
|
-
return this.test({
|
|
129
|
-
message: message || `Date Range must be between ${minDate.format(format)} and ${maxDate.format(format)}`,
|
|
130
|
-
name: 'between',
|
|
131
|
-
exclusive: true,
|
|
132
|
-
params: { min, max },
|
|
133
|
-
test({ startDate, endDate } = defaultValue) {
|
|
134
|
-
if (!startDate || !endDate || !min || !max) return true;
|
|
135
|
-
return (
|
|
136
|
-
maxDate.isValid() && minDate.isValid() && maxDate.isSameOrAfter(endDate) && minDate.isSameOrBefore(startDate)
|
|
137
|
-
);
|
|
138
|
-
},
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
isRequired(isRequired = true, msg) {
|
|
143
|
-
return this.test({
|
|
144
|
-
name: 'isRequired',
|
|
145
|
-
exclusive: true,
|
|
146
|
-
message: msg || 'This field is required.',
|
|
147
|
-
test({ startDate, endDate } = defaultValue) {
|
|
148
|
-
return !isRequired || (startDate && endDate);
|
|
149
|
-
},
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
typeError({ message }) {
|
|
154
|
-
return this.test({
|
|
155
|
-
name: 'typeError',
|
|
156
|
-
exclusive: true,
|
|
157
|
-
test({ startDate, endDate } = defaultValue) {
|
|
158
|
-
const errors = [];
|
|
159
|
-
|
|
160
|
-
if ((!startDate || !endDate) && (startDate || endDate)) {
|
|
161
|
-
errors.push(message || 'Start and End Date are required.');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (startDate && endDate && !startDate.isSameOrBefore(endDate)) {
|
|
165
|
-
errors.push('Start date must come before end date.');
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (startDate && !startDate.isValid()) {
|
|
169
|
-
errors.push('Start Date is invalid.');
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (endDate && !endDate.isValid()) {
|
|
173
|
-
errors.push('End Date is invalid.');
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return errors.length > 0 ? new ValidationError(errors, { startDate, endDate }, this.path) : true;
|
|
177
|
-
},
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
_typeCheck({ startDate, endDate } = defaultValue) {
|
|
182
|
-
return startDate && endDate && startDate.isValid() && endDate.isValid();
|
|
183
|
-
}
|
|
184
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { addMethod, array, number, object, string } from 'yup';
|
|
2
|
-
import AvDateSchema from './date';
|
|
3
|
-
import DateRangeSchema from './dateRange';
|
|
4
|
-
import isRequired from './isRequired';
|
|
5
|
-
import npi from './npi';
|
|
6
|
-
import phone from './phone';
|
|
7
|
-
|
|
8
|
-
export const avDate = (opts) => new AvDateSchema(opts);
|
|
9
|
-
export const dateRange = (opts) => new DateRangeSchema(opts);
|
|
10
|
-
|
|
11
|
-
addMethod(array, 'isRequired', isRequired);
|
|
12
|
-
addMethod(number, 'isRequired', isRequired);
|
|
13
|
-
addMethod(object, 'isRequired', isRequired);
|
|
14
|
-
addMethod(string, 'isRequired', isRequired);
|
|
15
|
-
|
|
16
|
-
addMethod(number, 'npi', npi);
|
|
17
|
-
addMethod(string, 'npi', npi);
|
|
18
|
-
|
|
19
|
-
addMethod(number, 'phone', phone);
|
|
20
|
-
addMethod(string, 'phone', phone);
|
package/src/isRequired.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
function isRequired(isRequired = true, msg) {
|
|
2
|
-
return this.test({
|
|
3
|
-
name: 'isRequired',
|
|
4
|
-
exclusive: true,
|
|
5
|
-
message: msg || 'This field is required.',
|
|
6
|
-
test(value) {
|
|
7
|
-
if (isRequired) {
|
|
8
|
-
if (typeof value === 'number') {
|
|
9
|
-
return value !== undefined;
|
|
10
|
-
}
|
|
11
|
-
if (Array.isArray(value)) {
|
|
12
|
-
return value.length > 0;
|
|
13
|
-
}
|
|
14
|
-
// String ( If you want to check for null add nullable )
|
|
15
|
-
return value !== undefined && value !== '';
|
|
16
|
-
}
|
|
17
|
-
return true;
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export default isRequired;
|
package/types/date.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import * as yup from 'yup';
|
|
2
|
-
|
|
3
|
-
declare module 'yup' {
|
|
4
|
-
interface DateOpts {
|
|
5
|
-
format?: string | 'MM/DD/YYYY';
|
|
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
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface AvDateSchemaConstructor {
|
|
16
|
-
(opts: DateOpts): AvDateSchema;
|
|
17
|
-
new (opts: DateOpts): AvDateSchema;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const avDate: AvDateSchemaConstructor;
|
|
21
|
-
}
|