-
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.
Resolves #798 Co-authored-by: Faron Dutton <faron.dutton@insightglobal.com>
- Loading branch information
Showing
8 changed files
with
140 additions
and
18 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
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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/networknt/schema/WriteOnlyValidator.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,34 @@ | ||
package com.networknt.schema; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
public class WriteOnlyValidator extends BaseJsonValidator { | ||
private static final Logger logger = LoggerFactory.getLogger(WriteOnlyValidator.class); | ||
|
||
private final boolean writeOnly; | ||
|
||
public WriteOnlyValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { | ||
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.WRITE_ONLY, validationContext); | ||
|
||
this.writeOnly = validationContext.getConfig().isWriteOnly(); | ||
logger.debug("Loaded WriteOnlyValidator for property {} as {}", parentSchema, "write mode"); | ||
parseErrorCode(getValidatorType().getErrorCodeKey()); | ||
} | ||
|
||
@Override | ||
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { | ||
debug(logger, node, rootNode, at); | ||
Set<ValidationMessage> errors= new HashSet<>(); | ||
if (this.writeOnly) { | ||
errors.add(buildValidationMessage(at)); | ||
} | ||
return errors; | ||
} | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
[ | ||
{ | ||
"description": "issue798", | ||
"schema": { | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"properties": { | ||
"a": { "type": "string" }, | ||
"b": { "type": "string", "readOnly": true }, | ||
"c": { "type": "string", "writeOnly": true }, | ||
"d": { "type": "string", "readOnly": true, "writeOnly": true } | ||
} | ||
}, | ||
"tests": [ | ||
{ | ||
"description": "default behavior", | ||
"data": { "a": "a string" }, | ||
"valid": true, | ||
"config": { "readOnly": true, "writeOnly": true } | ||
}, | ||
{ | ||
"description": "readonly behavior", | ||
"data": { "a": "a string", "b": "a string" }, | ||
"valid": false, | ||
"config": { "readOnly": true, "writeOnly": true }, | ||
"validationMessages": [ "$.b: is a readonly field, it cannot be changed" ] | ||
}, | ||
{ | ||
"description": "write-only behavior", | ||
"data": { "a": "a string", "c": "a string" }, | ||
"valid": false, | ||
"config": { "readOnly": true, "writeOnly": true }, | ||
"validationMessages": [ "$.c: is a write-only field, it cannot appear in the data" ] | ||
}, | ||
{ | ||
"description": "both behavior", | ||
"data": { "a": "a string", "d": "a string" }, | ||
"valid": false, | ||
"config": { "readOnly": true, "writeOnly": true }, | ||
"validationMessages": [ | ||
"$.d: is a readonly field, it cannot be changed", | ||
"$.d: is a write-only field, it cannot appear in the data" | ||
] | ||
} | ||
] | ||
} | ||
] |