https://github.com/mantinedev/mantine-form-yup-resolver
Yup schema resolver for @mantine/form
https://github.com/mantinedev/mantine-form-yup-resolver
Last synced: 26 days ago
JSON representation
Yup schema resolver for @mantine/form
- Host: GitHub
- URL: https://github.com/mantinedev/mantine-form-yup-resolver
- Owner: mantinedev
- License: mit
- Created: 2023-11-09T16:58:14.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-11-09T17:31:13.000Z (over 1 year ago)
- Last Synced: 2025-04-20T01:11:34.519Z (about 2 months ago)
- Language: TypeScript
- Homepage: https://mantine.dev/form/schema-validation/#yup
- Size: 960 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mantine-form-yup-resolver
[yup](https://www.npmjs.com/package/yup) resolver for [@mantine/form](https://mantine.dev/form/use-form/).
## Installation
With yarn:
```sh
yarn add yup mantine-form-yup-resolver
```With npm:
```sh
npm install yup mantine-form-yup-resolver
```## Basic fields validation
```tsx
import * as yup from 'yup';
import { useForm } from '@mantine/form';
import { yupResolver } from 'mantine-form-yup-resolver';const schema = yup.object().shape({
name: yup.string().min(2, 'Name should have at least 2 letters'),
email: yup.string().required('Invalid email').email('Invalid email'),
age: yup.number().min(18, 'You must be at least 18 to create an account'),
});const form = useForm({
initialValues: {
name: '',
email: '',
age: 16,
},
validate: yupResolver(schema),
});form.validate();
form.errors;
// -> {
// name: 'Name should have at least 2 letters',
// email: 'Invalid email',
// age: 'You must be at least 18 to create an account'
// }
```## Nested fields validation
```tsx
import * as yup from 'yup';
import { useForm } from '@mantine/form';
import { yupResolver } from 'mantine-form-yup-resolver';const nestedSchema = yup.object().shape({
nested: yup.object().shape({
field: yup.string().min(2, 'Field should have at least 2 letters'),
}),
});const form = useForm({
initialValues: {
nested: {
field: '',
},
},
validate: yupResolver(nestedSchema),
});form.validate();
form.errors;
// -> {
// 'nested.field': 'Field should have at least 2 letters',
// }
```## List fields validation
```tsx
import * as yup from 'yup';
import { useForm } from '@mantine/form';
import { yupResolver } from 'mantine-form-yup-resolver';const listSchema = yup.object().shape({
list: yup.array().of(
yup.object().shape({
name: yup.string().min(2, 'Name should have at least 2 letters'),
})
),
});const form = useForm({
initialValues: {
list: [{ name: '' }],
},
validate: yupResolver(listSchema),
});form.validate();
form.errors;
// -> {
// 'list.0.name': 'Name should have at least 2 letters',
// }
```## License
MIT