99
1010import { sep } from 'node:path'
1111import { Config } from '@athenna/config'
12- import { Validate , SimpleErrorReporter } from '#src'
13- import { Is , Exec , Module , Path } from '@athenna/common'
12+ import { SimpleErrorReporter } from '#src'
13+ import { Exec , Module , Path } from '@athenna/common'
1414import { Annotation , ServiceProvider } from '@athenna/ioc'
1515import { ValidatorImpl } from '#src/validator/ValidatorImpl'
16+ import type { UniqueOptions , ExistsOptions } from '#src/types'
17+ import { CustomValidations } from '#src/validator/CustomValidations'
1618import { ValidationException } from '#src/exceptions/ValidationException'
1719
18- type UniqueOptions = {
19- /**
20- * The table where the database will lookup for the data.
21- */
22- table : string
23-
24- /**
25- * The column name in database. If not defined, the name
26- * of the field in the schema will be used.
27- *
28- * @default 'fieldNameInYourSchema'
29- */
30- column ?: string
31-
32- /**
33- * Use the max field to stablish a max limit for your validation.
34- * In some cases in your database you might have a max of 10 tuples
35- * with the same data. Use this option to validate that the number
36- * of fields registered in database cannot be bigger than the number
37- * defined on this option.
38- *
39- * @example
40- * ```ts
41- * const schema = this.validator.object({
42- * name: this.validator.string().unique({ table: 'users', max: 10 })
43- * })
44- *
45- * const data = { name: 'lenon' }
46- *
47- * // Will throw if there are 10 users with name `lenon`
48- * // created in database
49- * await this.validator.validate({ schema: this.schema, data })
50- * ```
51- * @default undefined
52- */
53- max ?: number
54- }
55-
5620declare module '@vinejs/vine' {
5721 interface VineString {
5822 unique ( options : UniqueOptions ) : this
23+ exists ( options : ExistsOptions ) : this
5924 }
6025}
6126
@@ -81,44 +46,8 @@ export class ValidatorProvider extends ServiceProvider {
8146 return
8247 }
8348
84- const DB = ioc . safeUse ( 'Athenna/Core/Database' )
85-
86- Validate . extend ( ) . string ( 'unique' , async ( value , options , field ) => {
87- /**
88- * We do not want to deal with non-string
89- * values. The "string" rule will handle the
90- * the validation.
91- */
92- if ( ! Is . String ( value ) ) {
93- return
94- }
95-
96- if ( ! options . column ) {
97- options . column = field . name as string
98- }
99-
100- if ( options . max ) {
101- const rows = await DB . table ( options . table )
102- . select ( options . column )
103- . where ( options . column , value )
104- . findMany ( )
105-
106- if ( rows . length > options . max ) {
107- field . report ( 'The {{ field }} field is not unique' , 'unique' , field )
108- }
109-
110- return
111- }
112-
113- const existsRow = await DB . table ( options . table )
114- . select ( options . column )
115- . where ( options . column , value )
116- . exists ( )
117-
118- if ( existsRow ) {
119- field . report ( 'The {{ field }} field is not unique' , 'unique' , field )
120- }
121- } )
49+ CustomValidations . registerUnique ( )
50+ CustomValidations . registerExists ( )
12251 }
12352
12453 public async registerValidators ( ) {
0 commit comments