11import type { SimpleObject } from '../types'
2- import { cloneDeep , get , has , omit } from 'lodash'
2+ import { castArray , cloneDeep , get , has , omit } from 'lodash'
33import { is , toCamelCase , toSnakeCase } from '../util'
44
55class Validator {
@@ -12,11 +12,10 @@ class Validator {
1212 }
1313
1414 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 )
15+ if ( forceUpdate || this . missed ( field ) ) {
16+ this . errors [ field ] = [ message ]
17+ } else if ( ! this . errors [ field ] . includes ( message ) ) {
18+ this . errors [ field ] . unshift ( message )
2019 }
2120 }
2221
@@ -26,32 +25,15 @@ class Validator {
2625 }
2726
2827 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- }
28+ const fields = this . fields ( castArray ( field ) )
29+ const foundField = fields . find ( ( f ) => has ( this . errors , f ) ) ?? ''
30+ const value = this . get ( foundField )
31+ return Array . isArray ( value ) ? value [ 0 ] : value
4432 }
4533
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
34+ firstBy ( obj : SimpleObject < any > , field : string = Object . keys ( obj ) [ 0 ] ) : string {
35+ const value : string = obj [ field ]
36+ return Array . isArray ( value ) ? value [ 0 ] : value
5537 }
5638
5739 missed ( field : string | string [ ] ) {
@@ -64,20 +46,17 @@ class Validator {
6446
6547 any ( field : string [ ] = [ ] , returnObject ?: boolean ) {
6648 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
7849 const errors : SimpleObject < any > = { }
79- fields . forEach ( ( key : string ) => ( errors [ key ] = this . get ( key ) ) )
80- return Object . keys ( errors ) . length > 0
50+
51+ if ( ! fields . length ) return returnObject ? { } : Object . keys ( this . errors ) . length > 0
52+
53+ fields . forEach ( ( f : string ) => {
54+ const val = this . get ( f )
55+ if ( returnObject && val . length ) errors [ f ] = val
56+ else if ( ! returnObject ) errors [ f ] = val
57+ } )
58+
59+ return returnObject ? errors : Object . keys ( errors ) . length > 0
8160 }
8261
8362 get ( field : string ) : string | string [ ] {
@@ -117,15 +96,18 @@ class Validator {
11796 this . clear ( names )
11897 }
11998
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 ) )
99+ fields ( field : string | string [ ] ) {
100+ const processField = ( f : string ) => {
101+ if ( f . includes ( '*' ) ) {
102+ const regex = new RegExp ( `^ ${ f . replace ( '*' , '.*' ) } $` , 'i' )
103+ for ( const key in this . errors ) {
104+ if ( regex . test ( key ) ) fields . push ( toCamelCase ( key ) , toSnakeCase ( key ) )
105+ }
106+ } else fields . push ( toCamelCase ( f ) , toSnakeCase ( f ) )
128107 }
108+
109+ const fields : string [ ] = [ ]
110+ Array . isArray ( field ) ? field . forEach ( processField ) : processField ( field )
129111 return [ ...new Set ( fields ) ] . filter ( Boolean )
130112 }
131113}
0 commit comments