-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
24bc57e
commit b888a00
Showing
3 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/com/networknt/schema/DependentRequiredTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.networknt.schema; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.empty; | ||
|
||
class DependentRequiredTest { | ||
|
||
public static final String SCHEMA = | ||
"{ " + | ||
" \"$schema\":\"http://json-schema.org/draft/2019-09/schema\"," + | ||
" \"type\": \"object\"," + | ||
" \"properties\": {" + | ||
" \"optional\": \"string\"," + | ||
" \"requiredWhenOptionalPresent\": \"string\"" + | ||
" }," + | ||
" \"dependentRequired\": {" + | ||
" \"optional\": [ \"requiredWhenOptionalPresent\" ]," + | ||
" \"otherOptional\": [ \"otherDependentRequired\" ]" + | ||
" }" + | ||
"}"; | ||
|
||
private static final JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909); | ||
private static final JsonSchema schema = factory.getSchema(SCHEMA); | ||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@Test | ||
void shouldReturnNoErrorMessagesForObjectWithoutOptionalField() throws IOException { | ||
|
||
Set<ValidationMessage> messages = whenValidate("{}"); | ||
|
||
assertThat(messages, empty()); | ||
} | ||
|
||
@Test | ||
void shouldReturnErrorMessageForObjectWithoutDependentRequiredField() throws IOException { | ||
|
||
Set<ValidationMessage> messages = whenValidate("{ \"optional\": \"present\" }"); | ||
|
||
assertThat( | ||
messages.stream().map(ValidationMessage::getMessage).collect(Collectors.toList()), | ||
contains("$: has a missing property \"requiredWhenOptionalPresent\" which is dependent required because \"optional\" is present")); | ||
} | ||
|
||
@Test | ||
void shouldReturnNoErrorMessagesForObjectWithOptionalAndDependentRequiredFieldSet() throws JsonProcessingException { | ||
|
||
Set<ValidationMessage> messages = | ||
whenValidate("{ \"optional\": \"present\", \"requiredWhenOptionalPresent\": \"present\" }"); | ||
|
||
assertThat(messages, empty()); | ||
} | ||
|
||
private static Set<ValidationMessage> whenValidate(String content) throws JsonProcessingException { | ||
return schema.validate(mapper.readTree(content)); | ||
} | ||
|
||
} |