public class JSONValidator extends Object implements Validator
In this example the validator itself is stored in a JSON text file as well. This is not
a requirement, you can build the validator in code as well.
The validator is composed of predicates which can be found in the package com.sdicons.json.validator.predicates
final JSONParser parser = new JSONParser(ValidatorTest.class.getResourceAsStream("/rules/range-validator.json"));
final Validator validator = new JSONValidator((JSONObject) parser.nextValue());
validator.validate(new JSONInteger(new BigDecimal("5")));
The validator file looks like this. It is a range check.
{
"name" :"Range Test",
"type" :"range",
"min" : 50,
"max" : 100
}
You can also use the ValidatorBuilder
to create the validators programmatically.
Validator v = vb.range(50, 100);
p = new JSONParser("55");
json = p.nextValue();
v.validate(json);
Constructor and Description |
---|
JSONValidator(JSONObject aValidation)
Construct the validator based on the JSON description.
|
public JSONValidator(JSONObject aValidation) throws ValidationException
aValidation
- The JSON description of the validator.ValidationException
- If the JSON description did not represent a validator.public void validate(JSONValue aValue) throws ValidationException
validate
in interface Validator
aValue
- ValidationException
Copyright © 2012. All Rights Reserved.