|
| 1 | +# @vee-validate/joi |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="https://vee-validate.logaretm.com/v4/integrations/joi-schema-validation/" target="_blank"> |
| 5 | + <img width="150" src="https://github.com/logaretm/vee-validate/raw/main/logo.png"> |
| 6 | + </a> |
| 7 | + |
| 8 | + <a href="https://github.com/hapijs/joi/" target="_blank"> |
| 9 | + <img width="150" src="https://joi.dev/img/joiTransparent.png"> |
| 10 | + </a> |
| 11 | +</p> |
| 12 | + |
| 13 | +> Official vee-validate integration with Joi schema validation |
| 14 | +
|
| 15 | +<p align="center"> |
| 16 | + <a href="https://github.com/sponsors/logaretm"> |
| 17 | + <img src='https://sponsors.logaretm.com/sponsors.svg'> |
| 18 | + </a> |
| 19 | +</p> |
| 20 | + |
| 21 | +## Guide |
| 22 | + |
| 23 | +[Joi](https://github.com/hapijs/joi/) is a feature rich validation library for the browser and nodejs |
| 24 | + |
| 25 | +In their own words it is a: |
| 26 | + |
| 27 | +> The most powerful schema description language and data validator for JavaScript. |
| 28 | +
|
| 29 | +You can use joi as a typed schema with the `@vee-validate/joi` package: |
| 30 | + |
| 31 | +```sh |
| 32 | +# npm |
| 33 | +npm install @vee-validate/joi |
| 34 | +# yarn |
| 35 | +yarn add @vee-validate/joi |
| 36 | +# pnpm |
| 37 | +pnpm add @vee-validate/joi |
| 38 | +``` |
| 39 | + |
| 40 | +The `@vee-valdiate/joi` package exposes a `toTypedSchema` function that accepts any joi schema. Which then you can pass along to `validationSchema` option on `useForm`. |
| 41 | + |
| 42 | +This makes the form values and submitted values typed automatically and caters for both input and output types of that schema. |
| 43 | + |
| 44 | +```ts |
| 45 | +import { useForm } from 'vee-validate'; |
| 46 | +import { object, string } from 'joi'; |
| 47 | +import { toTypedSchema } from '@vee-validate/joi'; |
| 48 | + |
| 49 | +interface FormData { |
| 50 | + email: string; |
| 51 | + password: string; |
| 52 | + name?: string; |
| 53 | +} |
| 54 | + |
| 55 | +const { values, handleSubmit } = useForm({ |
| 56 | + validationSchema: toTypedSchema( |
| 57 | + object<FormData>({ |
| 58 | + email: string().min(1).required().message('required'), |
| 59 | + password: string().min(1).message('required'), |
| 60 | + name: string().optional(), |
| 61 | + }), |
| 62 | + ), |
| 63 | +}); |
| 64 | + |
| 65 | +// ❌ Type error, which means `values` is type-safe |
| 66 | +values.email.endsWith('@gmail.com'); |
| 67 | + |
| 68 | +handleSubmit(submitted => { |
| 69 | + // No errors, because email is required! |
| 70 | + submitted.email.endsWith('@gmail.com'); |
| 71 | + |
| 72 | + // ❌ Type error, because `name` is not required so it could be undefined |
| 73 | + // Means that your fields are now type safe! |
| 74 | + submitted.name.length; |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +### Joi default values |
| 79 | + |
| 80 | +You can also define default values on your joi schema directly and it will be picked up by the form: |
| 81 | + |
| 82 | +```ts |
| 83 | +import { useForm } from 'vee-validate'; |
| 84 | +import { object, string } from 'joi'; |
| 85 | +import { toTypedSchema } from '@vee-validate/joi'; |
| 86 | + |
| 87 | +const { values, handleSubmit } = useForm({ |
| 88 | + validationSchema: toTypedSchema( |
| 89 | + object({ |
| 90 | + email: string(). default( '[email protected]'), |
| 91 | + password: string().default(''), |
| 92 | + }), |
| 93 | + ), |
| 94 | +}); |
| 95 | +``` |
| 96 | + |
| 97 | +Your initial values will be using the schema defaults, and also the defaults will be used if the values submitted is missing these fields. |
0 commit comments