diff --git a/src/dataValidation.js b/src/dataValidation.js index a195416..471abf1 100644 --- a/src/dataValidation.js +++ b/src/dataValidation.js @@ -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])); diff --git a/tests/dataValidation.test.js b/tests/dataValidation.test.js index 6fa45ab..ef83a4f 100644 --- a/tests/dataValidation.test.js +++ b/tests/dataValidation.test.js @@ -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',