|
| 1 | +--- |
| 2 | +summary: Validation rules added to VineJS through AdonisJS container |
| 3 | +--- |
| 4 | + |
| 5 | +# Validation rules |
| 6 | + |
| 7 | +Lucid adds validation rules to VineJS to use in your schemas. Under the hood, it registers a provider in your AdonisJS application that extends VineJS rules. |
| 8 | +You can read more in the [AdonisJS docs](https://docs.adonisjs.com/guides/concepts/service-providers#service-providers) and [VineJS docs](https://vinejs.dev/docs/extend/custom_rules). |
| 9 | + |
| 10 | +You can use these rules directly from your VineJS schema. For example, the `unique` rule: |
| 11 | + |
| 12 | +```ts |
| 13 | +import vine from '@vinejs/vine' |
| 14 | + |
| 15 | +const schema = vine.object({ |
| 16 | + email: vine |
| 17 | + .string() |
| 18 | + // highlight-start |
| 19 | + .unique({ |
| 20 | + table: 'users', |
| 21 | + column: 'email', |
| 22 | + }), |
| 23 | + // highlight-end |
| 24 | +}) |
| 25 | +``` |
| 26 | + |
| 27 | +## Unique |
| 28 | + |
| 29 | +Ensure the value is unique (does not exists) inside a given database table and column. |
| 30 | + |
| 31 | +:::note |
| 32 | +The rule is a macro for `VineString` and `VineNumber`, so you can use it after `vine.string()` or `vine.number()`. |
| 33 | +::: |
| 34 | + |
| 35 | +You may either pass a callback to query the database directly, or an object: |
| 36 | + |
| 37 | +- The callback must return `true` if the value is unique (does not exist), or `false` if the value already exists. |
| 38 | +- You may also pass an options object to specify the table and column. |
| 39 | + |
| 40 | +```ts |
| 41 | +// Usage with callback |
| 42 | +const schema = vine.object({ |
| 43 | + email: vine |
| 44 | + .string() |
| 45 | + // highlight-start |
| 46 | + .unique((db, value) => { |
| 47 | + const row = await db.from('users').where('email', value).first() |
| 48 | + return row === null |
| 49 | + }), |
| 50 | + // highlight-end |
| 51 | +}) |
| 52 | + |
| 53 | +// Usage with options |
| 54 | +const schema = vine.object({ |
| 55 | + email: vine |
| 56 | + .string() |
| 57 | + // highlight-start |
| 58 | + .unique({ table: 'users', column: 'email' }), |
| 59 | + // highlight-end |
| 60 | +}) |
| 61 | +``` |
| 62 | + |
| 63 | +Following is the list of available parameters for the unique rule: |
| 64 | + |
| 65 | +```ts |
| 66 | +type UniqueRuleCallback = (db: Database, value: string, field: FieldContext) => Promise<boolean> |
| 67 | + |
| 68 | +type UniqueRuleOptions = { |
| 69 | + table: string |
| 70 | + column?: string |
| 71 | + filter?: (db: DatabaseQueryBuilderContract, value: unknown, field: FieldContext) => Promise<void> |
| 72 | +} |
| 73 | +``` |
| 74 | +
|
| 75 | +You may also use your Lucid model directly inside the callback: |
| 76 | +
|
| 77 | +```ts |
| 78 | +const schema = vine.object({ |
| 79 | + email: vine |
| 80 | + .string() |
| 81 | + // highlight-start |
| 82 | + .unique((_, value) => { |
| 83 | + const row = await User.findBy('email', value) |
| 84 | + return row === null |
| 85 | + }), |
| 86 | + // highlight-end |
| 87 | +}) |
| 88 | +``` |
| 89 | + |
| 90 | +## Exists |
| 91 | + |
| 92 | +Ensure the value exists inside a given database table and column. This is the inverse of the unique rule. |
| 93 | + |
| 94 | +:::note |
| 95 | +The rule is also a macro for `VineString` and `VineNumber`, so you can use it after `vine.string()` or `vine.number()`. |
| 96 | +::: |
| 97 | + |
| 98 | +You may either pass a callback to query the database directly, or an object: |
| 99 | + |
| 100 | +- The callback must return `true` if the value exists, `false` otherwise. |
| 101 | +- You may also pass an option object to specify the table and column. |
| 102 | + |
| 103 | +```ts |
| 104 | +// Usage with callback |
| 105 | +const schema = vine.object({ |
| 106 | + slug: vine |
| 107 | + .string() |
| 108 | + // highlight-start |
| 109 | + .exists((db, value) => { |
| 110 | + const row = await db.from('categories').
C9B8
where('slug', value).first() |
| 111 | + return row !== null |
| 112 | + }), |
| 113 | + // highlight-end |
| 114 | +}) |
| 115 | + |
| 116 | +// Usage with options |
| 117 | +const schema = vine.object({ |
| 118 | + slug: vine |
| 119 | + .string() |
| 120 | + // highlight-start |
| 121 | + .exists({ table: 'categories', column: 'slug' }), |
| 122 | + // highlight-end |
| 123 | +}) |
| 124 | +``` |
| 125 | + |
| 126 | +Following is the list of available parameters for the exists rule: |
| 127 | + |
| 128 | +```ts |
| 129 | +type ExistsRuleCallback = (db: Database, value: string, field: FieldContext) => Promise<boolean> |
| 130 | + |
| 131 | +type ExistsRuleOptions = { |
| 132 | + table: string |
| 133 | + column?: string |
| 134 | + filter?: (db: DatabaseQueryBuilderContract, value: unknown, field: FieldContext) => Promise<void> |
| 135 | +} |
| 136 | +``` |
0 commit comments