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, long) and
validateDecimal(ValidationReport, BigDecimal).
| Modifier and Type | Field and Description |
|---|---|
protected BigDecimal |
decimalValue
The keyword value as a
BigDecimal |
private boolean |
isLong
Does the keyword value fits into a
long? |
protected long |
longValue
The keyword value coerced as a
long |
keyword| 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,
BigDecimal instanceValue)
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,
long instanceValue)
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
|
validateInstanceprotected final BigDecimal decimalValue
BigDecimalprotected final long longValue
longprivate final boolean isLong
long?protected abstract void validateLong(ValidationReport report, long instanceValue)
longreport - the validation reportinstanceValue - the instance value to validate as a longprotected abstract void validateDecimal(ValidationReport report, BigDecimal instanceValue)
longreport - the validation reportinstanceValue - the instance to validate as a BigDecimalpublic 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,
long) or validateDecimal(ValidationReport, BigDecimal).
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.