-
-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The unique rule from VineJS should be simpler? #995
Comments
I made an implementation on a side project, but I don't know if it follows the VineJS best way of doing it: import vine from '@vinejs/vine'
import { FieldContext } from '@vinejs/vine/types'
import db from '@adonisjs/lucid/services/db'
import { DatabaseQueryBuilderContract } from '@adonisjs/lucid/types/querybuilder'
/**
* Options accepted by the uniqueSimple rule
*/
export type UniqueSimpleOptions = {
table: string
column?: string
filter?: (db: DatabaseQueryBuilderContract, value: string, field: FieldContext) => Promise<void>
}
/**
* Implementation
*/
async function uniqueSimple(value: unknown, options: UniqueSimpleOptions, field: FieldContext) {
/**
* We do not want to deal with non-string
* values. The "string" rule will handle the
* the validation.
*/
if (typeof value !== 'string') {
return
}
if (typeof field.name !== 'string') {
return
}
const columnName = options.column || field.name
const baseQuery = db.from(options.table).select(columnName).where(columnName, value)
await options.filter?.(baseQuery, value, field)
const row = await baseQuery.first()
if (row) {
field.report('The {{ field }} has already been taken', 'database.unique', field)
}
}
/**
* Converting a function to a VineJS rule
*/
export const uniqueSimpleRule = vine.createRule(uniqueSimple) |
Happy the discussion exists as I was on my way to open an identical one. To contribute a bit: v5 |
@thetutlage ; I found this implemention in https://github.com/adocasts/adocasts/blob/next/app/validators/helpers/db.ts Used that way: import { exists } from '#app/validators/helpers/db'
vine.string().exists(exists('table', 'field', { caseInsensitive: boolean })) That finds my needs for the migration from I guess you could tweak it to be the exact same as the previous rule (object parameters). |
We really need simple validations, like in version 5 |
Can we have some PR for the same? Also, will be nice if you can also take care of the docs for the same |
Can do the PR tonight but don't want to lick the cookie so feel free to PR first. Just to clear things up, @thetutlage do you want it to fully replace the current implem or to live aside it? |
Live alongside the current API |
Discussed in adonisjs/core#4356
Originally posted by brunolipe-a January 26, 2024
I think that always use a query to check if the value is unique in database is to complex for something that we use very often.
Maybe we can have an uniqueRaw that uses the current code, and other that receives only table as required, something like that:
The text was updated successfully, but these errors were encountered: