Skip to content

MarkDementev/Data-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CI status:

Actions Status Maintainability Test Coverage

Overview

This project contains functionality for strings, numbers and maps validation. Map validation provides structure verification functionality.

How to use:

//create Validator object
Validator validator = new Validator();
//choose specific validation schema and create its object
StringSchema schema = validator.string();
NumberSchema schema = validator.number();
MapSchema schema = validator.map();
//form methods calling chain to fill schema by validation checks
//for example:
schema.required().minLength(5);
//use isValid(Object validatingObject) to validate
schema.isValid("Dementev"); // true

String validation methods:

required() — adds a constraint to the schema that prevents null or an empty string from being used as a value. String data type required;

minLength(int minLength) — adds a minimum length validation for the string. The string must be equal to or longer than the specified number;

contains(String textToContains) — adds a restriction on the content of the string. The string must contain a specific substring.

Integer validation methods:

required() — adds a constraint to the schema that prevents null or an empty string from being used as a value. Integer data type required;

positive() — adds a constraint - the number must be positive;

range(int newStartRange, int newEndRange) — adds a valid range that the value of a number must fall within, including bounds.

Map validation methods:

required() — adds a constraint to the schema that prevents null from being used as a value. Map data type required;

sizeof() — adds a limit to the size of the map. The number of key-value pairs in the Map object must be equal to the specified;

shape(Map<String, BaseSchema> newSchemas) - used to define the properties of a Map object and create a schema to validate their values.

Validation examples:

Validator v = new Validator();
// Strings validation:
StringSchema schema = v.string().required();

schema.isValid("what does the fox say"); // true
schema.isValid(""); // false
// Numbers validation:
NumberSchema schema = v.number().required().positive();

schema.isValid(-10); // false
schema.isValid(10); // true
// Map validation with structure verification functionality:
Map<String, BaseSchema> schemas = new HashMap<>();
schemas.put("name", v.string().required());
schemas.put("age", v.number().positive());

MapSchema schema = v.map().sizeof(2).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", "");
human2.put("age", null);
schema.isValid(human1); // false

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published