Java LIVR validator

Language Independent Validation Rules (v2.0) - java implementation

License

License

GroupId

GroupId

com.github.gaborkolarovics
ArtifactId

ArtifactId

livr-core
Last Version

Last Version

1.5.1
Release Date

Release Date

Type

Type

jar
Description

Description

Java LIVR validator
Language Independent Validation Rules (v2.0) - java implementation
Project URL

Project URL

https://github.com/gaborkolarovics/livr-validator
Source Code Management

Source Code Management

https://github.com/gaborkolarovics/livr-validator/tree/master

Download livr-core

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.gaborkolarovics/livr-core/ -->
<dependency>
    <groupId>com.github.gaborkolarovics</groupId>
    <artifactId>livr-core</artifactId>
    <version>1.5.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.gaborkolarovics/livr-core/
implementation 'com.github.gaborkolarovics:livr-core:1.5.1'
// https://jarcasting.com/artifacts/com.github.gaborkolarovics/livr-core/
implementation ("com.github.gaborkolarovics:livr-core:1.5.1")
'com.github.gaborkolarovics:livr-core:jar:1.5.1'
<dependency org="com.github.gaborkolarovics" name="livr-core" rev="1.5.1">
  <artifact name="livr-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.gaborkolarovics', module='livr-core', version='1.5.1')
)
libraryDependencies += "com.github.gaborkolarovics" % "livr-core" % "1.5.1"
[com.github.gaborkolarovics/livr-core "1.5.1"]

Dependencies

compile (3)

Group / Artifact Type Version
org.apache.commons : commons-lang3 jar 3.11
com.googlecode.json-simple : json-simple jar 1.1.1
org.slf4j : slf4j-api jar 1.7.30

test (1)

Group / Artifact Type Version
junit : junit jar 4.13.1

Project Modules

There are no modules declared in this project.

livr-validator

build Quality Gate Status Maven Central

Lightweight validator supporting Language Independent Validation Rules Specification (LIVR)

Description

See 'LIVR Specification' for detailed documentation and list of supported rules.

Features:

  • Rules are declarative and language independent
  • Any number of rules for each field
  • Return together errors for all fields
  • Excludes all fields that do not have validation rules described
  • Has possibility to validatate complex hierarchical structures
  • Easy to describe and undersand rules
  • Returns understandable error codes(not error messages)
  • Easy to add own rules
  • Rules are be able to change results output ("trim", "nested_object", for example)
  • Multipurpose (user input validation, configs validation, contracts programming etc)

Usage

Dependency

maven

<dependency>
  <groupId>com.github.gaborkolarovics</groupId>
  <artifactId>livr-validator</artifactId>
  <version>1.5.1</version>
</dependency>

Gradle

implementation 'com.github.gaborkolarovics:livr-validator:1.5.1'

Code

Schema source

  • Simple string schema
@LivrSchema(schema = "{\"name\": \"required\", \"email\": \"required\"}")
public class SamplePOJO{
    private String name;
    private String email;
    // Getter.. Setter..
}
  • Classpath resource
@LivrSchema(schema = "classpath:schemas/samplePOJO.json")
public class SamplePOJO{
    private String name;
    private String email;
    // Getter.. Setter..
}
  • File resource
@LivrSchema(schema = "file:/path/of/schemas/samplePOJO.json")
public class SamplePOJO{
    private String name;
    private String email;
    // Getter.. Setter..
}

Aliases

@LivrSchema(schema = "{\"password\": \"strong_password\"}"
    aliases = { "{\"name\": \"strong_password\", \"rules\": {\"min_length\": 6}, \"error\": \"WEAK_PASSWORD\"}" }
    )
public class SamplePOJO{
    private String password;
    // Getter.. Setter..
}

Alias loader support file: and classpath: resource loading like schema.

Custom rule

  • Pojo
@LivrSchema(schema = "{\"name\": {\"my_length\": 50 }}"
	rules = { MyLength.class })
public class SamplePOJO{
    private String name;
    private String email;
    // Getter.. Setter..
}
  • CustomRule
public class MyLength implements Rule {

    @Override
    public Function<List<Object>, Function<FunctionKeeper, Object>> func() {

        return ruleDefinition -> {
            final Long maxLength = Long.valueOf(ruleDefinition.get(0) + "");

            return wrapper -> {
                if ((wrapper.getValue() == null) || (wrapper.getValue() + "").equals("")) {
                    return "";
                }

                final String value = wrapper.getValue() + "";
                if (value.length() > maxLength) {
                    return "MY_TOO_LONG";
                }
                wrapper.getFieldResultArr().add(value);
                return "";
            };
        };
    }

    @Override
    public String rule() {
        return "my_length";
    }

}

Extra rules

This package contains livr-extra-rules module. See README.md.

@LivrSchema(schema = "{\"id\": \"uuid\" }}"
	scanRulePackages = { "livr.validation.rules" })

License

This repository is licensed under the GNU Affero General Public License.

Author

  • Java (LIVR 2.0), maintainer vlbaluk (Vladislav Baluk)
  • javax annotation, maintainer Gábor Kolárovics

Versions

Version
1.5.1
1.5.0
1.4.2
1.4.1
1.4.0
1.3.1
1.3.0
1.2.1
1.2.0
1.1.0