constraint
![](https://bcentral-packageicons.azureedge.net/images/ballerina_constraint_1.5.0.png)
ballerina/constraint Ballerina library
Overview
This module provides features to validate the values that have been assigned to Ballerina types.
The Ballerina constraint
module facilitates APIs to do validations on the Ballerina types further with the use of annotations.
Functions![](/images/permalink.svg)
validate![](/images/permalink.svg)
function validate(anydata value, typedesc<anydata> td) returns td|Error
Validates the provided value against the configured annotations. Additionally, if the type of the value is different from the expected return type then the value will be cloned with the contextually expected type before the validation.
Parameters
- value anydata - The
anydata
type value to be constrained
- td typedesc<anydata> (default <>) - The type descriptor of the value to be constrained
Return Type
- td|Error - The type descriptor of the value which is validated or else an
constraint:Error
in case of an error
Enums![](/images/permalink.svg)
constraint: DateOption![](/images/permalink.svg)
Represents the date option to be validated.
Members
Annotations![](/images/permalink.svg)
constraint: Array![](/images/permalink.svg)
The annotation, which is used for the constraints of the anydata[]
type.
constraint: Date![](/images/permalink.svg)
The annotation, which is used for the constraints of the Date
type, which is structurally equivalent
to the following record:
type Date record { int year; int month; int day; };
This annotation will enable validation on the date fields in the record. Additionally,
the option
field can be used to validate the date against the provided option.
constraint: Float![](/images/permalink.svg)
The annotation, which is used for the constraints of the float
type.
constraint: Int![](/images/permalink.svg)
The annotation, which is used for the constraints of the int
type.
constraint: Number![](/images/permalink.svg)
The annotation, which is used for the constraints of the int
, float
, and decimal
types.
constraint: String![](/images/permalink.svg)
The annotation, which is used for the constraints of the string
type.
Records![](/images/permalink.svg)
constraint: ArrayConstraints![](/images/permalink.svg)
Represents the constraints associated with anydata[]
type.
Fields
constraint: ConstraintRecord![](/images/permalink.svg)
Represents the constraint details as a record.
Fields
- value anydata - The value for the constraint
- message string - The message to be returned in case of the constraint violation
constraint: DateConstraints![](/images/permalink.svg)
Represents the constraints associated with Date
type.
Fields
- option DateOption|record {| value DateOption |}? - date option to be validated
- message string? - The message to be returned in case of the constraint violation
constraint: FloatConstraints![](/images/permalink.svg)
Represents the constraints associated with float
type.
Fields
constraint: IntConstraints![](/images/permalink.svg)
Represents the constraints associated with int
type.
Fields
constraint: NumberConstraints![](/images/permalink.svg)
Represents the constraints associated with int
, float
and decimal
types.
Fields
constraint: StringConstraints![](/images/permalink.svg)
Represents the constraints associated with string
type.
Fields
Errors![](/images/permalink.svg)
Import
import ballerina/constraint;
Metadata
Released date: 8 months ago
Version: 1.5.0
License: Apache-2.0
Compatibility
Platform: java17
Ballerina version: 2201.8.0
GraalVM compatible: Yes
Pull count
Total: 436795
Current verison: 85186
Weekly downloads
Keywords
constraint
validation
Contributors