Skip to content
This repository was archived by the owner on Feb 10, 2023. It is now read-only.

Test for list property that can contain nulls #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
*.ipr
*.iml
*.iws
.idea/

build
out
.gradle

.DS_Store
.DS_Store
Original file line number Diff line number Diff line change
@@ -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<ErrorMessage> errors = schema.validate(json);

assertTrue(errors.isEmpty());
}

}
5 changes: 5 additions & 0 deletions src/test/resources/list-multiple-types-valid-document.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"myList": [
{"name":"bob", "age": 36},
null,
{"name":"dave", "age":10}
]}
31 changes: 31 additions & 0 deletions src/test/resources/list-multiple-types.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
]
}
}
}
}