@@ -7,7 +7,9 @@ const cleanKey = (str) => {
7
7
return parts [ parts . length - 1 ] ;
8
8
}
9
9
10
- const flattenObject = ( obj , prefix = '' ) => {
10
+ /* template is because we don't want to flatten the data object if we're validating an object, otherwise we'll
11
+ dig into the object to find the values to validate */
12
+ const flattenObject = ( obj , prefix = '' , template = [ ] ) => {
11
13
if ( ! obj ) {
12
14
return { } ;
13
15
}
@@ -16,7 +18,9 @@ const flattenObject = (obj, prefix = '') => {
16
18
typeof value === 'object' && ! Array . isArray ( value )
17
19
? {
18
20
...flattened ,
19
- ...flattenObject ( value , `${ prefix } ${ key } .` )
21
+ ...( template . includes ( `${ prefix } ${ key } ` )
22
+ ? { [ `${ prefix } ${ key } ` ] : value }
23
+ : flattenObject ( value , `${ prefix } ${ key } .` ) ) ,
20
24
}
21
25
: Object . assign ( flattened , { [ `${ prefix } ${ key } ` ] : value } ) ,
22
26
{ }
@@ -104,9 +108,8 @@ class Validator {
104
108
}
105
109
106
110
async validate ( data , rules ) {
107
- const flatData = flattenObject ( data ) ;
108
111
const flatRules = flattenObject ( rules ) ;
109
-
112
+ const flatData = flattenObject ( data , undefined , Object . keys ( flatRules ) ) ;
110
113
const validationResults = await Promise . all (
111
114
Object . keys ( flatRules ) . flatMap (
112
115
async ( key ) => {
0 commit comments