|
| 1 | +# Joi |
| 2 | + |
| 3 | +Basic usage: |
| 4 | + |
| 5 | +```tsx |
| 6 | +"use client"; |
| 7 | +import { JoiProvider } from "@autoform/joi"; |
| 8 | +import Joi from "joi"; |
| 9 | +import { buildJoiFieldConfig } from "@autoform/react"; |
| 10 | +import { AutoForm, FieldTypes } from "@autoform/mui"; // use any UI library |
| 11 | + |
| 12 | +const fieldConfig = buildJoiFieldConfig< |
| 13 | + FieldTypes, |
| 14 | + { |
| 15 | + // You can define custom props here |
| 16 | + isImportant?: boolean; |
| 17 | + } |
| 18 | +>(); |
| 19 | + |
| 20 | +// Define your form schema using Joi |
| 21 | +const joiFormSchema = Joi.object({ |
| 22 | + // Use the "label" method to set the label |
| 23 | + // You can set a default value |
| 24 | + name: Joi.string().required().label("Your Name").default("John Doe"), |
| 25 | + |
| 26 | + age: Joi.number().required().positive().integer().messages({ |
| 27 | + "any.required": |
| 28 | + "We need your age to verify you're old enough to use this form", |
| 29 | + }), |
| 30 | + |
| 31 | + email: Joi.string() |
| 32 | + .email({ tlds: { allow: false } }) |
| 33 | + // You can add additional config for a field using fieldConfig |
| 34 | + .meta( |
| 35 | + fieldConfig({ |
| 36 | + inputProps: { |
| 37 | + type: "email", |
| 38 | + }, |
| 39 | + customData: { |
| 40 | + // You can add custom data here |
| 41 | + isImportant: true, |
| 42 | + }, |
| 43 | + }) |
| 44 | + ), |
| 45 | + |
| 46 | + website: Joi.string().uri().allow(null), |
| 47 | + |
| 48 | + // Date will show a date picker |
| 49 | + birthday: Joi.date().optional(), |
| 50 | + |
| 51 | + // You can use arrays and sub-objects |
| 52 | + guests: Joi.array().items( |
| 53 | + Joi.object({ |
| 54 | + name: Joi.string().required(), |
| 55 | + }) |
| 56 | + ), |
| 57 | + hobbies: Joi.array().items(Joi.string()), |
| 58 | + |
| 59 | + // You can use enums, will show a select field |
| 60 | + color: Joi.any().valid("red", "green", "blue"), |
| 61 | +}); |
| 62 | + |
| 63 | +export const joiSchemaProvider = new JoiProvider(joiFormSchema); |
| 64 | + |
| 65 | +function App() { |
| 66 | + return ( |
| 67 | + <AutoForm |
| 68 | + schema={joiSchemaProvider} |
| 69 | + onSubmit={(data) => { |
| 70 | + console.log(data); |
| 71 | + }} |
| 72 | + withSubmit |
| 73 | + /> |
| 74 | + ); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### Joi configuration |
| 79 | + |
| 80 | +#### Validations |
| 81 | + |
| 82 | +Your form schema can use any of Joi's validation methods. |
| 83 | + |
| 84 | +#### Label |
| 85 | + |
| 86 | +You can use the `label` method to set a label for each field. If no label is set, the field name will be used and un-camel-cased. |
| 87 | + |
| 88 | +```tsx |
| 89 | +const formSchema = Joi.object({ |
| 90 | + username: Joi.string().label("Your username"), |
| 91 | + someValue: Joi.string(), // Will be "Some Value" |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +#### Default values |
| 96 | + |
| 97 | +You can set a default value for a field using the `default` method. |
| 98 | + |
| 99 | +```tsx |
| 100 | +const formSchema = Joi.object({ |
| 101 | + favouriteNumber: Joi.number().default(5), |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +If you want to set default value of date, convert it to Date first using `new Date(val)`. |
| 106 | + |
| 107 | +#### Select/Enums |
| 108 | + |
| 109 | +You can use `any().valid` to create a select field. |
| 110 | + |
| 111 | +```tsx |
| 112 | +enum BreadTypes { |
| 113 | + White = "White bread", |
| 114 | + Brown = "Brown bread", |
| 115 | + Wholegrain = "Wholegrain bread", |
| 116 | + Other = "Other", |
| 117 | +} |
| 118 | + |
| 119 | +const formSchema = Joi.object({ |
| 120 | + breadType: Joi.any().valid(...Object.values(BreadTypes)), |
| 121 | +}); |
| 122 | +``` |
| 123 | + |
| 124 | +#### Arrays |
| 125 | + |
| 126 | +AutoForm supports arrays: |
| 127 | + |
| 128 | +```tsx |
| 129 | +const formSchema = Joi.object({ |
| 130 | + invitedGuests: Joi.array() |
| 131 | + .items( |
| 132 | + // Define the fields for each item |
| 133 | + Joi.object({ |
| 134 | + name: Joi.string(), |
| 135 | + age: Joi.number(), |
| 136 | + }) |
| 137 | + ) |
| 138 | + // Optionally set a custom label - otherwise this will be inferred from the field name |
| 139 | + .label("Guests invited to the party"), |
| 140 | + hobbies: Joi.array().items(Joi.string()), |
| 141 | +}); |
| 142 | +``` |
| 143 | + |
| 144 | +Arrays are not supported as the root element of the form schema. |
| 145 | + |
| 146 | +You also can set default value of an array using .default(), but please make sure the array element has same structure with the schema. |
| 147 | + |
| 148 | +```tsx |
| 149 | +// Add array default value example |
| 150 | +const formSchema = Joi.object({ |
| 151 | + invitedGuests: Joi.array() |
| 152 | + .items( |
| 153 | + Joi.object({ |
| 154 | + name: Joi.string().required(), |
| 155 | + age: Joi.number(), |
| 156 | + }) |
| 157 | + ) |
| 158 | + .default([ |
| 159 | + { name: "John", age: 24 }, |
| 160 | + { name: "Jane", age: 20 }, |
| 161 | + ]) |
| 162 | + .label("Guests invited to the party"), |
| 163 | +}); |
| 164 | +``` |
| 165 | + |
| 166 | +#### Sub-objects |
| 167 | + |
| 168 | +You may use sub-objects to group fields together. These will be rendered with their own title. |
| 169 | + |
| 170 | +```tsx |
| 171 | +const formSchema = Joi.object({ |
| 172 | + guestDetails: Joi.object({ |
| 173 | + name: Joi.string(), |
| 174 | + age: Joi.number(), |
| 175 | + }), |
| 176 | +}); |
| 177 | +``` |
| 178 | + |
| 179 | +#### Field configuration |
| 180 | + |
| 181 | +You can use the `fieldConfig` function to set additional configuration for how a field should be rendered. This function is independent of the UI library you use so you can provide the FieldTypes that are supported by your UI library. |
| 182 | + |
| 183 | +It's recommended that you create your own fieldConfig function that uses the base fieldConfig function from `@autoform/react` and adds your own customizations: |
| 184 | + |
| 185 | +```tsx |
| 186 | +import { buildJoiFieldConfig } from "@autoform/react"; |
| 187 | +import { FieldTypes } from "@autoform/mui"; |
| 188 | + |
| 189 | +const fieldConfig = buildJoiFieldConfig< |
| 190 | + FieldTypes, // You should provide the "FieldTypes" type from the UI library you use |
| 191 | + { |
| 192 | + isImportant?: boolean; // You can add custom props here |
| 193 | + } |
| 194 | +>(); |
| 195 | +``` |
0 commit comments