public abstract class NumericKeywordValidator extends KeywordValidator
We separate validation in two: if both the keyword value and
instance value are integers which fit into a long,
we use that (for performance reasons). If one of them doesn't,
then we use BigDecimal instead (for accuracy reasons).
This means that extending this validator will require you to implement
two methods: validateLong(ValidationReport, JsonNode) and
validateDecimal(ValidationReport, JsonNode).
You will note that these two methods take a JsonNode as an
argument. Use the following methods to convert these to the appropriate
types:
| Modifier and Type | Field and Description |
|---|---|
private boolean |
isLong
Does the keyword value fits into a
long? |
protected JsonNode |
number
The keyword value
|
keyword, nodeFactory| Modifier | Constructor and Description |
|---|---|
protected |
NumericKeywordValidator(String keyword,
JsonNode schema)
Protected constructor
|
| Modifier and Type | Method and Description |
|---|---|
String |
toString() |
void |
validate(ValidationContext context,
ValidationReport report,
JsonNode instance)
Main validation method
|
protected abstract void |
validateDecimal(ValidationReport report,
JsonNode instance)
Method to be implemented by a numeric validator if either of the
keyword value or instance value do not fit into a
long |
protected abstract void |
validateLong(ValidationReport report,
JsonNode instance)
Method to be implemented by a numeric validator if both the keyword
value and instance value fit into a
long |
private static boolean |
valueIsLong(JsonNode node)
Test whether a numeric instance is a long
|
newMsg, validateInstanceprotected final JsonNode number
private final boolean isLong
long?protected abstract void validateLong(ValidationReport report, JsonNode instance)
longreport - the validation reportinstance - the instance to validateprotected abstract void validateDecimal(ValidationReport report, JsonNode instance)
longreport - the validation reportinstance - the instance to validatepublic final void validate(ValidationContext context, ValidationReport report, JsonNode instance)
This is where the test for long is done on both the keyword
value and instance value. According to the result,
this method will then call either validateLong(ValidationReport,
JsonNode) or validateDecimal(ValidationReport, JsonNode)
validate in class KeywordValidatorcontext - the contextreport - the validation reportinstance - the instance to validateprivate static boolean valueIsLong(JsonNode node)
We use both a test on the instance type and Jackson's JsonNode.canConvertToLong(). The first test is needed since the
latter method will also return true if the value is a decimal which
integral part fits into a long, and we don't want that.
node - the node to testpublic String toString()
toString in class KeywordValidatorCopyright © 2012. All Rights Reserved.