https://github.com/mantinedev/mantine-form-joi-resolver
Joi schema resolver for @mantine/form
https://github.com/mantinedev/mantine-form-joi-resolver
Last synced: 4 months ago
JSON representation
Joi schema resolver for @mantine/form
- Host: GitHub
- URL: https://github.com/mantinedev/mantine-form-joi-resolver
- Owner: mantinedev
- License: mit
- Created: 2023-11-09T17:32:31.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-11-10T07:36:54.000Z (over 1 year ago)
- Last Synced: 2025-01-26T14:20:00.784Z (4 months ago)
- Language: TypeScript
- Homepage: https://mantine.dev/form/schema-validation/#joi
- Size: 963 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-joi-resolver
[joi](https://www.npmjs.com/package/joi) resolver for [@mantine/form](https://mantine.dev/form/use-form/).
## Installation
With yarn:
```sh
yarn add joi mantine-form-joi-resolver
```With npm:
```sh
npm install joi mantine-form-joi-resolver
```## Basic fields validation
```tsx
import Joi from 'joi';
import { useForm } from '@mantine/form';
import { joiResolver } from 'mantine-form-joi-resolver';const schema = Joi.object({
name: Joi.string().min(2).messages({
'string.min': 'Name should have at least 2 letters',
'string.empty': 'Name should have at least 2 letters',
}),
email: Joi.string()
.email({ tlds: { allow: false } })
.messages({
'string.email': 'Invalid email',
'string.empty': 'Invalid email',
}),
age: Joi.number().min(18).message('You must be at least 18 to create an account'),
});const form = useForm({
initialValues: {
name: '',
email: '',
age: 16,
},
validate: joiResolver(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 Joi from 'joi';
import { useForm } from '@mantine/form';
import { joiResolver } from 'mantine-form-joi-resolver';const nestedSchema = Joi.object({
nested: Joi.object({
field: Joi.string().min(2).messages({
'string.min': 'Field should have at least 2 letters',
'string.empty': 'Field should have at least 2 letters',
}),
}),
});
const form = useForm({
initialValues: {
nested: {
field: '',
},
},
validate: joiResolver(nestedSchema),
});form.validate();
form.errors;
// -> {
// 'nested.field': 'Field should have at least 2 letters',
// }
```## List fields validation
```tsx
import Joi from 'joi';
import { useForm } from '@mantine/form';
import { joiResolver } from 'mantine-form-joi-resolver';const listSchema = Joi.object({
list: Joi.array().items(
Joi.object({
name: Joi.string().min(2).messages({
'string.min': 'Name should have at least 2 letters',
'string.empty': 'Name should have at least 2 letters',
}),
})
),
});const form = useForm({
initialValues: {
list: [{ name: '' }],
},
validate: joiResolver(listSchema),
});form.validate();
form.errors;
// -> {
// 'list.0.name': 'Name should have at least 2 letters',
// }
```## License
MIT