You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`react-class-validator` easily integrates with [Formik](https://formik.org/). You can simply use the `validate`
120
-
function returned from `useValidation`, so long as the Formik fields are named the same as the keys in your validation
121
-
class. Individual fields will have to be validated with `onBlur` functionality.
86
+
### `useValidation(validationClass)`
122
87
123
-
### Formik error messages
88
+
Returns an object with:
124
89
125
-
To display error messages without custom handling, messages will need to be outputted as a map upon validation.
126
-
Do this by overriding the default `resultType` (you can also do this at the component-level).
90
+
| Property | Type | Description |
91
+
|------------|------|-------------|
92
+
|`validate`|`(payload, filter?) => Promise<errors \| undefined>`| Validates the payload against the class. Returns `undefined` when valid. |
93
+
|`errors`|`{ [key]: string[] } \| undefined`| Current validation errors keyed by property name. Each value is an array of error messages. |
127
94
128
-
```typescript
129
-
const options:ValidatorContextOptions= {
130
-
resultType: 'map'
131
-
};
132
-
```
95
+
**Parameters:**
96
+
97
+
-`payload` — an object with the fields to validate (keys should match the validation class properties)
98
+
-`filter` (optional) — an array of property names to exclude from the error result, useful for single-field validation on blur
99
+
100
+
## Usage with Formik
101
+
102
+
The `validate` function can be passed directly to Formik's `validate` prop. Since it returns an error map keyed by property name (or `undefined` when valid), it integrates seamlessly.
133
103
134
-
Then you can simply integrate with the default Formik flow.
0 commit comments