- java.lang.Object
-
- com.erudika.para.core.validation.Constraint
-
public abstract class Constraint extends Object
Represents a validation constraint.- Author:
- Alex Bogdanovski [alex@erudika.com]
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static Constraintbuild(String cname, Map<String,Object> payload)Builds a new constraint from a given name and payload.static Constraintdigits(Number integer, Number fraction)static Constraintemail()The 'email' constraint - field must contain a valid email.static Constraintfalsy()The 'falsy' constraint - field value must not be equal to 'true'.static ConstraintfromAnnotation(Annotation anno)Builds a new constraint from the annotation data.static Constraintfuture()The 'future' constraint - field value must be aDateor a timestamp in the future.StringgetName()The constraint name.Map<String,Object>getPayload()The payload (a map).abstract booleanisValid(Object actualValue)Validates the given value against the this constraint.static booleanisValidConstraintName(String name)Returns true if that validator is the list of known validators.static booleanisValidConstraintType(Class<? extends Annotation> type)Returns true if that validator is the list of known validators.static booleanmatches(Class<? extends Annotation> anno, String consName)Verifies that the given annotation type corresponds to a known constraint.static Constraintmax(Number max)The 'max' constraint - field must contain a number smaller than or equal to max.static Constraintmin(Number min)The 'min' constraint - field must contain a number larger than or equal to min.static Constraintpast()The 'past' constraint - field value must be aDateor a timestamp in the past.static Constraintpattern(Object regex)The 'pattern' constraint - field must contain a value matching a regular expression.static Constraintrequired()The 'required' constraint - marks a field as required.voidsetName(String name)Sets the name of the constraint.voidsetPayload(Map<String,Object> payload)Sets the payload.static Constraintsize(Number min, Number max)The 'size' constraint - field must be aString,Map,Collectionor array with a given minimum and maximum length.static Constrainttruthy()The 'truthy' constraint - field value must be equal to 'true'.static Constrainturl()The 'url' constraint - field value must be a valid URL.
-
-
-
Method Detail
-
isValid
public abstract boolean isValid(Object actualValue)
Validates the given value against the this constraint.- Parameters:
actualValue- a value / property of an object- Returns:
- true if the value satisfies the constraint
-
getName
public String getName()
The constraint name.- Returns:
- a name
-
setName
public void setName(String name)
Sets the name of the constraint.- Parameters:
name- a name
-
setPayload
public void setPayload(Map<String,Object> payload)
Sets the payload.- Parameters:
payload- a map
-
matches
public static boolean matches(Class<? extends Annotation> anno, String consName)
Verifies that the given annotation type corresponds to a known constraint.- Parameters:
anno- annotation typeconsName- constraint name- Returns:
- true if known
-
fromAnnotation
public static Constraint fromAnnotation(Annotation anno)
Builds a new constraint from the annotation data.- Parameters:
anno- JSR-303 annotation instance- Returns:
- a new constraint
-
isValidConstraintName
public static boolean isValidConstraintName(String name)
Returns true if that validator is the list of known validators.- Parameters:
name- a name- Returns:
- true if validator is known
-
isValidConstraintType
public static boolean isValidConstraintType(Class<? extends Annotation> type)
Returns true if that validator is the list of known validators.- Parameters:
type- annotation class type- Returns:
- true if validator is known
-
required
public static Constraint required()
The 'required' constraint - marks a field as required.- Returns:
- constraint
-
min
public static Constraint min(Number min)
The 'min' constraint - field must contain a number larger than or equal to min.- Parameters:
min- the minimum value- Returns:
- constraint
-
max
public static Constraint max(Number max)
The 'max' constraint - field must contain a number smaller than or equal to max.- Parameters:
max- the maximum value- Returns:
- constraint
-
size
public static Constraint size(Number min, Number max)
The 'size' constraint - field must be aString,Map,Collectionor array with a given minimum and maximum length.- Parameters:
min- the minimum lengthmax- the maximum length- Returns:
- constraint
-
digits
public static Constraint digits(Number integer, Number fraction)
The 'digits' constraint - field must be aNumberorStringcontaining digits where the number of digits in the integral part is limited by 'integer', and the number of digits for the fractional part is limited by 'fraction'.- Parameters:
integer- the max number of digits for the integral partfraction- the max number of digits for the fractional part- Returns:
- constraint
-
pattern
public static Constraint pattern(Object regex)
The 'pattern' constraint - field must contain a value matching a regular expression.- Parameters:
regex- a regular expression- Returns:
- constraint
-
email
public static Constraint email()
The 'email' constraint - field must contain a valid email.- Returns:
- constraint
-
falsy
public static Constraint falsy()
The 'falsy' constraint - field value must not be equal to 'true'.- Returns:
- constraint
-
truthy
public static Constraint truthy()
The 'truthy' constraint - field value must be equal to 'true'.- Returns:
- constraint
-
future
public static Constraint future()
The 'future' constraint - field value must be aDateor a timestamp in the future.- Returns:
- constraint
-
past
public static Constraint past()
The 'past' constraint - field value must be aDateor a timestamp in the past.- Returns:
- constraint
-
url
public static Constraint url()
The 'url' constraint - field value must be a valid URL.- Returns:
- constraint
-
build
public static Constraint build(String cname, Map<String,Object> payload)
Builds a new constraint from a given name and payload.- Parameters:
cname- the constraint namepayload- the payload- Returns:
- constraint
-
-