react-hook-form-rules 1.0.9 → 1.0.11
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 +27 -29
- 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/constants.d.ts +1 -0
- package/dist/rules/constants.js +4 -0
- package/dist/rules/default/get-max-length-rule.d.ts +11 -0
- package/dist/rules/default/get-max-length-rule.js +18 -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 +18 -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/constants.ts +1 -0
- package/rules/default/get-max-length-rule.ts +15 -0
- package/rules/default/get-max-rule.ts +11 -0
- package/rules/default/get-min-length-rule.ts +15 -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} +5 -5
- package/rules/get-email-rule.ts +0 -25
- package/rules/get-max-array-length.ts +0 -18
- package/rules/get-max-length-rule.ts +0 -22
- 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/rules/helpers/get-min-error.ts +0 -9
- package/rules/helpers/get-min-length-error.ts +0 -9
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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
4
|
|
|
@@ -20,8 +20,8 @@ This rule makes the field required.
|
|
|
20
20
|
#### Options
|
|
21
21
|
| Option | Type | Default | Description |
|
|
22
22
|
| -------- | --------- | ------ | ---------- |
|
|
23
|
-
| isRequired | boolean | true |
|
|
24
|
-
| message | string | "This field is required." |
|
|
23
|
+
| isRequired | boolean | true | Dynamic parameter that indicates whether the field is required. |
|
|
24
|
+
| message | string | "This field is required." | Custom message. |
|
|
25
25
|
|
|
26
26
|
#### Code example
|
|
27
27
|
```
|
|
@@ -48,8 +48,8 @@ This rule makes the array field required.
|
|
|
48
48
|
#### Options
|
|
49
49
|
| Option | Type | Default | Description |
|
|
50
50
|
| -------- | --------- | ------ | ---------- |
|
|
51
|
-
| arr | array |
|
|
52
|
-
| message | string | "This field is required." |
|
|
51
|
+
| arr | array | - | Array of values. |
|
|
52
|
+
| message | string | "This field is required." | Custom message. |
|
|
53
53
|
|
|
54
54
|
|
|
55
55
|
#### Code example
|
|
@@ -76,8 +76,8 @@ This rule makes the object field required.
|
|
|
76
76
|
#### Options
|
|
77
77
|
| Option | Type | Default | Description |
|
|
78
78
|
| -------- | --------- | ------ | ---------- |
|
|
79
|
-
| obj | object |
|
|
80
|
-
| message | string | "This field is required." |
|
|
79
|
+
| obj | object | - | Object with values. |
|
|
80
|
+
| message | string | "This field is required." | Custom message. |
|
|
81
81
|
|
|
82
82
|
|
|
83
83
|
#### Code example
|
|
@@ -102,8 +102,8 @@ This rule returns an error if the number is greater than the specified value.
|
|
|
102
102
|
#### Options
|
|
103
103
|
| Option | Type | Default | Description |
|
|
104
104
|
| -------- | --------- | ------ | ---------- |
|
|
105
|
-
| max | number | - |
|
|
106
|
-
| message | string | - |
|
|
105
|
+
| max | number | - | Maximum number. |
|
|
106
|
+
| message | string | - | Custom message. |
|
|
107
107
|
|
|
108
108
|
#### Code example
|
|
109
109
|
```
|
|
@@ -127,8 +127,8 @@ This rule checks that the number of characters in the string does not exceed the
|
|
|
127
127
|
#### Options
|
|
128
128
|
| Option | Type | Default | Description |
|
|
129
129
|
| -------- | --------- | ------ | ---------- |
|
|
130
|
-
| maxLength | number | - |
|
|
131
|
-
| message | string | - |
|
|
130
|
+
| maxLength | number | - | Maximum number of characters allowed. |
|
|
131
|
+
| message | string | - | Custom message. |
|
|
132
132
|
|
|
133
133
|
### Code example
|
|
134
134
|
```
|
|
@@ -153,9 +153,9 @@ This rule returns an error if the number of values in the array exceeds the spec
|
|
|
153
153
|
#### Options
|
|
154
154
|
| Option | Type | Default | Description |
|
|
155
155
|
| -------- | --------- | ------ | ---------- |
|
|
156
|
-
| arr | arrray | - |
|
|
157
|
-
| maxLength | number | - |
|
|
158
|
-
| message | string | - |
|
|
156
|
+
| arr | arrray | - | Array of values. |
|
|
157
|
+
| maxLength | number | - | Maximum number of values. |
|
|
158
|
+
| message | string | - | Custom message |
|
|
159
159
|
|
|
160
160
|
#### Code example
|
|
161
161
|
```
|
|
@@ -178,8 +178,8 @@ This rule checks that the string contains a minimum number of characters.
|
|
|
178
178
|
#### Options
|
|
179
179
|
| Option | Type | Default | Description |
|
|
180
180
|
| -------- | --------- | ------ | ---------- |
|
|
181
|
-
| min | number | - |
|
|
182
|
-
| message | string | - |
|
|
181
|
+
| min | number | - | Minimum number. |
|
|
182
|
+
| message | string | - | Custom message. |
|
|
183
183
|
|
|
184
184
|
#### Code example
|
|
185
185
|
```
|
|
@@ -203,8 +203,8 @@ The rule checks that the string contains at least the specified number of charac
|
|
|
203
203
|
#### Options
|
|
204
204
|
| Option | Type | Default | Description |
|
|
205
205
|
| -------- | --------- | ------ | ---------- |
|
|
206
|
-
| minLength | number | - |
|
|
207
|
-
| message | string | - |
|
|
206
|
+
| minLength | number | - | Minimum number of characters allowed. |
|
|
207
|
+
| message | string | - | Custom message |
|
|
208
208
|
|
|
209
209
|
#### Code example
|
|
210
210
|
```
|
|
@@ -228,9 +228,9 @@ The rule checks that the array contains a minimum number of elements.
|
|
|
228
228
|
#### Options
|
|
229
229
|
| Option | Type | Default | Description |
|
|
230
230
|
| -------- | --------- | ------ | ---------- |
|
|
231
|
-
| arr | arrray | - |
|
|
232
|
-
| minLength | number | - |
|
|
233
|
-
| message | string | - |
|
|
231
|
+
| arr | arrray | - | Array of values. |
|
|
232
|
+
| minLength | number | - | Maximum number of values. |
|
|
233
|
+
| message | string | - | Custom message |
|
|
234
234
|
|
|
235
235
|
#### Code example
|
|
236
236
|
```
|
|
@@ -251,8 +251,9 @@ he rule checks the entered email for correctness.
|
|
|
251
251
|
#### Options
|
|
252
252
|
| Option | Type | Default | Description |
|
|
253
253
|
| -------- | --------- | ------ | ---------- |
|
|
254
|
-
|
|
|
255
|
-
|
|
|
254
|
+
| props | object | - | Object with values |
|
|
255
|
+
| props.pattern | RegExp | - | Regular expression. |
|
|
256
|
+
| props.message | string | - | Custom message |
|
|
256
257
|
|
|
257
258
|
#### Code example
|
|
258
259
|
```
|
|
@@ -276,8 +277,9 @@ The rule checks the entered URL for correctness.
|
|
|
276
277
|
#### Options
|
|
277
278
|
| Option | Type | Default | Description |
|
|
278
279
|
| -------- | --------- | ------ | ---------- |
|
|
279
|
-
|
|
|
280
|
-
|
|
|
280
|
+
| props | object | - | Object with values |
|
|
281
|
+
| props.pattern | RegExp | - | Regular expression. |
|
|
282
|
+
| props.message | string | - | Custom message |
|
|
281
283
|
|
|
282
284
|
#### Code example
|
|
283
285
|
```
|
|
@@ -289,7 +291,3 @@ const {...} = useController({
|
|
|
289
291
|
}}
|
|
290
292
|
})
|
|
291
293
|
```
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
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 constants_1 = require("../constants");
|
|
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 || constants_1.REQUIRED_MESSAGE_TEXT);
|
|
14
|
+
};
|
|
15
|
+
exports.getRequiredArrayRule = getRequiredArrayRule;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const REQUIRED_MESSAGE_TEXT = "This field is required.";
|
|
@@ -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,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMaxLengthRule = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Validates the maximum length of a string.
|
|
6
|
+
* @param maxLength - Maximum number of characters allowed.
|
|
7
|
+
* @param message - Custom message.
|
|
8
|
+
*
|
|
9
|
+
* @example rules: { maxLength: getMaxLengthRule(1000) }
|
|
10
|
+
*/
|
|
11
|
+
const getMaxLengthRule = (maxLength, message) => {
|
|
12
|
+
const formattedMessage = `Maximum length - ${maxLength}`;
|
|
13
|
+
return {
|
|
14
|
+
value: maxLength,
|
|
15
|
+
message: message || formattedMessage,
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
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,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMinLengthRule = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Returns a rule that validates the minimum length of a string.
|
|
6
|
+
* @param minLength - Minimum number of characters allowed.
|
|
7
|
+
* @param message - Custom message.
|
|
8
|
+
*
|
|
9
|
+
* @example rules: { minLength: getMinLengthRule(20) }
|
|
10
|
+
*/
|
|
11
|
+
const getMinLengthRule = (minLength, message) => {
|
|
12
|
+
const formattedMessage = `Minimum length - ${minLength}`;
|
|
13
|
+
return {
|
|
14
|
+
value: minLength,
|
|
15
|
+
message: message || formattedMessage,
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
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 constants_1 = require("../constants");
|
|
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 = constants_1.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 constants_1 = require("../constants");
|
|
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 || constants_1.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.11",
|
|
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
|
+
import { REQUIRED_MESSAGE_TEXT } from "../constants";
|
|
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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const REQUIRED_MESSAGE_TEXT = 'This field is required.';
|
|
@@ -0,0 +1,15 @@
|
|
|
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 const getMaxLengthRule = (maxLength: number, message?: string) => {
|
|
9
|
+
const formattedMessage = `Maximum length - ${maxLength}`;
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
value: maxLength,
|
|
13
|
+
message: message || formattedMessage,
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -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,15 @@
|
|
|
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 const getMinLengthRule = (minLength: number, message?: string) => {
|
|
9
|
+
const formattedMessage = `Minimum length - ${minLength}`;
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
value: minLength,
|
|
13
|
+
message: message || formattedMessage,
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -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
|
+
import { REQUIRED_MESSAGE_TEXT } from "../constants";
|
|
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,13 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import { REQUIRED_MESSAGE_TEXT } from "../constants";
|
|
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
|
*/
|
|
10
|
-
export const getRequiredObjectRule = (obj: Record<string, unknown
|
|
10
|
+
export const getRequiredObjectRule = (obj: Record<string, unknown>, message?: string) => {
|
|
11
11
|
const formattedMessage = message || REQUIRED_MESSAGE_TEXT;
|
|
12
12
|
|
|
13
13
|
if (!obj) {
|
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
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import plural from "plural-ru";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Валидирует максимальную длину строки
|
|
5
|
-
* @param maxLength - Максимальное количество символов для ввода
|
|
6
|
-
* @param mrssage - Кастомное сообщение.
|
|
7
|
-
*
|
|
8
|
-
* @example rules: { maxLength: getMaxLengthRule(1000)}
|
|
9
|
-
*/
|
|
10
|
-
export const getMaxLengthRule = (maxLength: number, message?: string) => {
|
|
11
|
-
const formattedMessage = `Максимальная длина ${maxLength} ${plural(
|
|
12
|
-
maxLength,
|
|
13
|
-
"символ",
|
|
14
|
-
"символа",
|
|
15
|
-
"символов",
|
|
16
|
-
)}.`;
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
value: maxLength,
|
|
20
|
-
message: message || formattedMessage,
|
|
21
|
-
};
|
|
22
|
-
};
|
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
|
-
};
|