Skip to content

Commit f15751b

Browse files
committed
Fix #92: Consider oneOf/anyOf when syncing data
1 parent 18a1393 commit f15751b

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/data.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ function getSyncedObject(data, schema, getRef) {
294294
for (let i = 0; i < keys.length; i++) {
295295
let key = keys[i];
296296
let schemaValue = schema_keys[key];
297-
297+
298298
let isRef = schemaValue.hasOwnProperty('$ref');
299299

300300
if (isRef) {
@@ -314,10 +314,17 @@ function getSyncedObject(data, schema, getRef) {
314314
}
315315

316316
if (!data.hasOwnProperty(key)) {
317+
/* This key is declared in schema but it's not present in the data.
318+
So we can use blank data here.
319+
*/
317320
if (type === 'array')
318321
newData[key] = getSyncedArray([], schemaValue, getRef);
319322
else if (type === 'object')
320323
newData[key] = getSyncedObject({}, schemaValue, getRef);
324+
else if (type === 'oneOf')
325+
newData[key] = getBlankOneOf(schemaValue, getRef);
326+
else if (type === 'anyOf')
327+
newData[key] = getBlankAntOf(schemaValue, getRef);
321328
else if (type === 'boolean')
322329
newData[key] = default_ === false ? false : (default_ || null);
323330
else if (type === 'integer' || type === 'number')
@@ -329,6 +336,10 @@ function getSyncedObject(data, schema, getRef) {
329336
newData[key] = getSyncedArray(data[key], schemaValue, getRef);
330337
else if (type === 'object')
331338
newData[key] = getSyncedObject(data[key], schemaValue, getRef);
339+
else if (type === 'oneOf')
340+
newData[key] = getSyncedOneOf(data[key], schemaValue, getRef);
341+
else if (type === 'anyOf')
342+
newData[key] = getSyncedAnyOf(data[key], schemaValue, getRef);
332343
else {
333344
// if the current value is not in choices, we reset to blank
334345
if (!valueInChoices(schemaValue, data[key]))
@@ -349,9 +360,14 @@ function getSyncedObject(data, schema, getRef) {
349360

350361
if (schemaValue.hasOwnProperty('const'))
351362
newData[key] = schemaValue.const;
352-
353363
}
354364

365+
if (schema.hasOwnProperty('oneOf'))
366+
newData = {...newData, ...getSyncedOneOf(data, schema, getRef)};
367+
368+
if (schema.hasOwnProperty('anyOf'))
369+
newData = {...newData, ...getSyncedAnyOf(data, schema, getRef)};
370+
355371
return newData;
356372
}
357373

@@ -397,7 +413,6 @@ export function getSyncedData(data, schema, getRef) {
397413
delete schema['$ref'];
398414
}
399415

400-
401416
let type = getSchemaType(schema);
402417

403418
let syncFunc = getSyncFunc(type);
@@ -479,6 +494,14 @@ export function findMatchingSubschemaIndex(data, schema, getRef, schemaName) {
479494
}
480495
}
481496

497+
if (index === null) {
498+
// still no match found
499+
if (data === null) // for null data, return the first subschema and hope for the best
500+
index = 1;
501+
else // for anything else, throw error
502+
throw new Error("No matching subschema found in '" + schemaName + "' for data '" + data + "' (type: " + dataType + ")");
503+
}
504+
482505
return index;
483506
}
484507

0 commit comments

Comments
 (0)