@availity/yup 3.0.10 → 3.2.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 +42 -0
- package/README.md +2 -2
- package/lib/date.js +22 -30
- package/lib/dateRange.js +6 -6
- package/package.json +12 -18
- package/src/date.js +14 -44
- package/src/dateRange.js +26 -50
- package/src/index.js +2 -2
- package/src/phone.js +1 -2
- package/{typings → types}/date.d.ts +2 -7
- package/{typings → types}/dateRange.d.ts +3 -9
- package/{typings → types}/index.d.ts +2 -4
- package/tests/date.test.js +0 -149
- package/tests/dateRange.test.js +0 -97
- package/tests/isRequired.test.js +0 -52
- package/tests/npi.test.js +0 -77
- package/tests/phone.test.js +0 -62
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,48 @@
|
|
|
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.2.0 (2021-10-22)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **yup:** switch to lodash from merge-options-es5 ([06456c9](https://github.com/Availity/sdk-js/commit/06456c90ac6ef7523c4ed0d2e4dc9af8c4a6dee5))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## [3.1.1](https://github.com/Availity/sdk-js/compare/@availity/yup@3.1.0...@availity/yup@3.1.1) (2021-06-16)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* **yup:** refactor typeError, use _typeCheck ([24cea5e](https://github.com/Availity/sdk-js/commit/24cea5e2140d72ecec169dfde9fd94eb18956e66))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# [3.1.0](https://github.com/Availity/sdk-js/compare/@availity/yup@3.0.11...@availity/yup@3.1.0) (2021-06-11)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Features
|
|
32
|
+
|
|
33
|
+
* **yup:** support inclusivity option ([94b1283](https://github.com/Availity/sdk-js/commit/94b1283c29e00f0db49ea58ae62d1cf16beef678))
|
|
34
|
+
* **yup:** unit test for inclusivity ([25bf64b](https://github.com/Availity/sdk-js/commit/25bf64bd1fbd9b03ff609d8b8614ce40b7a4f4be))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
## 3.0.11 (2021-05-25)
|
|
41
|
+
|
|
42
|
+
**Note:** Version bump only for package @availity/yup
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
6
48
|
## [3.0.10](https://github.com/Availity/sdk-js/compare/@availity/yup@3.0.9...@availity/yup@3.0.10) (2021-05-17)
|
|
7
49
|
|
|
8
50
|
**Note:** Version bump only for package @availity/yup
|
package/README.md
CHANGED
|
@@ -9,13 +9,13 @@
|
|
|
9
9
|
### NPM
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
|
|
12
|
+
npm install @availity/yup yup
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
### Yarn
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
|
|
18
|
+
yarn add @availity/yup yup
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
## [Documentation](https://availity.github.io/sdk-js/resources/yup)
|
package/lib/date.js
CHANGED
|
@@ -7,6 +7,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports["default"] = void 0;
|
|
9
9
|
|
|
10
|
+
require("core-js/modules/es.function.name.js");
|
|
11
|
+
|
|
10
12
|
require("core-js/modules/es.array.concat.js");
|
|
11
13
|
|
|
12
14
|
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
@@ -19,6 +21,10 @@ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits
|
|
|
19
21
|
|
|
20
22
|
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
|
|
21
23
|
|
|
24
|
+
var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
|
|
25
|
+
|
|
26
|
+
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
|
27
|
+
|
|
22
28
|
var _yup = require("yup");
|
|
23
29
|
|
|
24
30
|
var _moment = _interopRequireDefault(require("moment"));
|
|
@@ -34,7 +40,7 @@ var AvDateSchema = /*#__PURE__*/function (_mixed) {
|
|
|
34
40
|
var _super = (0, _createSuper2["default"])(AvDateSchema);
|
|
35
41
|
|
|
36
42
|
function AvDateSchema() {
|
|
37
|
-
var _this;
|
|
43
|
+
var _thisSuper, _this;
|
|
38
44
|
|
|
39
45
|
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultOpts,
|
|
40
46
|
_ref$format = _ref.format,
|
|
@@ -48,6 +54,14 @@ var AvDateSchema = /*#__PURE__*/function (_mixed) {
|
|
|
48
54
|
_this.getValidDate = _this.getValidDate.bind((0, _assertThisInitialized2["default"])(_this));
|
|
49
55
|
|
|
50
56
|
_this.withMutation(function () {
|
|
57
|
+
if (!_this.tests.some(function (test) {
|
|
58
|
+
var _test$OPTIONS;
|
|
59
|
+
|
|
60
|
+
return (test === null || test === void 0 ? void 0 : (_test$OPTIONS = test.OPTIONS) === null || _test$OPTIONS === void 0 ? void 0 : _test$OPTIONS.name) === 'typeError';
|
|
61
|
+
})) {
|
|
62
|
+
(0, _get2["default"])((_thisSuper = (0, _assertThisInitialized2["default"])(_this), (0, _getPrototypeOf2["default"])(AvDateSchema.prototype)), "typeError", _thisSuper).call(_thisSuper, 'Date is invalid.');
|
|
63
|
+
}
|
|
64
|
+
|
|
51
65
|
_this.transform(function mutate(value) {
|
|
52
66
|
return this.getValidDate(value);
|
|
53
67
|
});
|
|
@@ -57,36 +71,13 @@ var AvDateSchema = /*#__PURE__*/function (_mixed) {
|
|
|
57
71
|
}
|
|
58
72
|
|
|
59
73
|
(0, _createClass2["default"])(AvDateSchema, [{
|
|
60
|
-
key: "typeError",
|
|
61
|
-
value: function typeError() {
|
|
62
|
-
return this.test({
|
|
63
|
-
message: 'Date is invalid.',
|
|
64
|
-
name: 'typeError',
|
|
65
|
-
test: function test(value) {
|
|
66
|
-
if (value !== undefined) {
|
|
67
|
-
if (!this.schema.isType(value)) {
|
|
68
|
-
// Values that do not pass the previous .isType() check are expected to be a moment object
|
|
69
|
-
// because this.getValidDate(value) will have run. So as long as the passed in value
|
|
70
|
-
// is defined, moment._i will contain a string value to validate.
|
|
71
|
-
// If user enters a date and then removes it, should not show a typeError
|
|
72
|
-
// Note: this does not prevent other tests, like isRequired, from showing messages
|
|
73
|
-
// If user has touched a required field, error message should still show
|
|
74
|
-
return value._i === '';
|
|
75
|
-
} // When this.schema.isType(value) returns true
|
|
76
|
-
// we are avDate type with appropriate format
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
}, {
|
|
87
74
|
key: "_typeCheck",
|
|
88
75
|
value: function _typeCheck(value) {
|
|
89
|
-
|
|
76
|
+
// So as long as the passed in value is defined, moment._i will contain a string value to validate.
|
|
77
|
+
// If user enters a date and then removes it, should not show a typeError
|
|
78
|
+
// Note: this does not prevent other tests, like isRequired, from showing messages
|
|
79
|
+
// If user has touched a required field, required error message should still show
|
|
80
|
+
return value.isValid() || value._i === '';
|
|
90
81
|
}
|
|
91
82
|
}, {
|
|
92
83
|
key: "getValidDate",
|
|
@@ -155,6 +146,7 @@ var AvDateSchema = /*#__PURE__*/function (_mixed) {
|
|
|
155
146
|
}, {
|
|
156
147
|
key: "between",
|
|
157
148
|
value: function between(min, max, msg) {
|
|
149
|
+
var inclusivity = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '()';
|
|
158
150
|
var minDate = this.getValidDate(min);
|
|
159
151
|
var maxDate = this.getValidDate(max); // Can't use arrow function because we rely on 'this' referencing yup's internals
|
|
160
152
|
|
|
@@ -169,7 +161,7 @@ var AvDateSchema = /*#__PURE__*/function (_mixed) {
|
|
|
169
161
|
return true;
|
|
170
162
|
}
|
|
171
163
|
|
|
172
|
-
return value.isBetween(minDate, maxDate);
|
|
164
|
+
return value.isBetween(minDate, maxDate, undefined, inclusivity);
|
|
173
165
|
}
|
|
174
166
|
});
|
|
175
167
|
}
|
package/lib/dateRange.js
CHANGED
|
@@ -23,11 +23,11 @@ var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/creat
|
|
|
23
23
|
|
|
24
24
|
var _moment = _interopRequireDefault(require("moment"));
|
|
25
25
|
|
|
26
|
-
var _lodash = _interopRequireDefault(require("lodash.get"));
|
|
27
|
-
|
|
28
26
|
var _yup = require("yup");
|
|
29
27
|
|
|
30
|
-
var
|
|
28
|
+
var _get = _interopRequireDefault(require("lodash/get"));
|
|
29
|
+
|
|
30
|
+
var _merge2 = _interopRequireDefault(require("lodash/merge"));
|
|
31
31
|
|
|
32
32
|
var defaultOptions = {
|
|
33
33
|
startKey: 'startDate',
|
|
@@ -50,7 +50,7 @@ var DateRangeSchema = /*#__PURE__*/function (_mixed) {
|
|
|
50
50
|
type: 'dateRange'
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
-
var _merge = (0,
|
|
53
|
+
var _merge = (0, _merge2["default"])({}, defaultOptions, options),
|
|
54
54
|
startKey = _merge.startKey,
|
|
55
55
|
endKey = _merge.endKey,
|
|
56
56
|
format = _merge.format;
|
|
@@ -62,8 +62,8 @@ var DateRangeSchema = /*#__PURE__*/function (_mixed) {
|
|
|
62
62
|
|
|
63
63
|
_this.withMutation(function () {
|
|
64
64
|
_this.transform(function mutate(value) {
|
|
65
|
-
var start = (0,
|
|
66
|
-
var end = (0,
|
|
65
|
+
var start = (0, _get["default"])(value, startKey);
|
|
66
|
+
var end = (0, _get["default"])(value, endKey);
|
|
67
67
|
var startDate;
|
|
68
68
|
var endDate;
|
|
69
69
|
|
package/package.json
CHANGED
|
@@ -1,37 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@availity/yup",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Additional Methods for Yup validation library",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "src/index.js",
|
|
7
|
-
"
|
|
8
|
-
"scripts": {
|
|
9
|
-
"test": "test"
|
|
10
|
-
},
|
|
7
|
+
"types": "types/index.d.ts",
|
|
11
8
|
"keywords": [
|
|
12
|
-
"
|
|
13
|
-
"resumable",
|
|
14
|
-
"upload",
|
|
9
|
+
"yup",
|
|
15
10
|
"availity"
|
|
16
11
|
],
|
|
17
12
|
"author": "Kyle Gray <kyle.gray@availity.com>",
|
|
18
13
|
"license": "MIT",
|
|
19
|
-
"peerDependencies": {
|
|
20
|
-
"moment": "^2.24.0",
|
|
21
|
-
"yup": "^0.28.1"
|
|
22
|
-
},
|
|
23
14
|
"publishConfig": {
|
|
24
15
|
"access": "public"
|
|
25
16
|
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@babel/runtime": "^7.10.2",
|
|
19
|
+
"core-js": "^3.12.1",
|
|
20
|
+
"lodash": "^4.17.21"
|
|
21
|
+
},
|
|
26
22
|
"devDependencies": {
|
|
27
23
|
"moment": "^2.24.0",
|
|
28
24
|
"yup": "^0.28.1"
|
|
29
25
|
},
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"lodash.get": "^4.4.2",
|
|
34
|
-
"merge-options-es5": "^1.0.0"
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"moment": "^2.24.0",
|
|
28
|
+
"yup": "^0.28.1"
|
|
35
29
|
},
|
|
36
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "a247c20c27816eb3e3b68b983d12631a6cadd72b"
|
|
37
31
|
}
|
package/src/date.js
CHANGED
|
@@ -17,39 +17,21 @@ export default class AvDateSchema extends mixed {
|
|
|
17
17
|
this.getValidDate = this.getValidDate.bind(this);
|
|
18
18
|
|
|
19
19
|
this.withMutation(() => {
|
|
20
|
+
if (!this.tests.some((test) => test?.OPTIONS?.name === 'typeError')) {
|
|
21
|
+
super.typeError('Date is invalid.');
|
|
22
|
+
}
|
|
20
23
|
this.transform(function mutate(value) {
|
|
21
24
|
return this.getValidDate(value);
|
|
22
25
|
});
|
|
23
26
|
});
|
|
24
27
|
}
|
|
25
28
|
|
|
26
|
-
typeError() {
|
|
27
|
-
return this.test({
|
|
28
|
-
message: 'Date is invalid.',
|
|
29
|
-
name: 'typeError',
|
|
30
|
-
test(value) {
|
|
31
|
-
if (value !== undefined) {
|
|
32
|
-
if (!this.schema.isType(value)) {
|
|
33
|
-
// Values that do not pass the previous .isType() check are expected to be a moment object
|
|
34
|
-
// because this.getValidDate(value) will have run. So as long as the passed in value
|
|
35
|
-
// is defined, moment._i will contain a string value to validate.
|
|
36
|
-
// If user enters a date and then removes it, should not show a typeError
|
|
37
|
-
// Note: this does not prevent other tests, like isRequired, from showing messages
|
|
38
|
-
// If user has touched a required field, error message should still show
|
|
39
|
-
return value._i === '';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// When this.schema.isType(value) returns true
|
|
43
|
-
// we are avDate type with appropriate format
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
return true;
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
29
|
_typeCheck(value) {
|
|
52
|
-
|
|
30
|
+
// So as long as the passed in value is defined, moment._i will contain a string value to validate.
|
|
31
|
+
// If user enters a date and then removes it, should not show a typeError
|
|
32
|
+
// Note: this does not prevent other tests, like isRequired, from showing messages
|
|
33
|
+
// If user has touched a required field, required error message should still show
|
|
34
|
+
return value.isValid() || value._i === '';
|
|
53
35
|
}
|
|
54
36
|
|
|
55
37
|
getValidDate(value) {
|
|
@@ -60,8 +42,7 @@ export default class AvDateSchema extends mixed {
|
|
|
60
42
|
const minDate = this.getValidDate(min);
|
|
61
43
|
|
|
62
44
|
return this.test({
|
|
63
|
-
message:
|
|
64
|
-
message || `Date must be ${minDate.format(this.format)} or later.`,
|
|
45
|
+
message: message || `Date must be ${minDate.format(this.format)} or later.`,
|
|
65
46
|
name: 'min',
|
|
66
47
|
exclusive: true,
|
|
67
48
|
params: { min },
|
|
@@ -78,8 +59,7 @@ export default class AvDateSchema extends mixed {
|
|
|
78
59
|
const maxDate = this.getValidDate(max);
|
|
79
60
|
|
|
80
61
|
return this.test({
|
|
81
|
-
message:
|
|
82
|
-
message || `Date must be ${maxDate.format(this.format)} or earlier.`,
|
|
62
|
+
message: message || `Date must be ${maxDate.format(this.format)} or earlier.`,
|
|
83
63
|
name: 'max',
|
|
84
64
|
exclusive: true,
|
|
85
65
|
params: { max },
|
|
@@ -107,7 +87,7 @@ export default class AvDateSchema extends mixed {
|
|
|
107
87
|
});
|
|
108
88
|
}
|
|
109
89
|
|
|
110
|
-
between(min, max, msg) {
|
|
90
|
+
between(min, max, msg, inclusivity = '()') {
|
|
111
91
|
const minDate = this.getValidDate(min);
|
|
112
92
|
|
|
113
93
|
const maxDate = this.getValidDate(max);
|
|
@@ -117,23 +97,13 @@ export default class AvDateSchema extends mixed {
|
|
|
117
97
|
name: 'between',
|
|
118
98
|
exclusive: true, // Validation errors don't stack
|
|
119
99
|
// NOTE: Intentional use of single quotes - yup will handle the string interpolation
|
|
120
|
-
message:
|
|
121
|
-
msg ||
|
|
122
|
-
`Date must be between ${minDate.format(
|
|
123
|
-
this.format
|
|
124
|
-
)} and ${maxDate.format(this.format)}.`,
|
|
100
|
+
message: msg || `Date must be between ${minDate.format(this.format)} and ${maxDate.format(this.format)}.`,
|
|
125
101
|
test(value) {
|
|
126
|
-
if (
|
|
127
|
-
!value ||
|
|
128
|
-
!min ||
|
|
129
|
-
!max ||
|
|
130
|
-
!minDate.isValid() ||
|
|
131
|
-
!maxDate.isValid()
|
|
132
|
-
) {
|
|
102
|
+
if (!value || !min || !max || !minDate.isValid() || !maxDate.isValid()) {
|
|
133
103
|
return true;
|
|
134
104
|
}
|
|
135
105
|
|
|
136
|
-
return value.isBetween(minDate, maxDate);
|
|
106
|
+
return value.isBetween(minDate, maxDate, undefined, inclusivity);
|
|
137
107
|
},
|
|
138
108
|
});
|
|
139
109
|
}
|
package/src/dateRange.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import moment from 'moment';
|
|
2
|
-
import get from 'lodash.get';
|
|
3
2
|
import { mixed, ValidationError } from 'yup';
|
|
4
|
-
import
|
|
3
|
+
import get from 'lodash/get';
|
|
4
|
+
import merge from 'lodash/merge';
|
|
5
5
|
|
|
6
6
|
const defaultOptions = {
|
|
7
7
|
startKey: 'startDate',
|
|
@@ -64,16 +64,8 @@ export default class DateRangeSchema extends mixed {
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
distance({
|
|
67
|
-
min: {
|
|
68
|
-
|
|
69
|
-
units: minUnits = 'day',
|
|
70
|
-
errorMessage: minErrorMessage,
|
|
71
|
-
} = {},
|
|
72
|
-
max: {
|
|
73
|
-
value: maxValue,
|
|
74
|
-
units: maxUnits = 'day',
|
|
75
|
-
errorMessage: maxErrorMessage,
|
|
76
|
-
} = {},
|
|
67
|
+
min: { value: minValue, units: minUnits = 'day', errorMessage: minErrorMessage } = {},
|
|
68
|
+
max: { value: maxValue, units: maxUnits = 'day', errorMessage: maxErrorMessage } = {},
|
|
77
69
|
} = defaultValue) {
|
|
78
70
|
return this.test({
|
|
79
71
|
name: 'distance',
|
|
@@ -82,28 +74,24 @@ export default class DateRangeSchema extends mixed {
|
|
|
82
74
|
if ((!minValue && !maxValue) || !startDate || !endDate) return true;
|
|
83
75
|
|
|
84
76
|
if (maxValue && endDate.isAfter(startDate.add(maxValue, maxUnits), 'day')) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
);
|
|
96
|
-
}
|
|
77
|
+
return new ValidationError(
|
|
78
|
+
maxErrorMessage ||
|
|
79
|
+
`The end date must be within ${maxValue} ${maxUnits}${maxValue > 1 ? 's' : ''} of the start date`,
|
|
80
|
+
{
|
|
81
|
+
startDate,
|
|
82
|
+
endDate,
|
|
83
|
+
},
|
|
84
|
+
this.path
|
|
85
|
+
);
|
|
86
|
+
}
|
|
97
87
|
if (minValue && endDate.isBefore(startDate.add(minValue, minUnits), 'day')) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
);
|
|
106
|
-
}
|
|
88
|
+
return new ValidationError(
|
|
89
|
+
minErrorMessage ||
|
|
90
|
+
`The end date must be greater than ${minValue} ${minUnits}${minValue > 1 ? 's' : ''} of the start date`,
|
|
91
|
+
{ startDate, endDate },
|
|
92
|
+
this.path
|
|
93
|
+
);
|
|
94
|
+
}
|
|
107
95
|
|
|
108
96
|
return true;
|
|
109
97
|
},
|
|
@@ -115,9 +103,7 @@ export default class DateRangeSchema extends mixed {
|
|
|
115
103
|
|
|
116
104
|
const minDate = this.getValidDate(min);
|
|
117
105
|
return this.test({
|
|
118
|
-
message:
|
|
119
|
-
message ||
|
|
120
|
-
`Date Range must start on or after ${minDate.format(format)}`,
|
|
106
|
+
message: message || `Date Range must start on or after ${minDate.format(format)}`,
|
|
121
107
|
name: 'min',
|
|
122
108
|
exclusive: true,
|
|
123
109
|
params: { min },
|
|
@@ -136,8 +122,7 @@ export default class DateRangeSchema extends mixed {
|
|
|
136
122
|
const maxDate = this.getValidDate(max);
|
|
137
123
|
|
|
138
124
|
return this.test({
|
|
139
|
-
message:
|
|
140
|
-
message || `Date Range must end on or before ${maxDate.format(format)}`,
|
|
125
|
+
message: message || `Date Range must end on or before ${maxDate.format(format)}`,
|
|
141
126
|
name: 'max',
|
|
142
127
|
exclusive: true,
|
|
143
128
|
params: { max },
|
|
@@ -155,21 +140,14 @@ export default class DateRangeSchema extends mixed {
|
|
|
155
140
|
const maxDate = this.getValidDate(max);
|
|
156
141
|
|
|
157
142
|
return this.test({
|
|
158
|
-
message:
|
|
159
|
-
message ||
|
|
160
|
-
`Date Range must be between ${minDate.format(
|
|
161
|
-
format
|
|
162
|
-
)} and ${maxDate.format(format)}`,
|
|
143
|
+
message: message || `Date Range must be between ${minDate.format(format)} and ${maxDate.format(format)}`,
|
|
163
144
|
name: 'between',
|
|
164
145
|
exclusive: true,
|
|
165
146
|
params: { min, max },
|
|
166
147
|
test({ startDate, endDate } = defaultValue) {
|
|
167
148
|
if (!startDate || !endDate || !min || !max) return true;
|
|
168
149
|
return (
|
|
169
|
-
maxDate.isValid() &&
|
|
170
|
-
minDate.isValid() &&
|
|
171
|
-
maxDate.isSameOrAfter(endDate) &&
|
|
172
|
-
minDate.isSameOrBefore(startDate)
|
|
150
|
+
maxDate.isValid() && minDate.isValid() && maxDate.isSameOrAfter(endDate) && minDate.isSameOrBefore(startDate)
|
|
173
151
|
);
|
|
174
152
|
},
|
|
175
153
|
});
|
|
@@ -205,9 +183,7 @@ export default class DateRangeSchema extends mixed {
|
|
|
205
183
|
errors.push('End Date is invalid.');
|
|
206
184
|
}
|
|
207
185
|
|
|
208
|
-
return errors.length > 0
|
|
209
|
-
? new ValidationError(errors, { startDate, endDate }, this.path)
|
|
210
|
-
: true;
|
|
186
|
+
return errors.length > 0 ? new ValidationError(errors, { startDate, endDate }, this.path) : true;
|
|
211
187
|
},
|
|
212
188
|
});
|
|
213
189
|
}
|
package/src/index.js
CHANGED
|
@@ -5,8 +5,8 @@ import phone from './phone';
|
|
|
5
5
|
import AvDate from './date';
|
|
6
6
|
import DateRange from './dateRange';
|
|
7
7
|
|
|
8
|
-
export const avDate = opts => new AvDate(opts);
|
|
9
|
-
export const dateRange = opts => new DateRange(opts);
|
|
8
|
+
export const avDate = (opts) => new AvDate(opts);
|
|
9
|
+
export const dateRange = (opts) => new DateRange(opts);
|
|
10
10
|
|
|
11
11
|
addMethod(string, 'isRequired', isRequired);
|
|
12
12
|
addMethod(number, 'isRequired', isRequired);
|
package/src/phone.js
CHANGED
|
@@ -5,15 +5,10 @@ declare module 'yup' {
|
|
|
5
5
|
format?: string | 'MM/DD/YYYY';
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
interface AvDateSchema<T extends string | null | undefined = string>
|
|
9
|
-
extends MixedSchema<T> {
|
|
8
|
+
interface AvDateSchema<T extends string | null | undefined = string> extends MixedSchema<T> {
|
|
10
9
|
min(date: string, errorMessage?: string): DateRangeSchema<T>;
|
|
11
10
|
max(date: string, errorMessage?: string): DateRangeSchema<T>;
|
|
12
|
-
between(
|
|
13
|
-
minDate: string,
|
|
14
|
-
maxDate: string,
|
|
15
|
-
errorMessage?: string
|
|
16
|
-
): AvDateSchema<T>;
|
|
11
|
+
between(minDate: string, maxDate: string, errorMessage?: string): AvDateSchema<T>;
|
|
17
12
|
isRequired(required?: boolean, errorMessage?: string): AvDateSchema<T>;
|
|
18
13
|
}
|
|
19
14
|
|
|
@@ -23,18 +23,12 @@ declare module 'yup' {
|
|
|
23
23
|
max?: DistanceValue;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
interface DateRangeSchema<
|
|
27
|
-
T extends DateRangeObject | null | undefined = DateRangeObject
|
|
28
|
-
> extends MixedSchema<T> {
|
|
26
|
+
interface DateRangeSchema<T extends DateRangeObject | null | undefined = DateRangeObject> extends MixedSchema<T> {
|
|
29
27
|
min(date: string, errorMessage?: string): DateRangeSchema<T>;
|
|
30
28
|
max(date: string, errorMessage?: string): DateRangeSchema<T>;
|
|
31
|
-
between(
|
|
32
|
-
minDate: string,
|
|
33
|
-
maxDate: string,
|
|
34
|
-
errorMessage?: string
|
|
35
|
-
): DateRangeSchema<T>;
|
|
29
|
+
between(minDate: string, maxDate: string, errorMessage?: string): DateRangeSchema<T>;
|
|
36
30
|
isRequired(required?: boolean, errorMessage?: string): DateRangeSchema<T>;
|
|
37
|
-
distance(distanceOpts: DistanceOpts)
|
|
31
|
+
distance(distanceOpts: DistanceOpts): DateRangeSchema<T>;
|
|
38
32
|
}
|
|
39
33
|
|
|
40
34
|
export interface DateRangeSchemaConstructor {
|
|
@@ -3,8 +3,7 @@ import './dateRange.d.ts';
|
|
|
3
3
|
import './date.d.ts';
|
|
4
4
|
|
|
5
5
|
declare module 'yup' {
|
|
6
|
-
interface StringSchema<T extends string | null | undefined = string>
|
|
7
|
-
extends Schema<T> {
|
|
6
|
+
interface StringSchema<T extends string | null | undefined = string> extends Schema<T> {
|
|
8
7
|
isRequired(required?: boolean, errorMessage?: string): StringSchema<T>;
|
|
9
8
|
npi(errorMessage?: string): StringSchema<T>;
|
|
10
9
|
phone(errorMessage?: string): StringSchema<T>;
|
|
@@ -14,8 +13,7 @@ declare module 'yup' {
|
|
|
14
13
|
isRequired(required?: boolean, errorMessage?: string): ArraySchema<T>;
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
interface NumberSchema<T extends number | null | undefined = number>
|
|
18
|
-
extends Schema<T> {
|
|
16
|
+
interface NumberSchema<T extends number | null | undefined = number> extends Schema<T> {
|
|
19
17
|
isRequired(required?: boolean, errorMessage?: string): NumberSchema<T>;
|
|
20
18
|
npi(errorMessage?: string): NumberSchema<T>;
|
|
21
19
|
phone(errorMessage?: string): NumberSchema<T>;
|
package/tests/date.test.js
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { avDate } from '../src';
|
|
2
|
-
|
|
3
|
-
const INVALID = 'Date is invalid.';
|
|
4
|
-
|
|
5
|
-
const defaults = {};
|
|
6
|
-
|
|
7
|
-
const validate = async (date, { format, min, max, message } = defaults) => {
|
|
8
|
-
let schema = avDate({
|
|
9
|
-
format,
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
if (min && !max) {
|
|
13
|
-
schema = schema.min(min, message);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (max && !min) {
|
|
17
|
-
schema = schema.max(max, message);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (min && max) {
|
|
21
|
-
schema = schema.between(min, max, message);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return schema.validate(date);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
describe('Date', () => {
|
|
28
|
-
test('valid', async () => {
|
|
29
|
-
await expect(validate('12/12/2012')).resolves.toBeTruthy();
|
|
30
|
-
await expect(validate('12-12-2012')).resolves.toBeTruthy();
|
|
31
|
-
await expect(
|
|
32
|
-
validate('2012/12/12', { format: 'YYYY/MM/DD' })
|
|
33
|
-
).resolves.toBeTruthy();
|
|
34
|
-
await expect(
|
|
35
|
-
validate('20121212', { format: 'YYYYMMDD' })
|
|
36
|
-
).resolves.toBeTruthy();
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('invalid', async () => {
|
|
40
|
-
await expect(validate('2012')).rejects.toThrow(INVALID);
|
|
41
|
-
await expect(validate('12-2012')).rejects.toThrow(INVALID);
|
|
42
|
-
await expect(validate('12-2012-12')).rejects.toThrow(INVALID);
|
|
43
|
-
await expect(validate('invalid-date')).rejects.toThrow(INVALID);
|
|
44
|
-
await expect(validate('01/01/20011')).rejects.toThrow(INVALID);
|
|
45
|
-
|
|
46
|
-
// Allow user to clear date field without showing typeError
|
|
47
|
-
await expect(validate('')).resolves.toBeTruthy();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('min date', async () => {
|
|
51
|
-
await expect(
|
|
52
|
-
validate('12/12/2012', {
|
|
53
|
-
min: '12/11/2012',
|
|
54
|
-
})
|
|
55
|
-
).resolves.toBeTruthy();
|
|
56
|
-
|
|
57
|
-
await expect(
|
|
58
|
-
validate('12/12/2012', {
|
|
59
|
-
min: '12/12/2012',
|
|
60
|
-
})
|
|
61
|
-
).resolves.toBeTruthy();
|
|
62
|
-
|
|
63
|
-
await expect(
|
|
64
|
-
validate('12/10/2012', {
|
|
65
|
-
min: '12/11/2012',
|
|
66
|
-
})
|
|
67
|
-
).rejects.toThrow(`Date must be 12/11/2012 or later.`);
|
|
68
|
-
|
|
69
|
-
await expect(
|
|
70
|
-
validate('2012/12/12', {
|
|
71
|
-
min: '2012/12/11',
|
|
72
|
-
format: 'YYYY/MM/DD',
|
|
73
|
-
})
|
|
74
|
-
).resolves.toBeTruthy();
|
|
75
|
-
|
|
76
|
-
await expect(
|
|
77
|
-
validate('2012/12/10', {
|
|
78
|
-
min: '2012/12/11',
|
|
79
|
-
format: 'YYYY/MM/DD',
|
|
80
|
-
})
|
|
81
|
-
).rejects.toThrow();
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test('max date', async () => {
|
|
85
|
-
await expect(
|
|
86
|
-
validate('12/11/2012', {
|
|
87
|
-
max: '12/12/2012',
|
|
88
|
-
})
|
|
89
|
-
).resolves.toBeTruthy();
|
|
90
|
-
|
|
91
|
-
await expect(
|
|
92
|
-
validate('12/12/2012', {
|
|
93
|
-
max: '12/12/2012',
|
|
94
|
-
})
|
|
95
|
-
).resolves.toBeTruthy();
|
|
96
|
-
|
|
97
|
-
await expect(
|
|
98
|
-
validate('12/12/2012', {
|
|
99
|
-
max: '12/11/2012',
|
|
100
|
-
})
|
|
101
|
-
).rejects.toThrow(`Date must be 12/11/2012 or earlier.`);
|
|
102
|
-
|
|
103
|
-
await expect(
|
|
104
|
-
validate('2012/12/11', {
|
|
105
|
-
max: '2012/12/11',
|
|
106
|
-
format: 'YYYY/MM/DD',
|
|
107
|
-
})
|
|
108
|
-
).resolves.toBeTruthy();
|
|
109
|
-
|
|
110
|
-
await expect(
|
|
111
|
-
validate('2012/12/12', {
|
|
112
|
-
max: '2012/12/11',
|
|
113
|
-
format: 'YYYY/MM/DD',
|
|
114
|
-
})
|
|
115
|
-
).rejects.toThrow();
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test('between', async () => {
|
|
119
|
-
await expect(
|
|
120
|
-
validate('12/11/2012', {
|
|
121
|
-
max: '12/12/2012',
|
|
122
|
-
min: '12/10/2012',
|
|
123
|
-
})
|
|
124
|
-
).resolves.toBeTruthy();
|
|
125
|
-
|
|
126
|
-
await expect(
|
|
127
|
-
validate('12/12/2012', {
|
|
128
|
-
min: '12/12/2012',
|
|
129
|
-
max: '12/13/2012',
|
|
130
|
-
})
|
|
131
|
-
).rejects.toThrow(`Date must be between 12/12/2012 and 12/13/2012.`);
|
|
132
|
-
|
|
133
|
-
await expect(
|
|
134
|
-
validate('2012/12/11', {
|
|
135
|
-
max: '2012/12/13',
|
|
136
|
-
min: '2012/12/10',
|
|
137
|
-
format: 'YYYY/MM/DD',
|
|
138
|
-
})
|
|
139
|
-
).resolves.toBeTruthy();
|
|
140
|
-
|
|
141
|
-
await expect(
|
|
142
|
-
validate('2012/12/13', {
|
|
143
|
-
min: '2012/12/11',
|
|
144
|
-
max: '2012/12/12',
|
|
145
|
-
format: 'YYYY/MM/DD',
|
|
146
|
-
})
|
|
147
|
-
).rejects.toThrow();
|
|
148
|
-
});
|
|
149
|
-
});
|
package/tests/dateRange.test.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { dateRange } from '../src';
|
|
2
|
-
|
|
3
|
-
describe('DateRange', () => {
|
|
4
|
-
test('start comes before end', async () => {
|
|
5
|
-
const schema = dateRange();
|
|
6
|
-
|
|
7
|
-
await expect(
|
|
8
|
-
schema.validate({
|
|
9
|
-
startDate: '12/14/2012',
|
|
10
|
-
endDate: '12/13/2012',
|
|
11
|
-
})
|
|
12
|
-
).rejects.toThrow('Start date must come before end date.');
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test('Invalid Dates', async () => {
|
|
16
|
-
const schema = dateRange();
|
|
17
|
-
|
|
18
|
-
await expect(
|
|
19
|
-
schema.validate({
|
|
20
|
-
startDate: '',
|
|
21
|
-
endDate: '12/14/2012',
|
|
22
|
-
})
|
|
23
|
-
).rejects.toThrow('Start and End Date are required.');
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('defines min', async () => {
|
|
27
|
-
const schema = dateRange().min('12/12/2012');
|
|
28
|
-
|
|
29
|
-
const valid = await schema.isValid({
|
|
30
|
-
startDate: '12/13/2012',
|
|
31
|
-
endDate: '12/14/2012',
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
expect(valid).toBe(true);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test('errors on invalid', async () => {
|
|
38
|
-
const schema = dateRange().min('12/12/2012');
|
|
39
|
-
|
|
40
|
-
const valid = await schema.isValid({
|
|
41
|
-
startDate: '12/11/2012',
|
|
42
|
-
endDate: '12/13/2012',
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
expect(valid).toBe(false);
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
describe('date', () => {
|
|
50
|
-
test('should validate', async () => {
|
|
51
|
-
const schema = dateRange().between('12/10/2012', '12/13/2012');
|
|
52
|
-
|
|
53
|
-
const valid = await schema.isValid({
|
|
54
|
-
startDate: '12/11/2012',
|
|
55
|
-
endDate: '12/12/2012',
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
expect(valid).toBe(true);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test('should work with custom value keys', async () => {
|
|
62
|
-
const schema = dateRange({
|
|
63
|
-
startKey: 'helloDate',
|
|
64
|
-
endKey: 'worldDate',
|
|
65
|
-
}).between('12/10/2012', '12/13/2012');
|
|
66
|
-
|
|
67
|
-
const valid = await schema.isValid({
|
|
68
|
-
helloDate: '12/11/2012',
|
|
69
|
-
worldDate: '12/12/2012',
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
expect(valid).toBe(true);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('should validate distance', async () => {
|
|
76
|
-
const schema = dateRange().distance({
|
|
77
|
-
min: {
|
|
78
|
-
value: 5,
|
|
79
|
-
units: 'day',
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
let valid = await schema.isValid({
|
|
84
|
-
startDate: '12/11/2012',
|
|
85
|
-
endDate: '12/13/2012',
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
expect(valid).toBe(false);
|
|
89
|
-
|
|
90
|
-
valid = await schema.isValid({
|
|
91
|
-
startDate: '12/11/2012',
|
|
92
|
-
endDate: '12/18/2012',
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
expect(valid).toBe(true);
|
|
96
|
-
});
|
|
97
|
-
});
|
package/tests/isRequired.test.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { array, number, string } from 'yup';
|
|
2
|
-
import '../src';
|
|
3
|
-
|
|
4
|
-
describe('isRequired', () => {
|
|
5
|
-
test('should return error on empty input', async () => {
|
|
6
|
-
const schema = string().isRequired();
|
|
7
|
-
|
|
8
|
-
const valid = await schema.isValid('');
|
|
9
|
-
|
|
10
|
-
expect(valid).toBe(false);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test('should return error on no number', async () => {
|
|
14
|
-
const schema = number().isRequired();
|
|
15
|
-
|
|
16
|
-
const valid = await schema.isValid(null);
|
|
17
|
-
|
|
18
|
-
expect(valid).toBe(false);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('should return error on empty array', async () => {
|
|
22
|
-
const schema = array().isRequired();
|
|
23
|
-
|
|
24
|
-
const valid = await schema.isValid([]);
|
|
25
|
-
|
|
26
|
-
expect(valid).toBe(false);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test('should accept null input', async () => {
|
|
30
|
-
const schema = string()
|
|
31
|
-
.isRequired(true)
|
|
32
|
-
.nullable();
|
|
33
|
-
|
|
34
|
-
const valid = await schema.isValid(null);
|
|
35
|
-
|
|
36
|
-
expect(valid).toBe(true);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('should accept input', async () => {
|
|
40
|
-
const schema = string().isRequired(true);
|
|
41
|
-
|
|
42
|
-
const valid = await schema.isValid('Test');
|
|
43
|
-
|
|
44
|
-
expect(valid).toBe(true);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test('should render custom error message', async () => {
|
|
48
|
-
const schema = string().isRequired(true, 'Test Error Message');
|
|
49
|
-
|
|
50
|
-
await expect(schema.validate()).rejects.toThrow('Test Error Message');
|
|
51
|
-
});
|
|
52
|
-
});
|
package/tests/npi.test.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { number, string } from 'yup';
|
|
2
|
-
import '../src';
|
|
3
|
-
|
|
4
|
-
describe('npi', () => {
|
|
5
|
-
test('should be valid for empty input', async () => {
|
|
6
|
-
const schema = string().npi();
|
|
7
|
-
|
|
8
|
-
const valid = await schema.isValid('');
|
|
9
|
-
|
|
10
|
-
expect(valid).toBe(true);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test('should be valid for no number', async () => {
|
|
14
|
-
const schema = number().npi();
|
|
15
|
-
|
|
16
|
-
const valid = await schema.isValid();
|
|
17
|
-
|
|
18
|
-
expect(valid).toBe(true);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('should accept null input', async () => {
|
|
22
|
-
const schema = string()
|
|
23
|
-
.npi()
|
|
24
|
-
.nullable();
|
|
25
|
-
|
|
26
|
-
const valid = await schema.isValid(null);
|
|
27
|
-
|
|
28
|
-
expect(valid).toBe(true);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('should be invalid for invalid input', async () => {
|
|
32
|
-
const schema = string().npi();
|
|
33
|
-
|
|
34
|
-
const valid = await schema.isValid('1234');
|
|
35
|
-
|
|
36
|
-
expect(valid).toBe(false);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('should be invalid if NPI contains non-digits', async () => {
|
|
40
|
-
const schema = number().npi();
|
|
41
|
-
const valid = await schema.isValid('i2eh56789o');
|
|
42
|
-
expect(valid).toBe(false);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test('should be invalid if NPI is not 10 digits in length', async () => {
|
|
46
|
-
const schema = number().npi();
|
|
47
|
-
let valid = await schema.isValid('123456789');
|
|
48
|
-
expect(valid).toBe(false);
|
|
49
|
-
|
|
50
|
-
valid = await schema.isValid('12345678901');
|
|
51
|
-
expect(valid).toBe(false);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test('should be invalid if NPI does not start with a 1, 2, 3, or 4', async () => {
|
|
55
|
-
const schema = number().npi();
|
|
56
|
-
const valid = await schema.isValid('5678901234');
|
|
57
|
-
expect(valid).toBe(false);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test('should be invalid if NPI checksum does not match check digit', async () => {
|
|
61
|
-
const schema = number().npi();
|
|
62
|
-
const valid = await schema.isValid('1234567890');
|
|
63
|
-
expect(valid).toBe(false);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test('should be valid if NPI is valid', async () => {
|
|
67
|
-
const schema = number().npi();
|
|
68
|
-
const valid = await schema.isValid('1234567893');
|
|
69
|
-
expect(valid).toBe(true);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test('should render custom error message', async () => {
|
|
73
|
-
const schema = number().npi('Test Error Message');
|
|
74
|
-
|
|
75
|
-
await expect(schema.validate('123')).rejects.toThrow('Test Error Message');
|
|
76
|
-
});
|
|
77
|
-
});
|
package/tests/phone.test.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { number, string } from 'yup';
|
|
2
|
-
import '../src';
|
|
3
|
-
|
|
4
|
-
describe('phone', () => {
|
|
5
|
-
test('should accept empty string', async () => {
|
|
6
|
-
const schema = string().phone();
|
|
7
|
-
const valid = await schema.isValid('');
|
|
8
|
-
expect(valid).toBe(true);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test('should accept no number', async () => {
|
|
12
|
-
const schema = number().phone();
|
|
13
|
-
const valid = await schema.isValid();
|
|
14
|
-
expect(valid).toBe(true);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test('should accept null input', async () => {
|
|
18
|
-
const schema = string()
|
|
19
|
-
.phone()
|
|
20
|
-
.nullable();
|
|
21
|
-
const valid = await schema.isValid();
|
|
22
|
-
expect(valid).toBe(true);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test('should not accept less than 10 digits', async () => {
|
|
26
|
-
const schema = number().phone();
|
|
27
|
-
const valid = await schema.isValid('123456789');
|
|
28
|
-
expect(valid).toBe(false);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('should accept country code of "+1" and variations', async () => {
|
|
32
|
-
const schema = string().phone();
|
|
33
|
-
expect(await schema.isValid('+14444444444')).toBe(true);
|
|
34
|
-
expect(await schema.isValid('14444444444')).toBe(true);
|
|
35
|
-
expect(await schema.isValid('+1 4444444444')).toBe(true);
|
|
36
|
-
expect(await schema.isValid('1 4444444444')).toBe(true);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('must be 10 digits without country code', async () => {
|
|
40
|
-
const schema = number().phone();
|
|
41
|
-
expect(await schema.isValid('4444444444')).toBe(true);
|
|
42
|
-
expect(await schema.isValid('444 444 4444')).toBe(true);
|
|
43
|
-
expect(await schema.isValid('44444444445')).toBe(false);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test('can be formatted', async () => {
|
|
47
|
-
const schema = string().phone();
|
|
48
|
-
expect(await schema.isValid('(444) 444-4444')).toBe(true);
|
|
49
|
-
expect(await schema.isValid('+1 (444) 444-4444')).toBe(true);
|
|
50
|
-
expect(await schema.isValid('444-444-4444')).toBe(true);
|
|
51
|
-
expect(await schema.isValid('+1 444-444-4444')).toBe(true);
|
|
52
|
-
expect(await schema.isValid('444.444.4444')).toBe(true);
|
|
53
|
-
expect(await schema.isValid('(444) 444 4444')).toBe(true);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test('should display custom error message', async () => {
|
|
57
|
-
const schema = number().phone('Custom Error Message');
|
|
58
|
-
await expect(schema.validate('123321')).rejects.toThrow(
|
|
59
|
-
'Custom Error Message'
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
});
|