Skip to content

Matiika/Java-Data-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 

Repository files navigation

This is a data validator. It allows you to check variable values ​​for compliance with rules. Rule sets are selected by the user. There may be several rules.

If the variable follows all the rules, then true will be returned. If at least one rule is not followed, then it is false.

For example, for a variable with a string, you can create the following rules. Check if a string contains another string. Check if the string is greater than the specified length.

var v = new Validator();

        var schema = v.string();

        assertTrue(schema.isValid("")); // true
        assertTrue(schema.isValid(null)); // true

        schema.required();

        assertFalse(schema.isValid(null)); // false
        assertFalse(schema.isValid("")); // false
        assertTrue(schema.isValid("what does the fox say")); // true
        assertTrue(schema.isValid("hexlet")); // true

        assertTrue(schema.contains("wh").isValid("what does the fox say")); // true
        assertTrue(schema.contains("what").isValid("what does the fox say")); // true
        assertFalse(schema.contains("whatthe").isValid("what does the fox say")); // false

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

        var schema1 = v.string();
        assertTrue(schema1.minLength(10).minLength(4).isValid("Hexlet")); // true

For a variable with a numeric type, you can set the following rules. Check if a number is positive. Check if the number is within the range.

var v = new Validator();

        var schema = v.number();

        assertTrue(schema.isValid(5)); // true

        assertTrue(schema.isValid(null)); // true
        assertTrue(schema.positive().isValid(null)); // true

        schema.required();

        assertFalse(schema.isValid(null)); // false
        assertTrue(schema.isValid(10)); // true

        assertFalse(schema.isValid(-10)); // false
        assertFalse(schema.isValid(0)); // false

        schema.range(5, 10);

        assertTrue(schema.isValid(5)); // true
        assertTrue(schema.isValid(10)); // true
        assertFalse(schema.isValid(4)); // false
        assertFalse(schema.isValid(11)); // false

Hexlet tests and linter status:

Actions Status

Maintainability

Test Coverage

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published