|
| 1 | +# Doubter plugin for Roqueform |
| 2 | + |
| 3 | +Plugin that enhances [Roqueform](https://github.com/smikhalevski/roqueform) fields with validation methods powered by |
| 4 | +[Doubter](https://github.com/smikhalevski/doubter). |
| 5 | + |
| 6 | +```sh |
| 7 | +npm install --save-prod @roqueform/doubter-plugin |
| 8 | +``` |
| 9 | + |
| 10 | +# Usage example |
| 11 | + |
| 12 | +🔥 [**Try it on CodeSandbox**](https://codesandbox.io/s/roqueform-doubter-plugin-example-74hkgw) |
| 13 | + |
| 14 | +```tsx |
| 15 | +import * as d from 'doubter'; |
| 16 | +import { useField } from 'roqueform'; |
| 17 | +import { doubterPlugin } from '@roqueform/doubter-plugin'; |
| 18 | + |
| 19 | +const valueType = d.object({ |
| 20 | + bar: d.string().min(1), |
| 21 | +}); |
| 22 | + |
| 23 | +export const App = () => { |
| 24 | + const rootField = useField({ bar: '' }, doubterPlugin(valueType)); |
| 25 | + |
| 26 | + const handleSubmit = (event: SyntheticEvent): void => { |
| 27 | + event.preventDefault(); |
| 28 | + |
| 29 | + // Trigger validation |
| 30 | + rootField.validate(); |
| 31 | + |
| 32 | + if (rootField.isInvalid()) { |
| 33 | + // Isses are associated with fields automatically |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // The form value to submit |
| 38 | + const value = rootField.getValue(); |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <form onSubmit={handleSubmit}> |
| 43 | + |
| 44 | + <Field field={rootField.at('bar')}> |
| 45 | + {barField => ( |
| 46 | + <> |
| 47 | + <input |
| 48 | + value={barField.getValue()} |
| 49 | + onChange={event => { |
| 50 | + barField.dispatchValue(event.target.value); |
| 51 | + }} |
| 52 | + aria-invalid={barField.isInvalid()} |
| 53 | + /> |
| 54 | + |
| 55 | + {barField.getIssue()?.message} |
| 56 | + </> |
| 57 | + )} |
| 58 | + </Field> |
| 59 | + |
| 60 | + <button type="submit"> |
| 61 | + {'Submit'} |
| 62 | + </button> |
| 63 | + |
| 64 | + </form> |
| 65 | + ); |
| 66 | +}; |
| 67 | +``` |
| 68 | + |
| 69 | +# Validating fields |
| 70 | + |
| 71 | +First, you should first define your runtime data types using [Doubter](https://github.com/smikhalevski/doubter) DSL |
| 72 | +methods. |
| 73 | + |
| 74 | +```ts |
| 75 | +import * as d from 'doubter'; |
| 76 | + |
| 77 | +const valueType = d.object({ |
| 78 | + bar: d.string().min(5) |
| 79 | +}); |
| 80 | +// → Type<{ bar: string }> |
| 81 | +``` |
| 82 | + |
| 83 | +Then you can create a new field and enhance it with validation methods: |
| 84 | + |
| 85 | +```ts |
| 86 | +import { useField } from 'roqueform'; |
| 87 | +import { doubterPlugin } from '@roqueform/doubter-plugin'; |
| 88 | + |
| 89 | +const rootField = useField({ bar: 'qux' }, doubterPlugin(valueType)); |
| 90 | +``` |
| 91 | + |
| 92 | +Type of the field value is inferred from the provided runtime type definition, so everything remains statically checked |
| 93 | +even there's no TypeScript types were explicitly specified. |
| 94 | + |
| 95 | +When you call the `validate` method it triggers validation of the field and all of its derived fields. So if you call |
| 96 | +`validate` on the derived field, it won't validate the parent field: |
| 97 | + |
| 98 | +```ts |
| 99 | +// rootField is not validated here! |
| 100 | +rootField.at('bar').validate(); |
| 101 | +``` |
| 102 | + |
| 103 | +So it's safe to trigger validation of a single text field on every keystroke, since it does't have an overhead of |
| 104 | +validating the whole form object. On the other hand, you can validate the whole form by calling validate on the root |
| 105 | +field. |
| 106 | + |
| 107 | +To detect whether the field, or any of its derived fields contain an issue: |
| 108 | + |
| 109 | +```ts |
| 110 | +rootField.isInvalid(); |
| 111 | +// → true |
| 112 | +``` |
| 113 | + |
| 114 | +To retrieve an issue associated with a particular field: |
| 115 | + |
| 116 | +```ts |
| 117 | +rootField.at('bar').getIssue(); |
| 118 | +// → { message: 'Must have the minimum length of 5', … } |
| 119 | +``` |
| 120 | + |
| 121 | +# Manage issues manually |
| 122 | + |
| 123 | +You can manually associate an issue with the field: |
| 124 | + |
| 125 | +```ts |
| 126 | +rootField.at('bar').setIssue({ message: 'Oh, snap!' }); |
| 127 | +``` |
| 128 | + |
| 129 | +This allows you to mix client-side and server-side validation using the same mechanism. |
| 130 | + |
| 131 | +To delete an issue for the particular field: |
| 132 | + |
| 133 | +```ts |
| 134 | +rootField.at('bar').deleteIssue(); |
| 135 | +``` |
| 136 | + |
| 137 | +Sometimes it is required to clear issues of the field itself and all of its derived fields: |
| 138 | + |
| 139 | +```ts |
| 140 | +rootField.clearIssues(); |
| 141 | +``` |
0 commit comments