react-hook-form-rules 1.0.8 → 1.0.10
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/README.md +57 -43
- package/dist/index.d.ts +11 -11
- package/dist/index.js +14 -14
- package/dist/rules/array/get-max-array-length.d.ts +9 -0
- package/dist/rules/array/get-max-array-length.js +17 -0
- package/dist/rules/array/get-min-array-length.d.ts +9 -0
- package/dist/rules/array/get-min-array-length.js +17 -0
- package/dist/rules/array/get-required-array-rule.d.ts +8 -0
- package/dist/rules/array/get-required-array-rule.js +15 -0
- package/dist/rules/default/get-max-length-rule.d.ts +11 -0
- package/dist/rules/default/get-max-length-rule.js +22 -0
- package/dist/rules/default/get-max-rule.d.ts +11 -0
- package/dist/rules/default/get-max-rule.js +15 -0
- package/dist/rules/default/get-min-length-rule.d.ts +11 -0
- package/dist/rules/default/get-min-length-rule.js +22 -0
- package/dist/rules/default/get-min-rule.d.ts +11 -0
- package/dist/rules/default/get-min-rule.js +15 -0
- package/dist/rules/default/get-require-rule.d.ts +11 -0
- package/dist/rules/default/get-require-rule.js +16 -0
- package/dist/rules/link/get-email-rule.d.ts +17 -0
- package/dist/rules/link/get-email-rule.js +20 -0
- package/dist/rules/link/get-url-rule.d.ts +17 -0
- package/dist/rules/link/get-url-rule.js +20 -0
- package/dist/rules/object/get-required-object-rule.d.ts +8 -0
- package/dist/rules/object/get-required-object-rule.js +20 -0
- package/index.ts +13 -11
- package/package.json +2 -2
- package/rules/array/get-max-array-length.ts +19 -0
- package/rules/array/get-min-array-length.ts +19 -0
- package/rules/array/get-required-array-rule.ts +12 -0
- package/rules/{get-max-length-rule.ts → default/get-max-length-rule.ts} +4 -4
- package/rules/default/get-max-rule.ts +11 -0
- package/rules/default/get-min-length-rule.ts +22 -0
- package/rules/default/get-min-rule.ts +11 -0
- package/rules/default/get-require-rule.ts +16 -0
- package/rules/link/get-email-rule.ts +26 -0
- package/rules/{get-url-rule.ts → link/get-url-rule.ts} +5 -5
- package/rules/{get-required-object-rule.ts → object/get-required-object-rule.ts} +3 -3
- package/rules/get-email-rule.ts +0 -25
- package/rules/get-max-array-length.ts +0 -18
- package/rules/get-max-rule.ts +0 -11
- package/rules/get-min-array-length.ts +0 -18
- package/rules/get-min-length-rule.ts +0 -22
- package/rules/get-min-rule.ts +0 -20
- package/rules/get-require-rule.ts +0 -16
- package/rules/get-required-array-rule.ts +0 -12
package/README.md
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
# react-hook-form-rules
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This is a small library that contains a few rules for clearer validation handling in the [react-hook-form](https://www.react-hook-form.com/) library.
|
|
4
|
+
|
|
5
|
+
### Contacts
|
|
6
|
+
|
|
7
|
+
Hello everyone 👋.
|
|
8
|
+
|
|
9
|
+
If you have any questions or suggestions, [my github profile](https://github.com/chopperqt).
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Rules
|
|
14
|
+
|
|
15
|
+
### getRequiredRule
|
|
16
|
+
|
|
17
|
+
#### Description
|
|
4
18
|
This rule makes the field required.
|
|
5
19
|
|
|
6
|
-
|
|
20
|
+
#### Options
|
|
7
21
|
| Option | Type | Default | Description |
|
|
8
22
|
| -------- | --------- | ------ | ---------- |
|
|
9
23
|
| isRequired | boolean | true | |
|
|
10
24
|
| message | string | "This field is required." | . |
|
|
11
25
|
|
|
12
|
-
|
|
26
|
+
#### Code example
|
|
13
27
|
```
|
|
14
28
|
import { getRequiredRule } from 'react-hook-form-rules';
|
|
15
29
|
|
|
@@ -25,20 +39,20 @@ const {...} = useController({
|
|
|
25
39
|
|
|
26
40
|
|
|
27
41
|
|
|
28
|
-
|
|
42
|
+
### getRequiredArrayRule
|
|
29
43
|
|
|
30
|
-
|
|
44
|
+
#### Description
|
|
31
45
|
This rule makes the array field required.
|
|
32
46
|
|
|
33
47
|
|
|
34
|
-
|
|
48
|
+
#### Options
|
|
35
49
|
| Option | Type | Default | Description |
|
|
36
50
|
| -------- | --------- | ------ | ---------- |
|
|
37
51
|
| arr | array | [] | |
|
|
38
52
|
| message | string | "This field is required." | |
|
|
39
53
|
|
|
40
54
|
|
|
41
|
-
|
|
55
|
+
#### Code example
|
|
42
56
|
```
|
|
43
57
|
import { getRequiredArrayRule } from 'react-hook-form-rules';
|
|
44
58
|
|
|
@@ -53,20 +67,20 @@ const {...} = useController({
|
|
|
53
67
|
|
|
54
68
|
|
|
55
69
|
|
|
56
|
-
|
|
70
|
+
### getRequiredObjectRule
|
|
57
71
|
|
|
58
|
-
|
|
72
|
+
#### Description
|
|
59
73
|
This rule makes the object field required.
|
|
60
74
|
|
|
61
75
|
|
|
62
|
-
|
|
76
|
+
#### Options
|
|
63
77
|
| Option | Type | Default | Description |
|
|
64
78
|
| -------- | --------- | ------ | ---------- |
|
|
65
79
|
| obj | object | {} | |
|
|
66
80
|
| message | string | "This field is required." | |
|
|
67
81
|
|
|
68
82
|
|
|
69
|
-
|
|
83
|
+
#### Code example
|
|
70
84
|
```
|
|
71
85
|
import { getRequiredObjectRule } from 'react-hook-form-rules';
|
|
72
86
|
|
|
@@ -80,18 +94,18 @@ const {...} = useController({
|
|
|
80
94
|
|
|
81
95
|
|
|
82
96
|
|
|
83
|
-
|
|
97
|
+
### getMaxRule
|
|
84
98
|
|
|
85
|
-
|
|
99
|
+
#### Description
|
|
86
100
|
This rule returns an error if the number is greater than the specified value.
|
|
87
101
|
|
|
88
|
-
|
|
102
|
+
#### Options
|
|
89
103
|
| Option | Type | Default | Description |
|
|
90
104
|
| -------- | --------- | ------ | ---------- |
|
|
91
105
|
| max | number | - | - |
|
|
92
106
|
| message | string | - | - |
|
|
93
107
|
|
|
94
|
-
|
|
108
|
+
#### Code example
|
|
95
109
|
```
|
|
96
110
|
import { getMaxRule } from 'react-hook-form-rules';
|
|
97
111
|
|
|
@@ -105,12 +119,12 @@ const {...} = useController({
|
|
|
105
119
|
|
|
106
120
|
|
|
107
121
|
|
|
108
|
-
|
|
122
|
+
### getMaxLengthRule
|
|
109
123
|
|
|
110
|
-
|
|
124
|
+
#### Description
|
|
111
125
|
This rule checks that the number of characters in the string does not exceed the specified value.
|
|
112
126
|
|
|
113
|
-
|
|
127
|
+
#### Options
|
|
114
128
|
| Option | Type | Default | Description |
|
|
115
129
|
| -------- | --------- | ------ | ---------- |
|
|
116
130
|
| maxLength | number | - | - |
|
|
@@ -131,19 +145,19 @@ const {...} = useController({
|
|
|
131
145
|
|
|
132
146
|
|
|
133
147
|
|
|
134
|
-
|
|
148
|
+
### getMaxArrayLengthRule
|
|
135
149
|
|
|
136
|
-
|
|
150
|
+
#### Description
|
|
137
151
|
This rule returns an error if the number of values in the array exceeds the specified limit.
|
|
138
152
|
|
|
139
|
-
|
|
153
|
+
#### Options
|
|
140
154
|
| Option | Type | Default | Description |
|
|
141
155
|
| -------- | --------- | ------ | ---------- |
|
|
142
156
|
| arr | arrray | - | - |
|
|
143
157
|
| maxLength | number | - | - |
|
|
144
158
|
| message | string | - | - |
|
|
145
159
|
|
|
146
|
-
|
|
160
|
+
#### Code example
|
|
147
161
|
```
|
|
148
162
|
import { getMaxArrayLengthRule } from 'react-hook-form-rules';
|
|
149
163
|
|
|
@@ -156,18 +170,18 @@ const {...} = useController({
|
|
|
156
170
|
|
|
157
171
|
|
|
158
172
|
|
|
159
|
-
|
|
173
|
+
### getMinRule
|
|
160
174
|
|
|
161
|
-
|
|
175
|
+
#### Description
|
|
162
176
|
This rule checks that the string contains a minimum number of characters.
|
|
163
177
|
|
|
164
|
-
|
|
178
|
+
#### Options
|
|
165
179
|
| Option | Type | Default | Description |
|
|
166
180
|
| -------- | --------- | ------ | ---------- |
|
|
167
181
|
| min | number | - | - |
|
|
168
182
|
| message | string | - | - |
|
|
169
183
|
|
|
170
|
-
|
|
184
|
+
#### Code example
|
|
171
185
|
```
|
|
172
186
|
import { getMinRule } from 'react-hook-form-rules';
|
|
173
187
|
|
|
@@ -181,18 +195,18 @@ const {...} = useController({
|
|
|
181
195
|
|
|
182
196
|
|
|
183
197
|
|
|
184
|
-
|
|
198
|
+
### getMinLengthRule
|
|
185
199
|
|
|
186
|
-
|
|
200
|
+
#### Description
|
|
187
201
|
The rule checks that the string contains at least the specified number of characters.
|
|
188
202
|
|
|
189
|
-
|
|
203
|
+
#### Options
|
|
190
204
|
| Option | Type | Default | Description |
|
|
191
205
|
| -------- | --------- | ------ | ---------- |
|
|
192
206
|
| minLength | number | - | - |
|
|
193
207
|
| message | string | - | - |
|
|
194
208
|
|
|
195
|
-
|
|
209
|
+
#### Code example
|
|
196
210
|
```
|
|
197
211
|
import { getMinLengthRule } from 'react-hook-form-rules';
|
|
198
212
|
|
|
@@ -206,19 +220,19 @@ const {...} = useController({
|
|
|
206
220
|
|
|
207
221
|
|
|
208
222
|
|
|
209
|
-
|
|
223
|
+
### getMinArrayLengthRule
|
|
210
224
|
|
|
211
|
-
|
|
225
|
+
#### Description
|
|
212
226
|
The rule checks that the array contains a minimum number of elements.
|
|
213
227
|
|
|
214
|
-
|
|
228
|
+
#### Options
|
|
215
229
|
| Option | Type | Default | Description |
|
|
216
230
|
| -------- | --------- | ------ | ---------- |
|
|
217
231
|
| arr | arrray | - | - |
|
|
218
232
|
| minLength | number | - | - |
|
|
219
233
|
| message | string | - | - |
|
|
220
234
|
|
|
221
|
-
|
|
235
|
+
#### Code example
|
|
222
236
|
```
|
|
223
237
|
import { getMinArrayLengthRule } from 'react-hook-form-rules';
|
|
224
238
|
|
|
@@ -229,18 +243,18 @@ const {...} = useController({
|
|
|
229
243
|
})
|
|
230
244
|
```
|
|
231
245
|
|
|
232
|
-
|
|
246
|
+
### getEmailRule
|
|
233
247
|
|
|
234
|
-
|
|
248
|
+
#### Description
|
|
235
249
|
he rule checks the entered email for correctness.
|
|
236
250
|
|
|
237
|
-
|
|
251
|
+
#### Options
|
|
238
252
|
| Option | Type | Default | Description |
|
|
239
253
|
| -------- | --------- | ------ | ---------- |
|
|
240
254
|
| pattern | RegExp | - | - |
|
|
241
255
|
| message | string | - | - |
|
|
242
256
|
|
|
243
|
-
|
|
257
|
+
#### Code example
|
|
244
258
|
```
|
|
245
259
|
import { getEmailRule } from 'react-hook-form-rules';
|
|
246
260
|
|
|
@@ -254,18 +268,18 @@ const {...} = useController({
|
|
|
254
268
|
|
|
255
269
|
|
|
256
270
|
|
|
257
|
-
|
|
271
|
+
### getUrlRule
|
|
258
272
|
|
|
259
|
-
|
|
273
|
+
#### Description
|
|
260
274
|
The rule checks the entered URL for correctness.
|
|
261
275
|
|
|
262
|
-
|
|
276
|
+
#### Options
|
|
263
277
|
| Option | Type | Default | Description |
|
|
264
278
|
| -------- | --------- | ------ | ---------- |
|
|
265
279
|
| pattern | RegExp | - | - |
|
|
266
280
|
| message | string | - | - |
|
|
267
281
|
|
|
268
|
-
|
|
282
|
+
#### Code example
|
|
269
283
|
```
|
|
270
284
|
import { getUrlRule } from 'react-hook-form-rules';
|
|
271
285
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { getEmailRule } from "./rules/get-email-rule";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { getMinArrayLengthRule } from "./rules/get-min-array-length";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
1
|
+
import { getEmailRule } from "./rules/link/get-email-rule";
|
|
2
|
+
import { getUrlRule } from "./rules/link/get-url-rule";
|
|
3
|
+
import { getMaxArrayLengthRule } from "./rules/array/get-max-array-length";
|
|
4
|
+
import { getMinArrayLengthRule } from "./rules/array/get-min-array-length";
|
|
5
|
+
import { getRequiredArrayRule } from "./rules/array/get-required-array-rule";
|
|
6
|
+
import { getMaxLengthRule } from "./rules/default/get-max-length-rule";
|
|
7
|
+
import { getMaxRule } from "./rules/default/get-max-rule";
|
|
8
|
+
import { getMinLengthRule } from "./rules/default/get-min-length-rule";
|
|
9
|
+
import { getMinRule } from "./rules/default/get-min-rule";
|
|
10
|
+
import { getRequiredRule } from "./rules/default/get-require-rule";
|
|
11
|
+
import { getRequiredObjectRule } from "./rules/object/get-required-object-rule";
|
|
12
12
|
export { getMaxRule, getRequiredObjectRule, getRequiredArrayRule, getEmailRule, getMaxLengthRule, getMaxArrayLengthRule, getMinArrayLengthRule, getMinLengthRule, getMinRule, getRequiredRule, getUrlRule, };
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getUrlRule = exports.getRequiredRule = exports.getMinRule = exports.getMinLengthRule = exports.getMinArrayLengthRule = exports.getMaxArrayLengthRule = exports.getMaxLengthRule = exports.getEmailRule = exports.getRequiredArrayRule = exports.getRequiredObjectRule = exports.getMaxRule = void 0;
|
|
4
|
-
const get_email_rule_1 = require("./rules/get-email-rule");
|
|
4
|
+
const get_email_rule_1 = require("./rules/link/get-email-rule");
|
|
5
5
|
Object.defineProperty(exports, "getEmailRule", { enumerable: true, get: function () { return get_email_rule_1.getEmailRule; } });
|
|
6
|
-
const
|
|
6
|
+
const get_url_rule_1 = require("./rules/link/get-url-rule");
|
|
7
|
+
Object.defineProperty(exports, "getUrlRule", { enumerable: true, get: function () { return get_url_rule_1.getUrlRule; } });
|
|
8
|
+
const get_max_array_length_1 = require("./rules/array/get-max-array-length");
|
|
7
9
|
Object.defineProperty(exports, "getMaxArrayLengthRule", { enumerable: true, get: function () { return get_max_array_length_1.getMaxArrayLengthRule; } });
|
|
8
|
-
const
|
|
9
|
-
Object.defineProperty(exports, "getMaxLengthRule", { enumerable: true, get: function () { return get_max_length_rule_1.getMaxLengthRule; } });
|
|
10
|
-
const get_min_array_length_1 = require("./rules/get-min-array-length");
|
|
10
|
+
const get_min_array_length_1 = require("./rules/array/get-min-array-length");
|
|
11
11
|
Object.defineProperty(exports, "getMinArrayLengthRule", { enumerable: true, get: function () { return get_min_array_length_1.getMinArrayLengthRule; } });
|
|
12
|
-
const
|
|
12
|
+
const get_required_array_rule_1 = require("./rules/array/get-required-array-rule");
|
|
13
|
+
Object.defineProperty(exports, "getRequiredArrayRule", { enumerable: true, get: function () { return get_required_array_rule_1.getRequiredArrayRule; } });
|
|
14
|
+
const get_max_length_rule_1 = require("./rules/default/get-max-length-rule");
|
|
15
|
+
Object.defineProperty(exports, "getMaxLengthRule", { enumerable: true, get: function () { return get_max_length_rule_1.getMaxLengthRule; } });
|
|
16
|
+
const get_max_rule_1 = require("./rules/default/get-max-rule");
|
|
13
17
|
Object.defineProperty(exports, "getMaxRule", { enumerable: true, get: function () { return get_max_rule_1.getMaxRule; } });
|
|
14
|
-
const get_min_length_rule_1 = require("./rules/get-min-length-rule");
|
|
18
|
+
const get_min_length_rule_1 = require("./rules/default/get-min-length-rule");
|
|
15
19
|
Object.defineProperty(exports, "getMinLengthRule", { enumerable: true, get: function () { return get_min_length_rule_1.getMinLengthRule; } });
|
|
16
|
-
const get_min_rule_1 = require("./rules/get-min-rule");
|
|
20
|
+
const get_min_rule_1 = require("./rules/default/get-min-rule");
|
|
17
21
|
Object.defineProperty(exports, "getMinRule", { enumerable: true, get: function () { return get_min_rule_1.getMinRule; } });
|
|
18
|
-
const get_require_rule_1 = require("./rules/get-require-rule");
|
|
22
|
+
const get_require_rule_1 = require("./rules/default/get-require-rule");
|
|
19
23
|
Object.defineProperty(exports, "getRequiredRule", { enumerable: true, get: function () { return get_require_rule_1.getRequiredRule; } });
|
|
20
|
-
const
|
|
21
|
-
Object.defineProperty(exports, "getRequiredArrayRule", { enumerable: true, get: function () { return get_required_array_rule_1.getRequiredArrayRule; } });
|
|
22
|
-
const get_required_object_rule_1 = require("./rules/get-required-object-rule");
|
|
24
|
+
const get_required_object_rule_1 = require("./rules/object/get-required-object-rule");
|
|
23
25
|
Object.defineProperty(exports, "getRequiredObjectRule", { enumerable: true, get: function () { return get_required_object_rule_1.getRequiredObjectRule; } });
|
|
24
|
-
const get_url_rule_1 = require("./rules/get-url-rule");
|
|
25
|
-
Object.defineProperty(exports, "getUrlRule", { enumerable: true, get: function () { return get_url_rule_1.getUrlRule; } });
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates an array for the maximum number of values.
|
|
3
|
+
* @param arr - Array of values.
|
|
4
|
+
* @param maxLength - Maximum number of values.
|
|
5
|
+
* @param message - Custom message.
|
|
6
|
+
*
|
|
7
|
+
* @example getMaxArrayLengthRule(2, "Maximum number of values is 2").
|
|
8
|
+
*/
|
|
9
|
+
export declare const getMaxArrayLengthRule: (arr: unknown[], maxLength: number, message?: string) => string | true;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMaxArrayLengthRule = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Validates an array for the maximum number of values.
|
|
6
|
+
* @param arr - Array of values.
|
|
7
|
+
* @param maxLength - Maximum number of values.
|
|
8
|
+
* @param message - Custom message.
|
|
9
|
+
*
|
|
10
|
+
* @example getMaxArrayLengthRule(2, "Maximum number of values is 2").
|
|
11
|
+
*/
|
|
12
|
+
const getMaxArrayLengthRule = (arr, maxLength, message) => {
|
|
13
|
+
const defaultMessage = `Maximum number of values - ${maxLength}.`;
|
|
14
|
+
const formattedMessage = message || defaultMessage;
|
|
15
|
+
return arr.length < maxLength || formattedMessage;
|
|
16
|
+
};
|
|
17
|
+
exports.getMaxArrayLengthRule = getMaxArrayLengthRule;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates an array for the maximum number of values.
|
|
3
|
+
* @param arr - Array of values.
|
|
4
|
+
* @param maxLength - Maximum number of values.
|
|
5
|
+
* @param message - Custom message.
|
|
6
|
+
*
|
|
7
|
+
* @example getMaxArrayLengthRule(3, "Maximum number of values is 2").
|
|
8
|
+
*/
|
|
9
|
+
export declare const getMinArrayLengthRule: (arr: unknown[], minLength: number, message?: string) => string | true;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMinArrayLengthRule = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Validates an array for the maximum number of values.
|
|
6
|
+
* @param arr - Array of values.
|
|
7
|
+
* @param maxLength - Maximum number of values.
|
|
8
|
+
* @param message - Custom message.
|
|
9
|
+
*
|
|
10
|
+
* @example getMaxArrayLengthRule(3, "Maximum number of values is 2").
|
|
11
|
+
*/
|
|
12
|
+
const getMinArrayLengthRule = (arr, minLength, message) => {
|
|
13
|
+
const defaultMessage = `Minimum number of values - ${minLength}.`;
|
|
14
|
+
const formattedMessage = message || defaultMessage;
|
|
15
|
+
return arr.length > minLength || formattedMessage;
|
|
16
|
+
};
|
|
17
|
+
exports.getMinArrayLengthRule = getMinArrayLengthRule;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates an array, checking if it contains values.
|
|
3
|
+
* @param arr - Array of values.
|
|
4
|
+
* @param message - Custom message.
|
|
5
|
+
*
|
|
6
|
+
* @example rules: { validate: (arr) => getRequiredArrayRule(arr) }
|
|
7
|
+
*/
|
|
8
|
+
export declare const getRequiredArrayRule: (arr?: unknown[], message?: string) => string | true;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequiredArrayRule = void 0;
|
|
4
|
+
const REQUIRED_MESSAGE_TEXT = 'Это поле обязательное.';
|
|
5
|
+
/**
|
|
6
|
+
* Validates an array, checking if it contains values.
|
|
7
|
+
* @param arr - Array of values.
|
|
8
|
+
* @param message - Custom message.
|
|
9
|
+
*
|
|
10
|
+
* @example rules: { validate: (arr) => getRequiredArrayRule(arr) }
|
|
11
|
+
*/
|
|
12
|
+
const getRequiredArrayRule = (arr = [], message) => {
|
|
13
|
+
return arr.length > 0 || (message || REQUIRED_MESSAGE_TEXT);
|
|
14
|
+
};
|
|
15
|
+
exports.getRequiredArrayRule = getRequiredArrayRule;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates the maximum length of a string.
|
|
3
|
+
* @param maxLength - Maximum number of characters allowed.
|
|
4
|
+
* @param message - Custom message.
|
|
5
|
+
*
|
|
6
|
+
* @example rules: { maxLength: getMaxLengthRule(1000) }
|
|
7
|
+
*/
|
|
8
|
+
export declare const getMaxLengthRule: (maxLength: number, message?: string) => {
|
|
9
|
+
value: number;
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getMaxLengthRule = void 0;
|
|
7
|
+
const plural_ru_1 = __importDefault(require("plural-ru"));
|
|
8
|
+
/**
|
|
9
|
+
* Validates the maximum length of a string.
|
|
10
|
+
* @param maxLength - Maximum number of characters allowed.
|
|
11
|
+
* @param message - Custom message.
|
|
12
|
+
*
|
|
13
|
+
* @example rules: { maxLength: getMaxLengthRule(1000) }
|
|
14
|
+
*/
|
|
15
|
+
const getMaxLengthRule = (maxLength, message) => {
|
|
16
|
+
const formattedMessage = `Максимальная длина ${maxLength} ${(0, plural_ru_1.default)(maxLength, "символ", "символа", "символов")}.`;
|
|
17
|
+
return {
|
|
18
|
+
value: maxLength,
|
|
19
|
+
message: message || formattedMessage,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
exports.getMaxLengthRule = getMaxLengthRule;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns an error if the number exceeds the specified value.
|
|
3
|
+
* @param max - Maximum number.
|
|
4
|
+
* @param message - Custom message.
|
|
5
|
+
*
|
|
6
|
+
* @example rules: { max: getMaxRule(20) }
|
|
7
|
+
*/
|
|
8
|
+
export declare const getMaxRule: (max: number, message?: string) => {
|
|
9
|
+
value: number;
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMaxRule = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Returns an error if the number exceeds the specified value.
|
|
6
|
+
* @param max - Maximum number.
|
|
7
|
+
* @param message - Custom message.
|
|
8
|
+
*
|
|
9
|
+
* @example rules: { max: getMaxRule(20) }
|
|
10
|
+
*/
|
|
11
|
+
const getMaxRule = (max, message) => ({
|
|
12
|
+
value: max,
|
|
13
|
+
message: message || `Maximum value - ${max}.`,
|
|
14
|
+
});
|
|
15
|
+
exports.getMaxRule = getMaxRule;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a rule that validates the minimum length of a string.
|
|
3
|
+
* @param minLength - Minimum number of characters allowed.
|
|
4
|
+
* @param message - Custom message.
|
|
5
|
+
*
|
|
6
|
+
* @example rules: { minLength: getMinLengthRule(20) }
|
|
7
|
+
*/
|
|
8
|
+
export declare const getMinLengthRule: (minLength: number, message?: string) => {
|
|
9
|
+
value: number;
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getMinLengthRule = void 0;
|
|
7
|
+
const plural_ru_1 = __importDefault(require("plural-ru"));
|
|
8
|
+
/**
|
|
9
|
+
* Returns a rule that validates the minimum length of a string.
|
|
10
|
+
* @param minLength - Minimum number of characters allowed.
|
|
11
|
+
* @param message - Custom message.
|
|
12
|
+
*
|
|
13
|
+
* @example rules: { minLength: getMinLengthRule(20) }
|
|
14
|
+
*/
|
|
15
|
+
const getMinLengthRule = (minLength, message) => {
|
|
16
|
+
const formattedMessage = `Минимальная длина ${minLength} ${(0, plural_ru_1.default)(minLength, "символ", "символа", "символов")}.`;
|
|
17
|
+
return {
|
|
18
|
+
value: minLength,
|
|
19
|
+
message: message || formattedMessage,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
exports.getMinLengthRule = getMinLengthRule;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns an error if the number is less than the specified value.
|
|
3
|
+
* @param min - Minimum number.
|
|
4
|
+
* @param message - Custom message.
|
|
5
|
+
*
|
|
6
|
+
* @example rules: { min: getMinRule(20) }
|
|
7
|
+
*/
|
|
8
|
+
export declare const getMinRule: (min: number, message?: string) => {
|
|
9
|
+
value: number;
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMinRule = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Returns an error if the number is less than the specified value.
|
|
6
|
+
* @param min - Minimum number.
|
|
7
|
+
* @param message - Custom message.
|
|
8
|
+
*
|
|
9
|
+
* @example rules: { min: getMinRule(20) }
|
|
10
|
+
*/
|
|
11
|
+
const getMinRule = (min, message) => ({
|
|
12
|
+
value: min,
|
|
13
|
+
message: message || `Minimum value - ${min}.`,
|
|
14
|
+
});
|
|
15
|
+
exports.getMinRule = getMinRule;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates a required field.
|
|
3
|
+
* @param isRequired - Dynamic parameter that indicates whether the field is required.
|
|
4
|
+
* @param message - Custom message.
|
|
5
|
+
*
|
|
6
|
+
* @example rules: { required: getRequiredRule() }
|
|
7
|
+
*/
|
|
8
|
+
export declare const getRequiredRule: (isRequired?: boolean, message?: string) => {
|
|
9
|
+
value: boolean;
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequiredRule = void 0;
|
|
4
|
+
const REQUIRED_MESSAGE_TEXT = 'Это поле обязательное.';
|
|
5
|
+
/**
|
|
6
|
+
* Validates a required field.
|
|
7
|
+
* @param isRequired - Dynamic parameter that indicates whether the field is required.
|
|
8
|
+
* @param message - Custom message.
|
|
9
|
+
*
|
|
10
|
+
* @example rules: { required: getRequiredRule() }
|
|
11
|
+
*/
|
|
12
|
+
const getRequiredRule = (isRequired = true, message = REQUIRED_MESSAGE_TEXT) => ({
|
|
13
|
+
value: isRequired,
|
|
14
|
+
message,
|
|
15
|
+
});
|
|
16
|
+
exports.getRequiredRule = getRequiredRule;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface EmailRule {
|
|
2
|
+
pattern?: RegExp;
|
|
3
|
+
message?: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Validates an email address.
|
|
7
|
+
* @param options - Parameters.
|
|
8
|
+
* @param options.pattern - Regular expression.
|
|
9
|
+
* @param options.message - Custom message.
|
|
10
|
+
*
|
|
11
|
+
* @example rules: { pattern: getEmailRule() }
|
|
12
|
+
*/
|
|
13
|
+
export declare const getEmailRule: ({ pattern, message, }: EmailRule) => {
|
|
14
|
+
value: RegExp;
|
|
15
|
+
message: string;
|
|
16
|
+
};
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEmailRule = void 0;
|
|
4
|
+
const DEFAULT_PATTERN = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
|
|
5
|
+
const DEFAULT_MESSAGE = "Please check the correctness of the entered email address.";
|
|
6
|
+
/**
|
|
7
|
+
* Validates an email address.
|
|
8
|
+
* @param options - Parameters.
|
|
9
|
+
* @param options.pattern - Regular expression.
|
|
10
|
+
* @param options.message - Custom message.
|
|
11
|
+
*
|
|
12
|
+
* @example rules: { pattern: getEmailRule() }
|
|
13
|
+
*/
|
|
14
|
+
const getEmailRule = ({ pattern = DEFAULT_PATTERN, message = DEFAULT_MESSAGE, }) => {
|
|
15
|
+
return {
|
|
16
|
+
value: pattern,
|
|
17
|
+
message,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
exports.getEmailRule = getEmailRule;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface UrlRule {
|
|
2
|
+
message?: string;
|
|
3
|
+
pattern?: RegExp;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Returns a rule that validates the input URL.
|
|
7
|
+
* @param options - Parameters.
|
|
8
|
+
* @param options.pattern - Regular expression.
|
|
9
|
+
* @param options.message - Custom message.
|
|
10
|
+
*
|
|
11
|
+
* @example rules: { pattern: getUrlValidationRule() }
|
|
12
|
+
*/
|
|
13
|
+
export declare const getUrlRule: ({ pattern, message, }: UrlRule) => {
|
|
14
|
+
message: string;
|
|
15
|
+
value: RegExp;
|
|
16
|
+
};
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getUrlRule = void 0;
|
|
4
|
+
const DEFAULT_MESSAGE = "The link does not match the format.";
|
|
5
|
+
const DEFAULT_PATTERN = /(http(s)?):\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;
|
|
6
|
+
/**
|
|
7
|
+
* Returns a rule that validates the input URL.
|
|
8
|
+
* @param options - Parameters.
|
|
9
|
+
* @param options.pattern - Regular expression.
|
|
10
|
+
* @param options.message - Custom message.
|
|
11
|
+
*
|
|
12
|
+
* @example rules: { pattern: getUrlValidationRule() }
|
|
13
|
+
*/
|
|
14
|
+
const getUrlRule = ({ pattern = DEFAULT_PATTERN, message = DEFAULT_MESSAGE, }) => {
|
|
15
|
+
return {
|
|
16
|
+
message,
|
|
17
|
+
value: pattern,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
exports.getUrlRule = getUrlRule;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates an object, checking if it contains values.
|
|
3
|
+
* @param obj - Object with values.
|
|
4
|
+
* @param message - Custom message.
|
|
5
|
+
*
|
|
6
|
+
* @example rules: { validate: (obj) => getRequiredObjectRule(obj) }
|
|
7
|
+
*/
|
|
8
|
+
export declare const getRequiredObjectRule: (obj?: Record<string, unknown>, message?: string) => string | true;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequiredObjectRule = void 0;
|
|
4
|
+
const REQUIRED_MESSAGE_TEXT = 'Это поле обязательное.';
|
|
5
|
+
/**
|
|
6
|
+
* Validates an object, checking if it contains values.
|
|
7
|
+
* @param obj - Object with values.
|
|
8
|
+
* @param message - Custom message.
|
|
9
|
+
*
|
|
10
|
+
* @example rules: { validate: (obj) => getRequiredObjectRule(obj) }
|
|
11
|
+
*/
|
|
12
|
+
const getRequiredObjectRule = (obj = {}, message) => {
|
|
13
|
+
var _a;
|
|
14
|
+
const formattedMessage = message || REQUIRED_MESSAGE_TEXT;
|
|
15
|
+
if (!obj) {
|
|
16
|
+
return false || formattedMessage;
|
|
17
|
+
}
|
|
18
|
+
return ((_a = Object === null || Object === void 0 ? void 0 : Object.keys(obj)) === null || _a === void 0 ? void 0 : _a.length) > 0 || formattedMessage;
|
|
19
|
+
};
|
|
20
|
+
exports.getRequiredObjectRule = getRequiredObjectRule;
|
package/index.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
+
import { getEmailRule } from "./rules/link/get-email-rule";
|
|
2
|
+
import { getUrlRule } from "./rules/link/get-url-rule";
|
|
1
3
|
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
import {
|
|
4
|
+
import { getMaxArrayLengthRule } from "./rules/array/get-max-array-length";
|
|
5
|
+
import { getMinArrayLengthRule } from "./rules/array/get-min-array-length";
|
|
6
|
+
import { getRequiredArrayRule } from "./rules/array/get-required-array-rule";
|
|
7
|
+
|
|
8
|
+
import { getMaxLengthRule } from "./rules/default/get-max-length-rule";
|
|
9
|
+
import { getMaxRule } from "./rules/default/get-max-rule";
|
|
10
|
+
import { getMinLengthRule } from "./rules/default/get-min-length-rule";
|
|
11
|
+
import { getMinRule } from "./rules/default/get-min-rule";
|
|
12
|
+
import { getRequiredRule } from "./rules/default/get-require-rule";
|
|
13
|
+
|
|
14
|
+
import { getRequiredObjectRule } from "./rules/object/get-required-object-rule";
|
|
13
15
|
|
|
14
16
|
export {
|
|
15
17
|
getMaxRule,
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-hook-form-rules",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
9
|
"build": "tsc --declaration",
|
|
10
|
-
"
|
|
10
|
+
"deploy": "npm run build && npm publish"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [],
|
|
13
13
|
"author": "",
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates an array for the maximum number of values.
|
|
3
|
+
* @param arr - Array of values.
|
|
4
|
+
* @param maxLength - Maximum number of values.
|
|
5
|
+
* @param message - Custom message.
|
|
6
|
+
*
|
|
7
|
+
* @example getMaxArrayLengthRule(2, "Maximum number of values is 2").
|
|
8
|
+
*/
|
|
9
|
+
export const getMaxArrayLengthRule = (
|
|
10
|
+
arr: unknown[],
|
|
11
|
+
maxLength: number,
|
|
12
|
+
message?: string,
|
|
13
|
+
) => {
|
|
14
|
+
const defaultMessage = `Maximum number of values - ${maxLength}.`;
|
|
15
|
+
|
|
16
|
+
const formattedMessage = message || defaultMessage;
|
|
17
|
+
|
|
18
|
+
return arr.length < maxLength || formattedMessage;
|
|
19
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates an array for the maximum number of values.
|
|
3
|
+
* @param arr - Array of values.
|
|
4
|
+
* @param maxLength - Maximum number of values.
|
|
5
|
+
* @param message - Custom message.
|
|
6
|
+
*
|
|
7
|
+
* @example getMaxArrayLengthRule(3, "Maximum number of values is 2").
|
|
8
|
+
*/
|
|
9
|
+
export const getMinArrayLengthRule = (
|
|
10
|
+
arr: unknown[],
|
|
11
|
+
minLength: number,
|
|
12
|
+
message?: string,
|
|
13
|
+
) => {
|
|
14
|
+
const defaultMessage = `Minimum number of values - ${minLength}.`;
|
|
15
|
+
|
|
16
|
+
const formattedMessage = message || defaultMessage;
|
|
17
|
+
|
|
18
|
+
return arr.length > minLength || formattedMessage;
|
|
19
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const REQUIRED_MESSAGE_TEXT = 'Это поле обязательное.';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validates an array, checking if it contains values.
|
|
5
|
+
* @param arr - Array of values.
|
|
6
|
+
* @param message - Custom message.
|
|
7
|
+
*
|
|
8
|
+
* @example rules: { validate: (arr) => getRequiredArrayRule(arr) }
|
|
9
|
+
*/
|
|
10
|
+
export const getRequiredArrayRule = (arr: unknown[] = [], message?: string) => {
|
|
11
|
+
return arr.length > 0 || (message || REQUIRED_MESSAGE_TEXT);
|
|
12
|
+
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import plural from "plural-ru";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @param maxLength -
|
|
6
|
-
* @param
|
|
4
|
+
* Validates the maximum length of a string.
|
|
5
|
+
* @param maxLength - Maximum number of characters allowed.
|
|
6
|
+
* @param message - Custom message.
|
|
7
7
|
*
|
|
8
|
-
* @example rules: { maxLength: getMaxLengthRule(1000)}
|
|
8
|
+
* @example rules: { maxLength: getMaxLengthRule(1000) }
|
|
9
9
|
*/
|
|
10
10
|
export const getMaxLengthRule = (maxLength: number, message?: string) => {
|
|
11
11
|
const formattedMessage = `Максимальная длина ${maxLength} ${plural(
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns an error if the number exceeds the specified value.
|
|
3
|
+
* @param max - Maximum number.
|
|
4
|
+
* @param message - Custom message.
|
|
5
|
+
*
|
|
6
|
+
* @example rules: { max: getMaxRule(20) }
|
|
7
|
+
*/
|
|
8
|
+
export const getMaxRule = (max: number, message?: string) => ({
|
|
9
|
+
value: max,
|
|
10
|
+
message: message || `Maximum value - ${max}.`,
|
|
11
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import plural from "plural-ru";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a rule that validates the minimum length of a string.
|
|
5
|
+
* @param minLength - Minimum number of characters allowed.
|
|
6
|
+
* @param message - Custom message.
|
|
7
|
+
*
|
|
8
|
+
* @example rules: { minLength: getMinLengthRule(20) }
|
|
9
|
+
*/
|
|
10
|
+
export const getMinLengthRule = (minLength: number, message?: string) => {
|
|
11
|
+
const formattedMessage = `Минимальная длина ${minLength} ${plural(
|
|
12
|
+
minLength,
|
|
13
|
+
"символ",
|
|
14
|
+
"символа",
|
|
15
|
+
"символов",
|
|
16
|
+
)}.`;
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
value: minLength,
|
|
20
|
+
message: message || formattedMessage,
|
|
21
|
+
};
|
|
22
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns an error if the number is less than the specified value.
|
|
3
|
+
* @param min - Minimum number.
|
|
4
|
+
* @param message - Custom message.
|
|
5
|
+
*
|
|
6
|
+
* @example rules: { min: getMinRule(20) }
|
|
7
|
+
*/
|
|
8
|
+
export const getMinRule = (min: number, message?: string) => ({
|
|
9
|
+
value: min,
|
|
10
|
+
message: message || `Minimum value - ${min}.`,
|
|
11
|
+
})
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const REQUIRED_MESSAGE_TEXT = 'Это поле обязательное.';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validates a required field.
|
|
5
|
+
* @param isRequired - Dynamic parameter that indicates whether the field is required.
|
|
6
|
+
* @param message - Custom message.
|
|
7
|
+
*
|
|
8
|
+
* @example rules: { required: getRequiredRule() }
|
|
9
|
+
*/
|
|
10
|
+
export const getRequiredRule = (
|
|
11
|
+
isRequired = true,
|
|
12
|
+
message = REQUIRED_MESSAGE_TEXT
|
|
13
|
+
) => ({
|
|
14
|
+
value: isRequired,
|
|
15
|
+
message,
|
|
16
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const DEFAULT_PATTERN = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
|
|
2
|
+
const DEFAULT_MESSAGE =
|
|
3
|
+
"Please check the correctness of the entered email address.";
|
|
4
|
+
|
|
5
|
+
interface EmailRule {
|
|
6
|
+
pattern?: RegExp;
|
|
7
|
+
message?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Validates an email address.
|
|
12
|
+
* @param options - Parameters.
|
|
13
|
+
* @param options.pattern - Regular expression.
|
|
14
|
+
* @param options.message - Custom message.
|
|
15
|
+
*
|
|
16
|
+
* @example rules: { pattern: getEmailRule() }
|
|
17
|
+
*/
|
|
18
|
+
export const getEmailRule = ({
|
|
19
|
+
pattern = DEFAULT_PATTERN,
|
|
20
|
+
message = DEFAULT_MESSAGE,
|
|
21
|
+
}: EmailRule) => {
|
|
22
|
+
return {
|
|
23
|
+
value: pattern,
|
|
24
|
+
message,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const DEFAULT_MESSAGE = "
|
|
1
|
+
const DEFAULT_MESSAGE = "The link does not match the format."
|
|
2
2
|
const DEFAULT_PATTERN = /(http(s)?):\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/
|
|
3
3
|
|
|
4
4
|
interface UrlRule {
|
|
@@ -7,10 +7,10 @@ interface UrlRule {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
* @param options -
|
|
12
|
-
* @param options.pattern -
|
|
13
|
-
* @param options.message -
|
|
10
|
+
* Returns a rule that validates the input URL.
|
|
11
|
+
* @param options - Parameters.
|
|
12
|
+
* @param options.pattern - Regular expression.
|
|
13
|
+
* @param options.message - Custom message.
|
|
14
14
|
*
|
|
15
15
|
* @example rules: { pattern: getUrlValidationRule() }
|
|
16
16
|
*/
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
const REQUIRED_MESSAGE_TEXT = 'Это поле обязательное.';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @param obj -
|
|
6
|
-
* @param message -
|
|
4
|
+
* Validates an object, checking if it contains values.
|
|
5
|
+
* @param obj - Object with values.
|
|
6
|
+
* @param message - Custom message.
|
|
7
7
|
*
|
|
8
8
|
* @example rules: { validate: (obj) => getRequiredObjectRule(obj) }
|
|
9
9
|
*/
|
package/rules/get-email-rule.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const DEFAULT_PATTERN = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
|
|
2
|
-
const DEFAULT_MESSAGE = "Пожалуйста, проверьте правильность введенного адреса электронной почты."
|
|
3
|
-
|
|
4
|
-
interface EmailRule {
|
|
5
|
-
pattern?: RegExp
|
|
6
|
-
message?: string
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Валидирует адресс электронной почты.
|
|
11
|
-
* @param options - Параметры.
|
|
12
|
-
* @param options.pattern - Регулярное выражение.
|
|
13
|
-
* @param options.message - Кастомное сообщение.
|
|
14
|
-
*
|
|
15
|
-
* @example rules: { pattern: getEmailRule() }
|
|
16
|
-
*/
|
|
17
|
-
export const getEmailRule = ({
|
|
18
|
-
pattern = DEFAULT_PATTERN,
|
|
19
|
-
message = DEFAULT_MESSAGE,
|
|
20
|
-
}: EmailRule) => {
|
|
21
|
-
return {
|
|
22
|
-
value: pattern,
|
|
23
|
-
message,
|
|
24
|
-
}
|
|
25
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Валидирует массив, на максимальное кол-во значений
|
|
3
|
-
* @param arr - Массиов со значениями.
|
|
4
|
-
* @param maxLength - Максимальное количество значений.
|
|
5
|
-
* @param message - Кастомное сообщение.
|
|
6
|
-
* @example getMaxArrayLengthRule(2, "Максимальное кол-во значений - 2").
|
|
7
|
-
*/
|
|
8
|
-
export const getMaxArrayLengthRule = (
|
|
9
|
-
arr: unknown[],
|
|
10
|
-
maxLength: number,
|
|
11
|
-
message?: string,
|
|
12
|
-
) => {
|
|
13
|
-
const defaultMessage = `Максимальное количество значений - ${maxLength}.`;
|
|
14
|
-
|
|
15
|
-
const formattedMessage = message || defaultMessage;
|
|
16
|
-
|
|
17
|
-
return arr.length < maxLength || formattedMessage;
|
|
18
|
-
};
|
package/rules/get-max-rule.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Возвращает ошибку, если число, больше указанного.
|
|
3
|
-
* @param max - Максимальное число
|
|
4
|
-
* @param message - Кастомное сообщение.
|
|
5
|
-
*
|
|
6
|
-
* @example rules: { max: getMaxRule(20) }
|
|
7
|
-
*/
|
|
8
|
-
export const getMaxRule = (max: number, message?: string) => ({
|
|
9
|
-
value: max,
|
|
10
|
-
message: message || `Максимальное значение ${max}.`,
|
|
11
|
-
})
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Валидирует массив, на максимальное кол-во значений
|
|
3
|
-
* @param arr - Массиов со значениями.
|
|
4
|
-
* @param minLength - Максимальное количество значений.
|
|
5
|
-
* @param message - Кастомное сообщение.
|
|
6
|
-
* @example getMinArrayLengthRule(3, "Максимальное кол-во значений - 2").
|
|
7
|
-
*/
|
|
8
|
-
export const getMinArrayLengthRule = (
|
|
9
|
-
arr: unknown[],
|
|
10
|
-
minLength: number,
|
|
11
|
-
message?: string,
|
|
12
|
-
) => {
|
|
13
|
-
const defaultMessage = `Минимальное количество значений - ${minLength}.`;
|
|
14
|
-
|
|
15
|
-
const formattedMessage = message || defaultMessage;
|
|
16
|
-
|
|
17
|
-
return arr.length > minLength || formattedMessage;
|
|
18
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import plural from 'plural-ru'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Возвращает правило, которое валидирует минимальную длину строки
|
|
5
|
-
* @param maxLength - Минимальное количество символов для ввода
|
|
6
|
-
* @param message - Кастомное сообщение.
|
|
7
|
-
*
|
|
8
|
-
* @example rules: { minLength: getMinLengthRule(20) }
|
|
9
|
-
*/
|
|
10
|
-
export const getMinLengthRule = (minLength: number, message?: string) => {
|
|
11
|
-
const formattedMessage = `Минимальная длина ${minLength} ${plural(
|
|
12
|
-
minLength,
|
|
13
|
-
'символ',
|
|
14
|
-
'символа',
|
|
15
|
-
'символов',
|
|
16
|
-
)}.`
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
value: minLength,
|
|
20
|
-
message: message || formattedMessage,
|
|
21
|
-
};
|
|
22
|
-
};
|
package/rules/get-min-rule.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export interface MinRuleProps {
|
|
2
|
-
min: number
|
|
3
|
-
message?: string
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Возвращает ошибку, если число, меньше указанного.
|
|
8
|
-
* @param options - Параметры.
|
|
9
|
-
* @param options.max - Минимальное число
|
|
10
|
-
* @param options.message - Кастомное сообщение.
|
|
11
|
-
*
|
|
12
|
-
* @example rules: { max: getMinRule(20) }
|
|
13
|
-
*/
|
|
14
|
-
export const getMinRule = ({
|
|
15
|
-
min,
|
|
16
|
-
message,
|
|
17
|
-
}: MinRuleProps) => ({
|
|
18
|
-
value: min,
|
|
19
|
-
message: message || `Минимальное значение ${min}.`,
|
|
20
|
-
})
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
const REQUIRED_MESSAGE_TEXT = 'Это поле обязательное.';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Валидирует обязательное поле.
|
|
5
|
-
* @param isRequired - Динимический параметр, которые отвечает за то, что поле является обязательным.
|
|
6
|
-
* @param message - Кастомное сообщение.
|
|
7
|
-
*
|
|
8
|
-
* @example rules: { required: getRequiredRule() }
|
|
9
|
-
*/
|
|
10
|
-
export const getRequiredRule = (
|
|
11
|
-
isRequired = true,
|
|
12
|
-
message = REQUIRED_MESSAGE_TEXT
|
|
13
|
-
) => ({
|
|
14
|
-
value: isRequired,
|
|
15
|
-
message,
|
|
16
|
-
})
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
const REQUIRED_MESSAGE_TEXT = 'Это поле обязательное.';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Валидирует массив, проверяет есть ли в нем значения
|
|
5
|
-
* @param arr - Массиов со значениями
|
|
6
|
-
* @param message - Кастомное сообщение.
|
|
7
|
-
*
|
|
8
|
-
* @example rules: { validate: (arr) => getRequiredArrrayRule(arr) }
|
|
9
|
-
*/
|
|
10
|
-
export const getRequiredArrayRule = (arr: unknown[] = [], message?: string) => {
|
|
11
|
-
return arr.length > 0 || (message || REQUIRED_MESSAGE_TEXT);
|
|
12
|
-
};
|