@@ -44,33 +44,62 @@ export function getBlankData(schema) {
4444}
4545
4646
47- export function getBlankItem ( schema ) {
48- let dataObject = { } ;
4947
50- for ( let key in schema . fields ) {
51- if ( ! schema . fields . hasOwnProperty ( key ) )
52- continue ;
48+ function getSyncedArray ( data , schema ) {
49+ let newData = JSON . parse ( JSON . stringify ( data ) ) ;
5350
54- let item = schema . fields [ key ] ;
51+ for ( let i = 0 ; i < data . length ; i ++ ) {
52+ let item = data [ i ] ;
5553
56- dataObject [ key ] = '' ;
54+ if ( schema . items . type === 'array' ) {
55+ newData [ i ] = syncArray ( item , schema . items ) ;
56+ } else if ( schema . items . type === 'object' ) {
57+ newData [ i ] = syncObject ( item , schema . items ) ;
58+ }
59+ }
60+
61+ return newData ;
62+ }
63+
64+
65+ function getSyncedObject ( data , schema ) {
66+ let newData = JSON . parse ( JSON . stringify ( data ) ) ;
67+
68+ let keys = [ ...Object . keys ( schema . keys ) ] ;
69+
70+ for ( let i = 0 ; i < keys . length ; i ++ ) {
71+ let key = keys [ i ] ;
72+ let schemaValue = schema . keys [ key ] ;
73+
74+ if ( ! data . hasOwnProperty ( key ) ) {
75+ if ( schemaValue . type === 'string' )
76+ newData [ key ] = '' ;
77+ else if ( schemaValue . type === 'array' )
78+ newData [ key ] = syncArray ( [ ] , schemaValue ) ;
79+ else if ( schemaValue . type === 'object' )
80+ newData [ key ] = syncObject ( { } , schemaValue ) ;
81+ } else {
82+ if ( schemaValue . type === 'string' )
83+ newData [ key ] = data [ key ] ;
84+ else if ( schemaValue . type === 'array' )
85+ newData [ key ] = syncArray ( data [ key ] , schemaValue ) ;
86+ else if ( schemaValue . type === 'object' )
87+ newData [ key ] = syncObject ( data [ key ] , schemaValue ) ;
88+ }
89+
5790 }
5891
59- return dataObject ;
92+ return newData ;
6093}
6194
6295
6396export function getSyncedData ( data , schema ) {
6497 // adds those keys to data which are in schema but not in data
6598
66- let blankItem = getBlankItem ( schema ) ;
67-
68- if ( schema . type === 'object' ) {
69- return { ...blankItem , ...data } ;
70- } else if ( schema . type === 'array' ) {
71- for ( let i = 0 ; i < data . length ; i ++ ) {
72- data [ i ] = { ...blankItem , ...data [ i ] } ;
73- }
99+ if ( schema . type === 'array' ) {
100+ return getSyncedArray ( data , schema ) ;
101+ } else if ( schema . type === 'object' ) {
102+ return getSyncedObject ( data , schema ) ;
74103 }
75104
76105 return data ;
0 commit comments