constraint
Module constraint
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
validate
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
Annotations
constraint: Array
The annotation, which is used for the constraints of the anydata[]
type.
constraint: Float
The annotation, which is used for the constraints of the float
type.
constraint: Int
The annotation, which is used for the constraints of the int
type.
constraint: Number
The annotation, which is used for the constraints of the int
, float
, and decimal
types.
constraint: String
The annotation, which is used for the constraints of the string
type.
Records
constraint: ArrayConstraints
Represents the constraints associated with anydata[]
type.
Fields
- length int? - The number of members of the constrained type
- minLength int? - The inclusive lower bound of the number of members of the constrained type
- maxLength int? - The inclusive upper bound of the number of members of the constrained type
constraint: FloatConstraints
Represents the constraints associated with float
type.
Fields
- minValue float? - The inclusive lower bound of the constrained type
- maxValue float? - The inclusive upper bound of the constrained type
- minValueExclusive float? - The exclusive lower bound of the constrained type
- maxValueExclusive float? - The exclusive upper bound of the constrained type
constraint: IntConstraints
Represents the constraints associated with int
type.
Fields
- minValue int? - The inclusive lower bound of the constrained type
- maxValue int? - The inclusive upper bound of the constrained type
- minValueExclusive int? - The exclusive lower bound of the constrained type
- maxValueExclusive int? - The exclusive upper bound of the constrained type
constraint: NumberConstraints
Represents the constraints associated with int
, float
and decimal
types.
Fields
- minValue decimal? - The inclusive lower bound of the constrained type
- maxValue decimal? - The inclusive upper bound of the constrained type
- minValueExclusive decimal? - The exclusive lower bound of the constrained type
- maxValueExclusive decimal? - The exclusive upper bound of the constrained type
constraint: StringConstraints
Represents the constraints associated with string
type.
Fields
- length int? - The number of characters of the constrained
string
type
- minLength int? - The inclusive lower bound of the number of characters of the constrained
string
type
- maxLength int? - The inclusive upper bound of the number of characters of the constrained
string
type
- pattern RegExp? - The regular expression to be matched with the constrained
string
type
Errors
Import
import ballerina/constraint;
Metadata
Released date: almost 2 years ago
Version: 1.1.0
License: Apache-2.0
Compatibility
Platform: java11
Ballerina version: 2201.4.0
GraalVM compatible: Yes
Pull count
Total: 494681
Current verison: 19942
Weekly downloads
Keywords
constraint
validation
Contributors