-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changing ReadOnlyValidator to use boolean property instead of array.
Including the concept of read/write modes.
- Loading branch information
1 parent
dcd6749
commit cb248c3
Showing
5 changed files
with
172 additions
and
102 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
60 changes: 60 additions & 0 deletions
60
src/test/java/com/networknt/schema/ReadOnlyValidatorTest.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,60 @@ | ||
package com.networknt.schema; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
class ReadOnlyValidatorTest { | ||
|
||
@Test | ||
void givenConfigWriteFalseWhenReadOnlyTrueThenAllows() throws IOException { | ||
ObjectNode node = getJsonNode(); | ||
Set<ValidationMessage> errors = loadJsonSchema(false).validate(node); | ||
assertTrue(errors.isEmpty()); | ||
} | ||
|
||
@Test | ||
void givenConfigWriteTrueWhenReadOnlyTrueThenDenies() throws IOException { | ||
ObjectNode node = getJsonNode(); | ||
Set<ValidationMessage> errors = loadJsonSchema(true).validate(node); | ||
assertFalse(errors.isEmpty()); | ||
assertEquals("$.firstName: is a readonly field, it cannot be changed", | ||
errors.stream().map(e -> e.getMessage()).toList().get(0)); | ||
} | ||
|
||
private JsonSchema loadJsonSchema(Boolean write) { | ||
JsonSchema schema = this.getJsonSchema(write); | ||
schema.initializeValidators(); | ||
return schema; | ||
|
||
} | ||
|
||
private JsonSchema getJsonSchema(Boolean write) { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); | ||
SchemaValidatorsConfig schemaConfig = createSchemaConfig(write); | ||
InputStream schema = getClass().getClassLoader().getResourceAsStream("schema/read-only-schema.json"); | ||
return factory.getSchema(schema, schemaConfig); | ||
} | ||
|
||
private SchemaValidatorsConfig createSchemaConfig(Boolean write) { | ||
SchemaValidatorsConfig config = new SchemaValidatorsConfig(); | ||
config.setWriteMode(write); | ||
return config; | ||
} | ||
|
||
private ObjectNode getJsonNode() throws IOException { | ||
InputStream node = getClass().getClassLoader().getResourceAsStream("data/read-only-data.json"); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
return (ObjectNode) mapper.readTree(node); | ||
} | ||
|
||
} |
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,4 @@ | ||
{ | ||
"firstName": "George", | ||
"lastName": "Harrison" | ||
} |
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,15 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "Read Only Schema", | ||
"description": "Testing Read Only Schema Validation", | ||
"type": "object", | ||
"properties": { | ||
"firstName": { | ||
"type": "string", | ||
"readOnly": true | ||
}, | ||
"lastName": { | ||
"type": "string" | ||
} | ||
} | ||
} |