@coxy/react-validator 5.0.0 → 5.0.1
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 +12 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -71,41 +71,17 @@ Create your own rules easily, or use the built-in ones:
|
|
|
71
71
|
|
|
72
72
|
```javascript
|
|
73
73
|
const rules = {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
password: [
|
|
79
|
-
{ rule: value => value.length >= 6, message: 'Password must be at least 6 characters' },
|
|
80
|
-
],
|
|
81
|
-
notEmpty: [
|
|
82
|
-
{ rule: value => !!value && value.trim() !== '', message: 'Field cannot be empty' },
|
|
83
|
-
],
|
|
84
|
-
boolean: [
|
|
85
|
-
{ rule: value => typeof value === 'boolean', message: 'Must be a boolean value' },
|
|
86
|
-
],
|
|
87
|
-
min: (min) => ([
|
|
88
|
-
{ rule: value => Number(value) >= min, message: `Must be at least ${min}` },
|
|
89
|
-
]),
|
|
90
|
-
max: (max) => ([
|
|
91
|
-
{ rule: value => Number(value) <= max, message: `Must be at most ${max}` },
|
|
92
|
-
]),
|
|
93
|
-
length: (min, max) => ([
|
|
94
|
-
{ rule: value => value.length >= min, message: `Must be at least ${min} characters` },
|
|
95
|
-
...(max ? [{ rule: value => value.length <= max, message: `Must be at most ${max} characters` }] : [])
|
|
96
|
-
]),
|
|
97
|
-
};
|
|
74
|
+
notEmpty: [z.string().min(1, { error: 'Field is required' })],
|
|
75
|
+
isTrue: [z.boolean({ error: 'Value is required' }).and(z.literal(true))],
|
|
76
|
+
email: [z.string().min(1, { error: 'Email is required' }), z.email({ message: 'Email is invalid' })],
|
|
77
|
+
}
|
|
98
78
|
```
|
|
99
79
|
|
|
100
|
-
| Name
|
|
101
|
-
|
|
102
|
-
| email
|
|
103
|
-
|
|
|
104
|
-
|
|
|
105
|
-
| boolean | Array | Ensure a boolean value is present. |
|
|
106
|
-
| min(n) | Function | Validate number is >= `n`. |
|
|
107
|
-
| max(n) | Function | Validate number is <= `n`. |
|
|
108
|
-
| length(a,b) | Function | Validate string length is between `a` and `b` (optional). |
|
|
80
|
+
| Name | Type | Description |
|
|
81
|
+
|----------|-------|--------------------------------------------|
|
|
82
|
+
| email | Array | Validate non-empty email with regex check. |
|
|
83
|
+
| notEmpty | Array | Check if a string is not empty. |
|
|
84
|
+
| isTrue | Array | Ensure a boolean value is present. |
|
|
109
85
|
|
|
110
86
|
---
|
|
111
87
|
|
|
@@ -160,7 +136,7 @@ Use it server-side or in custom flows.
|
|
|
160
136
|
const validator = new Validator({ stopAtFirstError: true });
|
|
161
137
|
|
|
162
138
|
const field = validator.addField({
|
|
163
|
-
rules: rules.
|
|
139
|
+
rules: rules.email,
|
|
164
140
|
value: '12345',
|
|
165
141
|
});
|
|
166
142
|
```
|
|
@@ -200,8 +176,8 @@ validator.addField({
|
|
|
200
176
|
|
|
201
177
|
validator.addField({
|
|
202
178
|
id: 'password',
|
|
203
|
-
rules: rules.
|
|
204
|
-
value:
|
|
179
|
+
rules: rules.isTrue,
|
|
180
|
+
value: true,
|
|
205
181
|
});
|
|
206
182
|
|
|
207
183
|
const result = validator.validate();
|
package/package.json
CHANGED