Skip to content

KarUrals/java-educational-project-3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 

Repository files navigation

Data validator

This project is a library that can be used for check the correctness (validation) of data. Library allows the following checks:

  • String
    • required - any non-empty string
    • minLength - the string is equal to or longer than the specified number
    • contains - the string contains a specific substring
  • Number
    • required - any number including zero
    • positive - positive number
    • range - the range in which the number should fall, including borders
  • Map (nested validation support)
    • required - the Map data type is required
    • sizeof - the number of key-value pairs in the Map must be equal to the specified

Service badges

Description Badge
Hexlet tests and linter status: Actions Status
GitHub Actions workflow status: Java CI
CodeClimate maintainability status: Maintainability
CodeClimate TestCoverage status: Test Coverage

Usage example

import hexlet.code.Validator;
import hexlet.code.schemas.StringSchema;
import hexlet.code.schemas.NumberSchema;
import hexlet.code.schemas.MapSchema;
import hexlet.code.schemas.BaseSchema;

Validator v = new Validator();

// Strings
StringSchema schema = v.string().required();

schema.isValid("what does the fox say"); // true
schema.isValid(""); // false

// Numbers
NumberSchema schema = v.number().required().positive();

schema.isValid(-10); // false
schema.isValid(10); // true

// Map object with structure checking support
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

Frequently used commands

Install

make install

Run

make run-dist

Build

make build

Run checkstyle

make lint

Check for Dependency Updates

make check-updates