diff --git a/.gitignore b/.gitignore index 9bc0587..99e3304 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,10 @@ *.ipr *.iml *.iws +.idea/ build out .gradle -.DS_Store \ No newline at end of file +.DS_Store diff --git a/src/test/java/uk/co/o2/json/sample/ListOfMultipleTypesSchemaTest.java b/src/test/java/uk/co/o2/json/sample/ListOfMultipleTypesSchemaTest.java new file mode 100644 index 0000000..fd42691 --- /dev/null +++ b/src/test/java/uk/co/o2/json/sample/ListOfMultipleTypesSchemaTest.java @@ -0,0 +1,34 @@ +package uk.co.o2.json.sample; + + +import org.codehaus.jackson.JsonFactory; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.map.ObjectMapper; +import org.junit.Test; +import uk.co.o2.json.schema.ErrorMessage; +import uk.co.o2.json.schema.JsonSchema; +import uk.co.o2.json.schema.SchemaPassThroughCache; + +import java.util.List; + +import static org.junit.Assert.assertTrue; + +public class ListOfMultipleTypesSchemaTest { + + private JsonFactory jsonFactory = new JsonFactory(new ObjectMapper()); + private SchemaPassThroughCache schemaFactory = new SchemaPassThroughCache(jsonFactory); + + /* + Schema and json tested correctly on http://jsonschemalint.com/ + */ + @Test + public void canSuccessfullyValidateAListThatContainsObjectsAndSimpleTypes() throws Exception { + JsonSchema schema = schemaFactory.getSchema(getClass().getClassLoader().getResource("list-multiple-types.json")); + JsonNode json = jsonFactory.createJsonParser(getClass().getClassLoader().getResource("list-multiple-types-valid-document.json")).readValueAsTree(); + + List errors = schema.validate(json); + + assertTrue(errors.isEmpty()); + } + +} diff --git a/src/test/resources/list-multiple-types-valid-document.json b/src/test/resources/list-multiple-types-valid-document.json new file mode 100644 index 0000000..8821cbb --- /dev/null +++ b/src/test/resources/list-multiple-types-valid-document.json @@ -0,0 +1,5 @@ +{"myList": [ + {"name":"bob", "age": 36}, + null, + {"name":"dave", "age":10} +]} \ No newline at end of file diff --git a/src/test/resources/list-multiple-types.json b/src/test/resources/list-multiple-types.json new file mode 100644 index 0000000..85cb726 --- /dev/null +++ b/src/test/resources/list-multiple-types.json @@ -0,0 +1,31 @@ +{ + "type":"object", + "additionalProperties":false, + "properties":{ + "myList":{ + "type":"array", + "required":true, + "additionalItems":false, + "items":{ + "type":[ + "null", + { + "type":"object", + "additionalProperties":false, + "properties":{ + "name":{ + "type":"string", + "required":true + }, + "age":{ + "type":"number", + "required":true, + "minimum":0 + } + } + } + ] + } + } + } +}