11import type { SimpleObject } from '../types'
2- import { cloneDeep , get , has , omit } from 'lodash'
2+ import { castArray , cloneDeep , get , has , omit , replace } from 'lodash'
33import { is , toCamelCase , toSnakeCase } from '../util'
44
55class Validator {
6- public successful : boolean
7- public processing : boolean
8-
9- constructor ( public errors : SimpleObject < any > = { } ) {
10- this . processing = false
11- this . successful = false
12- }
6+ constructor ( public errors : SimpleObject < any > = { } , public processing = false , public successful = false ) { }
137
148 add ( field : string , message : string , forceUpdate ?: boolean ) {
15- if ( this . missed ( field ) ) this . errors [ field ] = [ ]
16- if ( ! this . errors [ field ] . includes ( message ) ) this . errors [ field ] . unshift ( message )
17- if ( forceUpdate ) {
18- this . errors [ field ] = [ ]
19- this . errors [ field ] . push ( message )
9+ if ( forceUpdate || this . missed ( field ) ) {
10+ this . errors [ field ] = [ message ]
11+ } else if ( ! this . errors [ field ] . includes ( message ) ) {
12+ this . errors [ field ] . unshift ( message )
2013 }
2114 }
2215
@@ -25,33 +18,15 @@ class Validator {
2518 return is ( Object . keys ( this . errors ) , fields )
2619 }
2720
28- first ( field : string | string [ ] ) : string | undefined {
29- if ( Array . isArray ( field ) ) {
30- const fields = this . fields ( field )
31- let fd = ''
32- for ( const f of fields ) {
33- if ( has ( this . errors , f ) ) {
34- fd = f
35- break
36- }
37- }
38- return this . first ( fd )
39- } else {
40- const value = this . get ( field )
41- if ( Array . isArray ( value ) ) return value [ 0 ]
42- return value
43- }
21+ first ( field : string | string [ ] ) {
22+ const fields = this . fields ( castArray ( field ) )
23+ const foundField = fields . find ( ( f ) => has ( this . errors , f ) ) ?? ''
24+ const value = this . get ( foundField )
25+ return Array . isArray ( value ) ? value [ 0 ] : value
4426 }
4527
46- firstBy ( obj : SimpleObject < any > , field ?: string ) {
47- let value : string
48- if ( ! field ) {
49- value = obj [ Object . keys ( obj ) [ 0 ] ]
50- } else {
51- value = obj [ field ]
52- }
53- if ( Array . isArray ( value ) ) value = value [ 0 ]
54- return value
28+ firstBy ( obj : SimpleObject < any > , field : string = Object . keys ( obj ) [ 0 ] ) : string {
29+ return castArray ( obj [ field ] ) [ 0 ]
5530 }
5631
5732 missed ( field : string | string [ ] ) {
@@ -64,20 +39,17 @@ class Validator {
6439
6540 any ( field : string [ ] = [ ] , returnObject ?: boolean ) {
6641 const fields = this . fields ( field )
67- if ( returnObject ) {
68- const errors : SimpleObject < any > = { }
69- if ( ! fields . length ) return { }
70- for ( const f of fields ) {
71- const val = this . get ( f )
72- if ( ! val . length ) continue
73- errors [ f ] = val
74- }
75- return errors
76- }
77- if ( ! fields . length ) return Object . keys ( this . errors ) . length > 0
7842 const errors : SimpleObject < any > = { }
79- fields . forEach ( ( key : string ) => ( errors [ key ] = this . get ( key ) ) )
80- return Object . keys ( errors ) . length > 0
43+
44+ if ( ! fields . length ) return returnObject ? { } : Object . keys ( this . errors ) . length > 0
45+
46+ fields . forEach ( ( f : string ) => {
47+ const val = this . get ( f )
48+ if ( returnObject && val . length ) errors [ f ] = val
49+ else if ( ! returnObject ) errors [ f ] = val
50+ } )
51+
52+ return returnObject ? errors : Object . keys ( errors ) . length > 0
8153 }
8254
8355 get ( field : string ) : string | string [ ] {
@@ -117,15 +89,18 @@ class Validator {
11789 this . clear ( names )
11890 }
11991
120- fields ( field : string | string [ ] ) : string [ ] {
121- const fields : string [ ] = [ ]
122- if ( Array . isArray ( field ) ) {
123- for ( const f of field ) {
124- fields . push ( toCamelCase ( f ) , toSnakeCase ( f ) )
125- }
126- } else {
127- fields . push ( toCamelCase ( field ) , toSnakeCase ( field ) )
92+ fields ( field : string | string [ ] ) {
93+ const processField = ( f : string ) => {
94+ if ( f . includes ( '*' ) ) {
95+ const regex = new RegExp ( `^ ${ replace ( f , '*' , '.*' ) } $` , 'i' )
96+ for ( const key in this . errors ) {
97+ if ( regex . test ( key ) ) fields . push ( toCamelCase ( key ) , toSnakeCase ( key ) )
98+ }
99+ } else fields . push ( toCamelCase ( f ) , toSnakeCase ( f ) )
128100 }
101+
102+ const fields : string [ ] = [ ]
103+ Array . isArray ( field ) ? field . forEach ( processField ) : processField ( field )
129104 return [ ...new Set ( fields ) ] . filter ( Boolean )
130105 }
131106}
0 commit comments