Skip to content

Commit 8f33985

Browse files
committed
FIx #59: Validation for oneOf/anyOf inside array items
1 parent 6c40eed commit 8f33985

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/dataValidation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ export default function DataValidator(schema) {
128128

129129
let next_validator = this.getValidator(next_type);
130130

131+
if (!next_validator) {
132+
if (next_schema.hasOwnProperty('oneOf')) {
133+
next_validator = this.validateOneOf;
134+
} else if (next_schema.hasOwnProperty('anyOf')) {
135+
next_validator = this.validateAnyOf;
136+
} else if (next_schema.hasOwnProperty('anyOf')) {
137+
// currently allOf is not supported in array items
138+
// next_validator = this.validateAllOf;
139+
}
140+
}
141+
131142
if (next_validator) {
132143
for (let i = 0; i < data.length; i++)
133144
next_validator(next_schema, data[i], this.joinCoords([coords, i]));

tests/dataValidation.test.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,96 @@ test.skip('anyOf in object property', () => {
361361
// :TODO:
362362
});
363363

364+
test('test oneOf in array items', () => {
365+
let schema = {
366+
"type": "array",
367+
"items": {
368+
"oneOf": [
369+
{
370+
"properties": {
371+
"name": {"type": "string", "required": true}
372+
}
373+
},
374+
{
375+
"type": "string"
376+
}
377+
]
378+
}
379+
};
380+
381+
let wrong_data;
382+
let data;
383+
let validator = new DataValidator(schema);
384+
385+
/* :TODO: uncommen this block when oneOf validation has been implemented.
386+
387+
// 1. wrong type
388+
wrong_data = [1, 2];
389+
expect(validator.validate(wrong_data).isValid).toBe(false);
390+
391+
// 2. empty object required key
392+
wrong_data = [{'name': ''}];
393+
expect(validator.validate(wrong_data).isValid).toBe(false);
394+
395+
// 3. wrong object key type
396+
wrong_data = [{'name': 123}];
397+
expect(validator.validate(wrong_data).isValid).toBe(false);
398+
*/
399+
400+
// 2. object items
401+
data = [{'name': 'john'}];
402+
expect(validator.validate(data).isValid).toBe(true);
403+
404+
// 3. string items
405+
data = ['hello'];
406+
expect(validator.validate(data).isValid).toBe(true);
407+
});
408+
409+
test('test anyOf in array items', () => {
410+
let schema = {
411+
"type": "array",
412+
"items": {
413+
"anyOf": [
414+
{
415+
"properties": {
416+
"name": {"type": "string", "required": true}
417+
}
418+
},
419+
{
420+
"type": "string"
421+
}
422+
]
423+
}
424+
};
425+
426+
let wrong_data;
427+
let data;
428+
let validator = new DataValidator(schema);
429+
430+
/* :TODO: uncommen this block when oneOf validation has been implemented.
431+
432+
// 1. wrong type
433+
wrong_data = [1, 2];
434+
expect(validator.validate(wrong_data).isValid).toBe(false);
435+
436+
// 2. empty object required key
437+
wrong_data = [{'name': ''}];
438+
expect(validator.validate(wrong_data).isValid).toBe(false);
439+
440+
// 3. wrong object key type
441+
wrong_data = [{'name': 123}];
442+
expect(validator.validate(wrong_data).isValid).toBe(false);
443+
*/
444+
445+
// 2. object items
446+
data = [{'name': 'john'}];
447+
expect(validator.validate(data).isValid).toBe(true);
448+
449+
// 3. string items
450+
data = ['hello'];
451+
expect(validator.validate(data).isValid).toBe(true);
452+
});
453+
364454
test('validateString method', () => {
365455
let schema = {
366456
'type': 'object',

0 commit comments

Comments
 (0)