Data Validator is a tool for checking the accuracy and consistency of values during runtime. It allows users to create a schema to define, transform, and verify values, ensuring that the values meet the desired format and requirements. This validator is able to check the correctnes of ints, strings and maps.
Stack: OOP, SOLID, JUnit, TDD, DSL.
- required - any number including zero
- positive - positive number
- range - the range in which the numbers should fall, including borders.
import hexlet.code.Validator;
import hexlet.code.schemas.NumberSchema;
Validator v = new Validator();
NumberSchema schema = v.number();
// Until the required() method is called, null is considered valid
schema.isValid(null); // true
schema.positive().isValid(null); // true
schema.required();
schema.isValid(null); // false
schema.isValid(10) // true
schema.isValid("5"); // false
schema.isValid(-10); // false
// Zero is not a positive number
schema.isValid(0); // false
schema.range(5, 10);
schema.isValid(5); // true
schema.isValid(10); // true
schema.isValid(4); // false
schema.isValid(11); // false
- required - any non-empty string
- minLength - the string is equal to or longer than the specified number
- contains - the string contains a specific substring
import hexlet.code.Validator;
import hexlet.code.schemas.StringSchema;
Validator v = new Validator();
StringSchema schema = v.string();
schema.isValid(""); // true
schema.isValid(null); // true
schema.required();
schema.isValid("what does the fox say"); // true
schema.isValid("hexlet"); // true
schema.isValid(null); // false
schema.isValid(5); // false
schema.isValid(""); // false
schema.contains("wh").isValid("what does the fox say"); // true
schema.contains("what").isValid("what does the fox say"); // true
schema.contains("whatthe").isValid("what does the fox say"); // false
schema.isValid("what does the fox say"); // false
// already false, as one more check contains("whatthe") has been added
- required - Map is required
- sizeof - the number of key-value pairs in the Map must be equal to the specified number
import hexlet.code.Validator;
import hexlet.code.schemas.MapSchema;
Validator v = new Validator();
MapSchema schema = v.map();
schema.isValid(null); // true
schema.required();
schema.isValid(null) // false
schema.isValid(new HashMap()); // true
Map<String, String> data = new HashMap<>();
data.put("key1", "value1");
schema.isValid(data); // true
schema.sizeof(2);
schema.isValid(data); // false
data.put("key2", "value2");
schema.isValid(data); // true
When working with complex data, it may be necessary to check not only the Map object itself, but also the data inside
- shape - allows to describe validation for Map object values by keys
Map<String, BaseSchema> schemas = new HashMap<>();
schemas.put("name", v.string().required());
schemas.put("age", v.number().positive());
schema.shape(schemas);
Map<String, Object> human1 = new HashMap<>();
human1.put("name", "Kolya");
human1.put("age", 100);
schema.isValid(human1); // true
Map<String, Object> human2 = new HashMap<>();
human2.put("name", "Maya");
human2.put("age", null);
schema.isValid(human2); // true
Map<String, Object> human3 = new HashMap<>();
human3.put("last name", "Korinevskaya");
human3.put("age", 50);
schema.isValid(human3); // false
Map<String, Object> human4 = new HashMap<>();
human4.put("name", "Valya");
human4.put("age", -5);
schema.isValid(human4); // false