Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
Constraint validations
Validating user input is a common requirement in most applications. This can prevent user entry errors before the app attempts to process the data.
The constraint
library provides such validations for the following Ballerina types: int
, float
, decimal
, string
, and anydata[]
.
For more information on the underlying module, see the constraint
module.
import ballerina/constraint;
// Constraint on the `int` type.
@constraint:Int {
minValue: 18
}
type Age int;
type Student record {|
// Constraint on the `string`-typed record field.
@constraint:String {
pattern: re`[0-9]{6}[A-Z|a-z]`
}
string id;
string name;
// Constrained type used as a record field.
Age age;
// Constraint on the `string[]`-typed record field.
@constraint:Array {
minLength: 1,
maxLength: 10
}
string[] subjects;
|};
public function main() returns error? {
Student student = {
id: "200146B",
name: "David John",
age: 25,
subjects: ["Maths", "Science", "English"]
};
// To validate the constraints on the `Student` record, the `validate` function should be
// called explicitly. If the validation is successful, then, this function returns the type
// descriptor of the value that is validated.
student = check constraint:validate(student);
// Set the student's age to 17, which will violate the `minValue` constraint on `Age`.
student.age = 17;
// When the validation fails, the `validate` function returns a `constraint:Error`.
student = check constraint:validate(student);
}
$ bal run constraint_validations.balerror: Validation failed for '$.age:minValue' constraint(s).
PreviousHandle CSV with custom configurations
NextHello world