11import { hasOwnProperty , is , isArray } from '../util'
2+ import get from 'lodash.get'
3+ import has from 'lodash.has'
24
35class Validator {
46 public errors : Record < string , any >
@@ -20,11 +22,11 @@ class Validator {
2022 }
2123 }
2224
23- has ( field : any | any [ ] ) {
25+ has ( field : string | string [ ] ) {
2426 if ( isArray ( field ) ) {
2527 return is ( Object . keys ( this . errors ) , field )
2628 }
27- let hasError = hasOwnProperty ( this . errors , field )
29+ let hasError = has ( this . errors , field )
2830 if ( ! hasError ) {
2931 const errors = Object . keys ( this . errors ) . filter (
3032 ( e : string ) => e . startsWith ( `${ field } .` ) || e . startsWith ( `${ field } [` ) ,
@@ -34,16 +36,18 @@ class Validator {
3436 return hasError
3537 }
3638
37- first ( field : any | any [ ] ) : string {
38- if ( field instanceof Array ) {
39+ first ( field : string | string [ ] ) : string | object {
40+ if ( Array . isArray ( field ) ) {
3941 for ( let i = 0 ; i < field . length ; i ++ ) {
40- if ( ! hasOwnProperty ( this . errors , field [ i ] ) ) {
42+ if ( ! has ( this . errors , field [ i ] ) ) {
4143 continue
4244 }
4345 return this . first ( field [ i ] )
4446 }
4547 }
46- return this . get ( field ) [ 0 ]
48+ const value = this . get ( field as string )
49+ if ( Array . isArray ( value ) ) return value [ 0 ]
50+ return value // return it if object like
4751 }
4852
4953 firstBy ( obj : Record < string , any > , field ?: string ) : string {
@@ -53,17 +57,15 @@ class Validator {
5357 } else {
5458 value = obj [ field ]
5559 }
56- if ( isArray ( value ) ) {
57- value = value [ 0 ]
58- }
60+ if ( isArray ( value ) ) value = value [ 0 ]
5961 return value
6062 }
6163
62- missed ( field ? : string | string [ ] ) : boolean {
64+ missed ( field : string | string [ ] ) : boolean {
6365 return ! this . has ( field )
6466 }
6567
66- nullState ( field ? : string | string [ ] ) : boolean | null {
68+ nullState ( field : string | string [ ] ) : boolean | null {
6769 return this . has ( field ) ? this . missed ( field ) : null
6870 }
6971
@@ -85,7 +87,7 @@ class Validator {
8587 }
8688
8789 get ( field : string ) : string | string [ ] {
88- return this . errors [ field ] || [ ]
90+ return get ( this . errors , field ) || [ ]
8991 }
9092
9193 all ( ) {
0 commit comments