Skip to content

Commit

Permalink
Merge pull request #998 from Tahul/develop
Browse files Browse the repository at this point in the history
fix(vinejs): add `exists` and `unique` bindings to `VineNumber`
  • Loading branch information
thetutlage committed Feb 6, 2024
2 parents c487b1a + 02c153f commit e038c67
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 5 additions & 1 deletion providers/database_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ declare module '@adonisjs/core/test_utils' {
* Extending VineJS schema types
*/
declare module '@vinejs/vine' {
export interface VineString {
interface VineLucidBindings {
/**
* Ensure the value is unique inside the database by self
* executing a query.
Expand All @@ -58,6 +58,10 @@ declare module '@vinejs/vine' {
*/
exists(callback: (db: Database, value: string, field: FieldContext) => Promise<boolean>): this
}

interface VineNumber extends VineLucidBindings {}

interface VineString extends VineLucidBindings {}
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/bindings/vinejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
* file that was distributed with this source code.
*/

import vine, { VineString } from '@vinejs/vine'
import vine, { VineNumber, VineString } from '@vinejs/vine'
import type { Database } from '../database/main.js'

/**
* Defines the "unique" and "exists" validation rules with
* VineJS.
*/
export function defineValidationRules(db: Database) {
const uniqueRule = vine.createRule<Parameters<VineString['unique']>[0]>(
const uniqueRule = vine.createRule<Parameters<VineString['unique'] | VineNumber['unique']>[0]>(
async (value, checker, field) => {
if (!field.isValid) {
return
Expand All @@ -28,7 +28,7 @@ export function defineValidationRules(db: Database) {
}
)

const existsRule = vine.createRule<Parameters<VineString['exists']>[0]>(
const existsRule = vine.createRule<Parameters<VineString['exists'] | VineNumber['exists']>[0]>(
async (value, checker, field) => {
if (!field.isValid) {
return
Expand All @@ -47,4 +47,10 @@ export function defineValidationRules(db: Database) {
VineString.macro('exists', function (this: VineString, checker) {
return this.use(existsRule(checker))
})
VineNumber.macro('unique', function (this: VineNumber, checker) {
return this.use(uniqueRule(checker))
})
VineNumber.macro('exists', function (this: VineNumber, checker) {
return this.use(existsRule(checker))
})
}

0 comments on commit e038c67

Please sign in to comment.