Allows to create a Validator object and to set a number of restrictions on it so that later check wheather the object to be checked is valid or not.
var validator = new Validator();
Validator can check three types of objects: string, integer and map. Let's call it schemas. Each schema is set by consequent method.
var strVal = validator.string(); var intVal = validator.integer(); var mapVal = validator.map();
Now we can set restrictions (will be described later) to our specified validator and then call method "isValid( object)" to check if our object fits restrictions.
String:
- required() - means that our object can't be null or empty string
- minLength(int length) - sets min length of the object
- contains(String chars) - requires that our object contains this chars
Restrictions are set by consequent method calls. The methods can be called as chain: strVal.required().minLengs(3).contains("abc");
This line means that the string we will check can't be null or "", must have in length 3 or more characters and contain "abc".
Integer:
- required() - can't be null
- positive() - must be > 0 or null
- range(int start, int end) - must be between start and end inclusive
Map:
- required() - can't be null
- sizeof(int size) - must be of this size
- shape(Map<String, schema> schema) - is for nested structures. Schema's value contains a string schema with already set number of restrictions. Method "isValid(Map<String, String> object)" takes as argument a Map<String, String>. If schema's key equals object's key and object's value is validated by schema's value, then shape method will return "true".
var validator = new Validator();
var numVal = validator.integer();
var mapVal = validator.map();
numVal.required().positive().range(4, 6);
var numSchema = Map.of("number", numVal);
mapVal.shape(numSchema);
var num1 = Map.of("num", 5);
var num2 = Map.of("num", 3);
mapVal.isValid(num1); // true
mapVal.isValid(num2); // false
- isValid method can be called in chain:
var res = validator.integer().required().positive().range(1, 6).isValid(5); // true
- a specified validator also remembers previous restrictions and renews them if consequent methods are called:
var intVal = validator.integer();
intVal.range(-5, -3);
intVal.isValid(-4); // true
intVal.positive();
intVal.isValid(4); // false
intVal.range(-5, 5);
intVal.isValid(4); // true