@@ -35,12 +35,15 @@ export function getBlankObject(schema, getRef) {
3535
3636
3737export function getBlankArray ( schema , getRef ) {
38- if ( schema . default )
38+ let minItems = schema . minItems || schema . min_items || 0 ;
39+
40+ if ( schema . default && schema . default . length >= minItems )
3941 return schema . default ;
4042
4143 let items = [ ] ;
4244
43- let minItems = schema . minItems || schema . min_items || 0 ;
45+ if ( schema . default )
46+ items = [ ...schema . default ] ;
4447
4548 if ( minItems === 0 )
4649 return items ;
@@ -59,23 +62,29 @@ export function getBlankArray(schema, getRef) {
5962 type = 'object' ;
6063
6164 if ( type === 'array' ) {
62- items . push ( getBlankArray ( schema . items , getRef ) )
65+ while ( items . length < minItems )
66+ items . push ( getBlankArray ( schema . items , getRef ) ) ;
6367 return items ;
64- }
65- else if ( type === 'object' ) {
66- items . push ( getBlankObject ( schema . items , getRef ) ) ;
68+ } else if ( type === 'object' ) {
69+ while ( items . length < minItems )
70+ items . push ( getBlankObject ( schema . items , getRef ) ) ;
6771 return items ;
6872 }
6973
7074 if ( schema . items . widget === 'multiselect' )
7175 return items ;
7276
73- if ( type === 'boolean' )
74- items . push ( schema . items . default === false ? false : ( schema . items . default || null ) ) ;
75- else if ( type === 'integer' || type === 'number' )
76- items . push ( schema . items . default === 0 ? 0 : ( schema . items . default || null ) ) ;
77- else // string, etc.
78- items . push ( schema . items . default || '' ) ;
77+ if ( type === 'boolean' ) {
78+ while ( items . length < minItems )
79+ items . push ( schema . items . default === false ? false : ( schema . items . default || null ) ) ;
80+ } else if ( type === 'integer' || type === 'number' ) {
81+ while ( items . length < minItems )
82+ items . push ( schema . items . default === 0 ? 0 : ( schema . items . default || null ) ) ;
83+ } else {
84+ // string, etc.
85+ while ( items . length < minItems )
86+ items . push ( schema . items . default || '' ) ;
87+ }
7988
8089 return items ;
8190}
@@ -116,23 +125,38 @@ function getSyncedArray(data, schema, getRef) {
116125 }
117126
118127 let type = schema . items . type ;
128+ let minItems = schema . minItems || schema . min_items || 0 ;
119129
120130 if ( type === 'list' )
121131 type = 'array' ;
122132 else if ( type === 'dict' )
123133 type = 'object' ;
124134
135+ const filler = '__JSONRORM_FILLER__' ; // filler for minItems
136+
137+ while ( data . length < minItems )
138+ data . push ( filler ) ;
139+
125140 for ( let i = 0 ; i < data . length ; i ++ ) {
126141 let item = data [ i ] ;
127142
128143 if ( type === 'array' ) {
144+ if ( item === filler )
145+ item = [ ] ;
129146 newData [ i ] = getSyncedArray ( item , schema . items , getRef ) ;
130147 } else if ( type === 'object' ) {
148+ if ( item === filler )
149+ item = { } ;
131150 newData [ i ] = getSyncedObject ( item , schema . items , getRef ) ;
132- }
133- else {
134- if ( ( type === 'integer' || type === 'number' ) && item === '' )
135- newData [ i ] = schema . items . default === 0 ? 0 : ( schema . items . default || null ) ;
151+ } else {
152+ if ( item === filler ) {
153+ if ( type === 'integer' || type === 'number' )
154+ newData [ i ] = schema . items . default === 0 ? 0 : ( schema . items . default || null ) ;
155+ else if ( type === 'boolean' )
156+ newData [ i ] = schema . items . default === false ? false : ( schema . items . default || null ) ;
157+ else
158+ newData [ i ] = schema . items . default || '' ;
159+ }
136160 }
137161 }
138162
@@ -180,10 +204,14 @@ function getSyncedObject(data, schema, getRef) {
180204 else if ( type === 'object' )
181205 newData [ key ] = getSyncedObject ( data [ key ] , schemaValue , getRef ) ;
182206 else {
183- if ( ( type === 'integer' || type === 'number' ) && data [ key ] === '' )
184- newData [ key ] = schemaValue . default === 0 ? 0 : ( schemaValue . default || null ) ;
185- else
207+ if ( data [ key ] === '' ) {
208+ if ( type === 'integer' || type === 'number' )
209+ newData [ key ] = schemaValue . default === 0 ? 0 : ( schemaValue . default || null ) ;
210+ else if ( type === 'boolean' )
211+ newData [ key ] = schemaValue . default === false ? false : ( schemaValue . default || null ) ;
212+ } else {
186213 newData [ key ] = data [ key ] ;
214+ }
187215 }
188216 }
189217
0 commit comments