Skip to content

Commit

Permalink
FIx #59: Validation for oneOf/anyOf inside array items
Browse files Browse the repository at this point in the history
  • Loading branch information
bhch committed Mar 24, 2023
1 parent 6c40eed commit 8f33985
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/dataValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ export default function DataValidator(schema) {

let next_validator = this.getValidator(next_type);

if (!next_validator) {
if (next_schema.hasOwnProperty('oneOf')) {
next_validator = this.validateOneOf;
} else if (next_schema.hasOwnProperty('anyOf')) {
next_validator = this.validateAnyOf;
} else if (next_schema.hasOwnProperty('anyOf')) {
// currently allOf is not supported in array items
// next_validator = this.validateAllOf;
}
}

if (next_validator) {
for (let i = 0; i < data.length; i++)
next_validator(next_schema, data[i], this.joinCoords([coords, i]));
Expand Down
90 changes: 90 additions & 0 deletions tests/dataValidation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,96 @@ test.skip('anyOf in object property', () => {
// :TODO:
});

test('test oneOf in array items', () => {
let schema = {
"type": "array",
"items": {
"oneOf": [
{
"properties": {
"name": {"type": "string", "required": true}
}
},
{
"type": "string"
}
]
}
};

let wrong_data;
let data;
let validator = new DataValidator(schema);

/* :TODO: uncommen this block when oneOf validation has been implemented.
// 1. wrong type
wrong_data = [1, 2];
expect(validator.validate(wrong_data).isValid).toBe(false);
// 2. empty object required key
wrong_data = [{'name': ''}];
expect(validator.validate(wrong_data).isValid).toBe(false);
// 3. wrong object key type
wrong_data = [{'name': 123}];
expect(validator.validate(wrong_data).isValid).toBe(false);
*/

// 2. object items
data = [{'name': 'john'}];
expect(validator.validate(data).isValid).toBe(true);

// 3. string items
data = ['hello'];
expect(validator.validate(data).isValid).toBe(true);
});

test('test anyOf in array items', () => {
let schema = {
"type": "array",
"items": {
"anyOf": [
{
"properties": {
"name": {"type": "string", "required": true}
}
},
{
"type": "string"
}
]
}
};

let wrong_data;
let data;
let validator = new DataValidator(schema);

/* :TODO: uncommen this block when oneOf validation has been implemented.
// 1. wrong type
wrong_data = [1, 2];
expect(validator.validate(wrong_data).isValid).toBe(false);
// 2. empty object required key
wrong_data = [{'name': ''}];
expect(validator.validate(wrong_data).isValid).toBe(false);
// 3. wrong object key type
wrong_data = [{'name': 123}];
expect(validator.validate(wrong_data).isValid).toBe(false);
*/

// 2. object items
data = [{'name': 'john'}];
expect(validator.validate(data).isValid).toBe(true);

// 3. string items
data = ['hello'];
expect(validator.validate(data).isValid).toBe(true);
});

test('validateString method', () => {
let schema = {
'type': 'object',
Expand Down

0 comments on commit 8f33985

Please sign in to comment.