Skip to content

Instantly share code, notes, and snippets.

@tmichel
Created August 28, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmichel/58507b9350220d83867a2ec2f429fcaa to your computer and use it in GitHub Desktop.
Save tmichel/58507b9350220d83867a2ec2f429fcaa to your computer and use it in GitHub Desktop.
Schema validation example
package io.sspinc.validator.example;
import io.sspinc.validator.Schema;
import io.sspinc.validator.ValidationErrors;
import static io.sspinc.validator.Validators.allowNull;
import static io.sspinc.validator.Validators.inclusion;
import static io.sspinc.validator.Validators.length;
import static io.sspinc.validator.Validators.presence;
public class Main {
public static void main(String[] args) {
Person person = new Person("", 32, null); //new Address());
ValidationErrors errors = getPersonSchema().validate(person);
errors.fullMessages().forEach(System.out::println);
}
private static Schema<Person> getPersonSchema() {
Schema<Address> addressSchema = Schema.builder(Address.class)
.add(Address::getCountry, inclusion("Hungary", "Germany"))
.build();
return Schema.builder(Person.class)
.add(Person::getName, presence())
.add(Person::getName, length(2, 12))
.add(Person::getAddress, allowNull(addressSchema))
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment