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