Constraints
Constraints define set of rules that are applied for validation of the Attribute
content as input. The Attribute
content is validated when the Attribute
is created or updated. Validation is controlled consistently by the platform and ensures that the Attribute
content is valid and can be used for further processing.
For more information about Attribute
types that support constraints, see Attributes.
Constraint properties
Every constraint is extended from the base class of BaseAttributeConstraint
which is abstracted from AttributeConstraint
.
The constraint has the following properties defined and inherited from the BaseAttributeConstraint
:
Property | Type | Short description | Required |
---|---|---|---|
description | string | Description of the constraint | No |
errorMessage | string | Error message to be displayed when the constraint is violated | No |
type | AttributeConstraintType | Type of the constraint | Yes |
data | AttributeConstraint | Data to be used for the constraint | Yes |
Supported constraints types
Supported constraint types are defined in the AttributeConstraintType
. The following content types are available and supported:
AttributeConstraintType | Class | Data |
---|---|---|
REGEXP | RegexpAttributeConstraint | string |
RANGE | RangeAttributeConstraint | RangeAttributeConstraintData |
DATETIME | DateTimeAttributeConstraint | DateTimeAttributeConstraintData |
Constraint type description and samples
RegExpAttributeConstraint
Use this constraint when the content needs to be validated against a regular expression.
- JSON
- Java
{
"description": "Alphanumeric Regex Constraint",
"errorMessage": "Only alphanumeric characters are allowed",
"type": "RegExp",
"data": "^[a-zA-Z0-9]*$"
}
AttributeConstraint constraint = new RegexpAttributeConstraint()
constraint.setDescription("Alphanumeric Regex Constraint")
constraint.setErrorMessage("Only alphanumeric characters are allowed")
constraint.setData("^[a-zA-Z0-9]*$");
constraint.setType(AttributeConstraintType.REGEXP);
RangeAttributeConstraint
Use this constraint when the content needs to be validated against a range of integers.
- JSON
- Java
{
"description": "Range Constraint",
"errorMessage": "Value should be between 1 and 10",
"type": "range",
"data": {
"from": 1,
"to": 10
}
}
RangeAttributeConstraintData data = new RangeAttributeConstraintData();
data.setFrom(1);
data.setTo(10);
RangeAttributeConstraint constraint = new RangeAttributeConstraint();
constraint.setDescription("Range Constraint");
constraint.setErrorMessage("Value should be between 1 and 10");
constraint.setData(data);
constraint.setType(AttributeConstraintType.RANGE);
DateTimeAttributeConstraint
Use this constraint when you need to validate date and time.
- JSON
- Java
{
"description": "Date Time Constraint",
"errorMessage": "Value should be between 1 and 10",
"type": "datetime",
"data": {
"from": "2020-01-01T00:00:00.000Z",
"to": "2020-12-31T00:00:00.000Z"
}
}
DateTimeAttributeConstraintData data = new DateTimeAttributeConstraintData();
data.setFrom(LocalDateTime.now());
data.setTo(LocalDateTime.of(2023, Month.JULY, 29, 19, 30, 40));
DateTimeAttributeConstraint constraint = new DateTimeAttributeConstraint();
constraint.setDescription("Date Time Constraint");
constraint.setErrorMessage("Value should be between the provided range");
constraint.setData(data);
constraint.setType(AttributeConstraintType.DATETIME);
Constraint model
The following diagram represents the constraint model inherited from the AttributeConstraint
. Details can be found in the CZERTAINLY Interfaces repository.