1- import { cloneDeep , get , has , omit , isArray } from 'lodash'
2- import { is } from '../util'
1+ import { cloneDeep , get , has , isArray , omit } from 'lodash'
2+ import { is , toCamelCase , toSnakeCase } from '../util'
33
44class Validator {
55 public errors : Record < string , any >
@@ -12,42 +12,36 @@ class Validator {
1212 this . errors = errors
1313 }
1414
15- add ( attribute : string , message : string , forceUpdate ?: boolean ) {
16- if ( this . missed ( attribute ) ) {
17- this . errors [ attribute ] = [ ]
15+ add ( field : string , message : string , forceUpdate ?: boolean ) {
16+ if ( this . missed ( field ) ) {
17+ this . errors [ field ] = [ ]
1818 }
19- if ( ! this . errors [ attribute ] . includes ( message ) ) {
20- this . errors [ attribute ] . unshift ( message )
19+ if ( ! this . errors [ field ] . includes ( message ) ) {
20+ this . errors [ field ] . unshift ( message )
2121 }
2222 if ( forceUpdate ) {
23- this . errors [ attribute ] = [ ]
24- this . errors [ attribute ] . push ( message )
23+ this . errors [ field ] = [ ]
24+ this . errors [ field ] . push ( message )
2525 }
2626 }
2727
2828 has ( field : string | string [ ] ) {
29- if ( isArray ( field ) ) {
30- return is ( Object . keys ( this . errors ) , field )
31- }
32- let hasError = has ( this . errors , field )
33- if ( ! hasError ) {
34- const errors = Object . keys ( this . errors ) . filter (
35- ( e : string ) => e . startsWith ( `${ field } .` ) || e . startsWith ( `${ field } [` ) ,
36- )
37- hasError = errors . length > 0
38- }
39- return hasError
29+ const fields = this . fields ( field )
30+ return is ( Object . keys ( this . errors ) , fields )
4031 }
4132
42- first ( field : string | string [ ] ) : string | object {
33+ first ( field : string | string [ ] ) : string | Record < string , any > | undefined {
34+ const fields = this . fields ( field )
4335 if ( Array . isArray ( field ) ) {
44- for ( const f of field ) {
45- if ( has ( this . errors , f ) ) return this . first ( f )
36+ for ( const f of fields ) {
37+ if ( ! has ( this . errors , f ) ) continue
38+ return this . first ( f )
4639 }
40+ } else {
41+ const value = this . get ( field )
42+ if ( Array . isArray ( value ) ) return value [ 0 ]
43+ return value
4744 }
48- const value = this . get ( field )
49- if ( Array . isArray ( value ) ) return value [ 0 ]
50- return value
5145 }
5246
5347 firstBy ( obj : Record < string , any > , field ?: string ) {
@@ -86,7 +80,7 @@ class Validator {
8680 return Object . keys ( errors ) . length > 0
8781 }
8882
89- get ( field : string | string [ ] ) : string | string [ ] {
83+ get ( field : string ) : string | string [ ] {
9084 return get ( this . errors , field , [ ] )
9185 }
9286
@@ -106,9 +100,9 @@ class Validator {
106100 this . fill ( { } )
107101 }
108102
109- clear ( attribute ?: string | string [ ] ) {
110- if ( ! attribute ) return this . flush ( )
111- const errors = omit ( cloneDeep ( this . errors ) , attribute )
103+ clear ( field ?: string | string [ ] ) {
104+ if ( ! field ) return this . flush ( )
105+ const errors = omit ( cloneDeep ( this . errors ) , field )
112106 this . fill ( errors )
113107 }
114108
@@ -122,6 +116,18 @@ class Validator {
122116 const names = prefix ? [ name , `${ prefix } .${ name } ` ] : [ name ]
123117 this . clear ( names )
124118 }
119+
120+ fields ( field : string | string [ ] ) {
121+ const fields = [ ]
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 ) )
128+ }
129+ return [ ...new Set ( fields ) ] . filter ( Boolean )
130+ }
125131}
126132
127133export type { Validator as ValidatorType }
0 commit comments