UNPKG

@adonisjs/lucid

Version:

SQL ORM built on top of Active Record pattern

46 lines (45 loc) 1.44 kB
/* * @adonisjs/lucid * * (c) AdonisJS * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import vine, { VineNumber, VineString } from '@vinejs/vine'; /** * Defines the "unique" and "exists" validation rules with * VineJS. */ export function defineValidationRules(db) { const uniqueRule = vine.createRule(async (value, checker, field) => { if (!field.isValid) { return; } const isUnqiue = await checker(db, value, field); if (!isUnqiue) { field.report('The {{ field }} has already been taken', 'database.unique', field); } }); const existsRule = vine.createRule(async (value, checker, field) => { if (!field.isValid) { return; } const exists = await checker(db, value, field); if (!exists) { field.report('The selected {{ field }} is invalid', 'database.exists', field); } }); VineString.macro('unique', function (checker) { return this.use(uniqueRule(checker)); }); VineString.macro('exists', function (checker) { return this.use(existsRule(checker)); }); VineNumber.macro('unique', function (checker) { return this.use(uniqueRule(checker)); }); VineNumber.macro('exists', function (checker) { return this.use(existsRule(checker)); }); }