This project implements the function of creating a custom library for data validation depending on their type. The library supports three data types: Strings, Integers and Maps. Three schemas are available: StringSchema, NumberSchema and MapSchema.
git clone git@github.com:fedorovaea18/Data_Validator.git
cd Data_Validator/app
make install
make lint
make test
StringSchema
StringSchema has three validation methods:
-
required() — makes the fields required and limits the possibility to use null or empty String;
-
minLength() — adds a minimum length constraint for the String. The String must be equal or longer than a specified number. Requires an integer parameter of minimum length;
-
contains() — adds a String content constraint. The String must contain a substring passed in the method parameter.
Validator v = new Validator();
StringSchema schema = v.string();
schema.required().isValid(""); //false
schema.minLength(6).isValid("fox"); //false
schema.contains("what").isValid("what does the fox say"); // trueNumberSchema
NumberSchema has three validation methods:
-
required() — makes the fileds required and limits the possibility to use null;
-
positive() — adds a constraint to use negative numbers;
-
range() — adds a range constraint (inclusive). Requires two integer parameters of the first and the last numbers of range.
Validator v = new Validator();
NumberSchema schema = v.number();
schema.required().isValid(-10); //false
schema.positive().isValid(5); // true
schema.range(5, 10).isValid(4); //falseMapSchema
MapSchema has three validation methods:
-
required() — makes the fields required and limits the possibility to use null.
-
sizeof() — adds a map size constraint. The K-V count must be equal to the number passed in the method parameter.
-
shape() — adds constraints to map values. Accepts as a parameter a map of keys whose values need to be validated and schemas that would validate the values.
Validator v = new Validator();
MapSchema schema = v.map();
schema.required().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
Map<String, BaseSchema> schemas = new HashMap<>();
schemas.put("firstName", v.string().required());
schemas.put("lastName", v.string().required().minLength(2));
schemas.put("age", v.number().required().positive());
schema.shape(schemas);
Map<String, Object> human1 = new HashMap<>();
human1.put("firstName", "John");
human1.put("lastName", "Smith");
human1.put("age", 30);
schema.isValid(human1); //true
Map<String, Object> human2 = new HashMap<>();
human2.put("firstName", "Anna");
human2.put("lastName", "B");
human2.put("age", -5);
schema.isValid(human2); //false
Map<String, Object> human3 = new HashMap<>();
human3.put("firstName", "Bob");
human3.put("lastName", null);
human3.put("age", 45);
schema.isValid(human3); //false