From 275a7bf5fe335bb81e4e9a246d9dacf11f403ba3 Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Thu, 10 Oct 2024 10:13:51 +0530 Subject: [PATCH 1/6] Update spec --- docs/spec/spec.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/spec/spec.md b/docs/spec/spec.md index 7381e53..bc8b232 100644 --- a/docs/spec/spec.md +++ b/docs/spec/spec.md @@ -87,6 +87,7 @@ The following table illustrates all the supported annotations with respect to th | `int`|`float`|`decimal` | `@constraint:Number` | | `string` | `@constraint:String` | | `any[]` | `@constraint:Array` | +| `constraint:Date` | `@constraint:Date` | ### 2.1. Constraint annotation on number types @@ -135,9 +136,9 @@ All the supported constraints on number types are illustrated in the following t | maxValue | v <= c | | minValueExclusive | v > c | | maxValueExclusive | v < c | -|maxDigits | Number of digits in v <= c | -|maxIntegerDigits | Number of integer digits in v <= c | -|maxFractionDigits | Number of fraction digits in v <= c | +| maxDigits | Number of digits in v <= c | +| maxIntegerDigits | Number of integer digits in v <= c | +| maxFractionDigits | Number of fraction digits in v <= c | When defining constraints on number types, either `minValue` or `minValueExclusive` can be present. Similarly, either `maxValue` or `maxValueExclusive` can be present. From 2b1613c54e9484229b3a892af64b3e9176ef57d0 Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Thu, 10 Oct 2024 10:14:08 +0530 Subject: [PATCH 2/6] Update README --- README.md | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7231a65..09f8b61 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,119 @@ Ballerina Constraint Library [![GitHub Last Commit](https://img.shields.io/github/last-commit/ballerina-platform/module-ballerina-constraint.svg?label=Last%20Commit)](https://github.com/ballerina-platform/module-ballerina-constraint/commits/master) [![GitHub issues](https://img.shields.io/github/issues/ballerina-platform/ballerina-standard-library/module/constraint.svg?label=Open%20Issues)](https://github.com/ballerina-platform/ballerina-standard-library/labels/module%2Fconstraint) -This library provides features to validate the values that have been assigned to Ballerina types. - The Ballerina `constraint` library facilitates APIs to do validations on the Ballerina types further with the use of annotations. +### Constraint annotations + +This library provides the following annotations to validate the values that have been assigned to Ballerina types. + +| Ballerina Type | Annotation | Supported Constraints | +|-----------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------| +| `int` | `@constraint:Int` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | +| `float` | `@constraint:Float` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | +| `int`|`float`|`decimal` | `@constraint:Number` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | +| `string` | `@constraint:String` | `length`, `minLength`, `maxLength`, `pattern` | +| `any[]` | `@constraint:Array` | `length`, `minLength`, `maxLength` | +| `constraint:Date` | `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | + +The following example demonstrates how to apply constraint annotations to Ballerina types. + +```ballerina +@constraint:String { pattern: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$` } +public type Email string; + +@constraint:Date { option: constraint:PAST } +public type DOB time:Date; + +public type Person record {| + @constraint:String { minLength: 5, maxLength: 10 } + string name; + @constraint:Int { minValue: 18, maxValue: 60 } + int age; + Email email; + @constraint:Array { minLength: 1, maxLength: 5 } + string[] phoneNumbers; + DOB dob; +|}; +``` + +### Constraint validation + +The `validate` function in this library can be used to validate the values that have been assigned to Ballerina types with respect to the constraints defined using the annotations. + +The following example demonstrates how to validate the values assigned to a Ballerina type. + +```ballerina +public function main() returns error? { + Person person = { + name: "John", + age: 25, + email: "john@mail.com", + phoneNumbers: ["1234567890", "0987654321"], + dob: { year: 1996, month: 5, day: 15 } + }; + + Person|error personValidated = constraint:validate(person); + if personValidated is error { + io:println("Validation failed: " + personValidated.message()); + } else { + io:println("Validation successful"); + } +} +``` + +### Custom error messages on validation failures + +Optionally a custom error message can be provided for each constraint. The following example demonstrates how to provide custom error messages for constraints. + +```ballerina +@constraint:String { + pattern: { + value: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$`, + message: "Invalid email address" + } +} +public type Email string; + +@constraint:Date { + option: { + value: constraint:PAST, + message: "Date of birth should be in the past" + }, + message: "Invalid date found" +} +public type DOB time:Date; + +public type Person record {| + @constraint:String { + minLength: 5, + maxLength: { + value: 10, + message: "Name should be less than 10 characters" + } + } + string name; + @constraint:Int { + minValue: { + value: 18, + message: "Age should be greater than 18" + }, + maxValue: 60 + } + int age; + Email email; + @constraint:Array { + minLength: { + value: 1, + message: "At least one phone number should be provided" + }, + maxLength: 5 + } + string[] phoneNumbers; + DOB dob; +|}; +``` + ## Issues and projects Issues and Projects tabs are disabled for this repository as this is part of the Ballerina Standard Library. To report bugs, request new features, start new discussions, view project boards, etc., go to the [Ballerina Standard Library parent repository](https://github.com/ballerina-platform/ballerina-standard-library). From cc81587b8ba162ff54bb040ab4a714e2b5a4550f Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Thu, 10 Oct 2024 10:15:38 +0530 Subject: [PATCH 3/6] Update module and package md files --- ballerina/Module.md | 111 +++++++++++++++++++++++++++++++++++++++++++ ballerina/Package.md | 111 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+) diff --git a/ballerina/Module.md b/ballerina/Module.md index fe95668..a824dcd 100644 --- a/ballerina/Module.md +++ b/ballerina/Module.md @@ -3,3 +3,114 @@ 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. + +### Constraint annotations + +This library provides the following annotations to validate the values that have been assigned to Ballerina types. + +| Ballerina Type | Annotation | Supported Constraints | +|-----------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------| +| `int` | `@constraint:Int` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | +| `float` | `@constraint:Float` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | +| `int`|`float`|`decimal` | `@constraint:Number` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | +| `string` | `@constraint:String` | `length`, `minLength`, `maxLength`, `pattern` | +| `any[]` | `@constraint:Array` | `length`, `minLength`, `maxLength` | +| `constraint:Date` | `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | + +The following example demonstrates how to apply constraint annotations to Ballerina types. + +```ballerina +@constraint:String { pattern: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$` } +public type Email string; + +@constraint:Date { option: constraint:PAST } +public type DOB time:Date; + +public type Person record {| + @constraint:String { minLength: 5, maxLength: 10 } + string name; + @constraint:Int { minValue: 18, maxValue: 60 } + int age; + Email email; + @constraint:Array { minLength: 1, maxLength: 5 } + string[] phoneNumbers; + DOB dob; +|}; +``` + +### Constraint validation + +The `validate` function in this library can be used to validate the values that have been assigned to Ballerina types with respect to the constraints defined using the annotations. + +The following example demonstrates how to validate the values assigned to a Ballerina type. + +```ballerina +public function main() returns error? { + Person person = { + name: "John", + age: 25, + email: "john@mail.com", + phoneNumbers: ["1234567890", "0987654321"], + dob: { year: 1996, month: 5, day: 15 } + }; + + Person|error personValidated = constraint:validate(person); + if personValidated is error { + io:println("Validation failed: " + personValidated.message()); + } else { + io:println("Validation successful"); + } +} +``` + +### Custom error messages on validation failures + +Optionally a custom error message can be provided for each constraint. The following example demonstrates how to provide custom error messages for constraints. + +```ballerina +@constraint:String { + pattern: { + value: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$`, + message: "Invalid email address" + } +} +public type Email string; + +@constraint:Date { + option: { + value: constraint:PAST, + message: "Date of birth should be in the past" + }, + message: "Invalid date found" +} +public type DOB time:Date; + +public type Person record {| + @constraint:String { + minLength: 5, + maxLength: { + value: 10, + message: "Name should be less than 10 characters" + } + } + string name; + @constraint:Int { + minValue: { + value: 18, + message: "Age should be greater than 18" + }, + maxValue: 60 + } + int age; + Email email; + @constraint:Array { + minLength: { + value: 1, + message: "At least one phone number should be provided" + }, + maxLength: 5 + } + string[] phoneNumbers; + DOB dob; +|}; +``` diff --git a/ballerina/Package.md b/ballerina/Package.md index bb28aea..a617402 100644 --- a/ballerina/Package.md +++ b/ballerina/Package.md @@ -4,6 +4,117 @@ This package provides features to validate the values that have been assigned to The Ballerina `constraint` package facilitates APIs to do validations on the Ballerina types further with the use of annotations. +### Constraint annotations + +This library provides the following annotations to validate the values that have been assigned to Ballerina types. + +| Ballerina Type | Annotation | Supported Constraints | +|-----------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------| +| `int` | `@constraint:Int` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | +| `float` | `@constraint:Float` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | +| `int`|`float`|`decimal` | `@constraint:Number` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | +| `string` | `@constraint:String` | `length`, `minLength`, `maxLength`, `pattern` | +| `any[]` | `@constraint:Array` | `length`, `minLength`, `maxLength` | +| `constraint:Date` | `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | + +The following example demonstrates how to apply constraint annotations to Ballerina types. + +```ballerina +@constraint:String { pattern: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$` } +public type Email string; + +@constraint:Date { option: constraint:PAST } +public type DOB time:Date; + +public type Person record {| + @constraint:String { minLength: 5, maxLength: 10 } + string name; + @constraint:Int { minValue: 18, maxValue: 60 } + int age; + Email email; + @constraint:Array { minLength: 1, maxLength: 5 } + string[] phoneNumbers; + DOB dob; +|}; +``` + +### Constraint validation + +The `validate` function in this library can be used to validate the values that have been assigned to Ballerina types with respect to the constraints defined using the annotations. + +The following example demonstrates how to validate the values assigned to a Ballerina type. + +```ballerina +public function main() returns error? { + Person person = { + name: "John", + age: 25, + email: "john@mail.com", + phoneNumbers: ["1234567890", "0987654321"], + dob: { year: 1996, month: 5, day: 15 } + }; + + Person|error personValidated = constraint:validate(person); + if personValidated is error { + io:println("Validation failed: " + personValidated.message()); + } else { + io:println("Validation successful"); + } +} +``` + +### Custom error messages on validation failures + +Optionally a custom error message can be provided for each constraint. The following example demonstrates how to provide custom error messages for constraints. + +```ballerina +@constraint:String { + pattern: { + value: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$`, + message: "Invalid email address" + } +} +public type Email string; + +@constraint:Date { + option: { + value: constraint:PAST, + message: "Date of birth should be in the past" + }, + message: "Invalid date found" +} +public type DOB time:Date; + +public type Person record {| + @constraint:String { + minLength: 5, + maxLength: { + value: 10, + message: "Name should be less than 10 characters" + } + } + string name; + @constraint:Int { + minValue: { + value: 18, + message: "Age should be greater than 18" + }, + maxValue: 60 + } + int age; + Email email; + @constraint:Array { + minLength: { + value: 1, + message: "At least one phone number should be provided" + }, + maxLength: 5 + } + string[] phoneNumbers; + DOB dob; +|}; +``` + ## Report issues To report bugs, request new features, start new discussions, view project boards, etc., go to the [Ballerina standard library parent repository](https://github.com/ballerina-platform/ballerina-standard-library). From d30a43eca8463f89ed28fd35c2f6ee3d84b0b4c2 Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Mon, 14 Oct 2024 10:43:05 +0530 Subject: [PATCH 4/6] Add suggestions from review --- README.md | 10 +++++----- ballerina/Module.md | 10 +++++----- ballerina/Package.md | 10 +++++----- docs/spec/spec.md | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 09f8b61..dff39b1 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,11 @@ Ballerina Constraint Library [![GitHub Last Commit](https://img.shields.io/github/last-commit/ballerina-platform/module-ballerina-constraint.svg?label=Last%20Commit)](https://github.com/ballerina-platform/module-ballerina-constraint/commits/master) [![GitHub issues](https://img.shields.io/github/issues/ballerina-platform/ballerina-standard-library/module/constraint.svg?label=Open%20Issues)](https://github.com/ballerina-platform/ballerina-standard-library/labels/module%2Fconstraint) -The Ballerina `constraint` library facilitates APIs to do validations on the Ballerina types further with the use of annotations. +The Ballerina `constraint` library provides annotations to add constraints to Ballerina types and API to validate the values with respect to the constraints defined in the respective types. ### Constraint annotations -This library provides the following annotations to validate the values that have been assigned to Ballerina types. +This library provides the following annotations on Ballerina types to validate the values created with the respective types. | Ballerina Type | Annotation | Supported Constraints | |-----------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------| @@ -23,7 +23,7 @@ This library provides the following annotations to validate the values that have | `any[]` | `@constraint:Array` | `length`, `minLength`, `maxLength` | | `constraint:Date` | `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | -The following example demonstrates how to apply constraint annotations to Ballerina types. +The following example demonstrates how to apply constraint annotations to types. ```ballerina @constraint:String { pattern: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$` } @@ -46,9 +46,9 @@ public type Person record {| ### Constraint validation -The `validate` function in this library can be used to validate the values that have been assigned to Ballerina types with respect to the constraints defined using the annotations. +The `validate` function in this library can be used to validate the values with respect to the constraints defined in the respective types. -The following example demonstrates how to validate the values assigned to a Ballerina type. +The following example demonstrates how to validate a value with respect to constraints in the type. The respective type is automatically inferred from the expression context. ```ballerina public function main() returns error? { diff --git a/ballerina/Module.md b/ballerina/Module.md index a824dcd..151ac66 100644 --- a/ballerina/Module.md +++ b/ballerina/Module.md @@ -1,12 +1,12 @@ ## Overview -This module provides features to validate the values that have been assigned to Ballerina types. +This module provides features to validate the values with respect to the constraints defined to the respective Ballerina types. The Ballerina `constraint` module facilitates APIs to do validations on the Ballerina types further with the use of annotations. ### Constraint annotations -This library provides the following annotations to validate the values that have been assigned to Ballerina types. +This library provides the following annotations on Ballerina types to validate the values created with the respective types. | Ballerina Type | Annotation | Supported Constraints | |-----------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------| @@ -17,7 +17,7 @@ This library provides the following annotations to validate the values that have | `any[]` | `@constraint:Array` | `length`, `minLength`, `maxLength` | | `constraint:Date` | `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | -The following example demonstrates how to apply constraint annotations to Ballerina types. +The following example demonstrates how to apply constraint annotations to types. ```ballerina @constraint:String { pattern: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$` } @@ -40,9 +40,9 @@ public type Person record {| ### Constraint validation -The `validate` function in this library can be used to validate the values that have been assigned to Ballerina types with respect to the constraints defined using the annotations. +The `validate` function in this library can be used to validate the values with respect to the constraints defined in the respective types. -The following example demonstrates how to validate the values assigned to a Ballerina type. +The following example demonstrates how to validate a value with respect to constraints in the type. The respective type is automatically inferred from the expression context. ```ballerina public function main() returns error? { diff --git a/ballerina/Package.md b/ballerina/Package.md index a617402..1521131 100644 --- a/ballerina/Package.md +++ b/ballerina/Package.md @@ -1,12 +1,12 @@ ## Package overview -This package provides features to validate the values that have been assigned to Ballerina types. +This package provides features to validate the values with respect to the constraints defined to the respective Ballerina types. The Ballerina `constraint` package facilitates APIs to do validations on the Ballerina types further with the use of annotations. ### Constraint annotations -This library provides the following annotations to validate the values that have been assigned to Ballerina types. +This library provides the following annotations on Ballerina types to validate the values created with the respective types. | Ballerina Type | Annotation | Supported Constraints | |-----------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------| @@ -17,7 +17,7 @@ This library provides the following annotations to validate the values that have | `any[]` | `@constraint:Array` | `length`, `minLength`, `maxLength` | | `constraint:Date` | `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | -The following example demonstrates how to apply constraint annotations to Ballerina types. +The following example demonstrates how to apply constraint annotations to types. ```ballerina @constraint:String { pattern: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$` } @@ -40,9 +40,9 @@ public type Person record {| ### Constraint validation -The `validate` function in this library can be used to validate the values that have been assigned to Ballerina types with respect to the constraints defined using the annotations. +The `validate` function in this library can be used to validate the values with respect to the constraints defined in the respective types. -The following example demonstrates how to validate the values assigned to a Ballerina type. +The following example demonstrates how to validate a value with respect to constraints in the type. The respective type is automatically inferred from the expression context. ```ballerina public function main() returns error? { diff --git a/docs/spec/spec.md b/docs/spec/spec.md index bc8b232..33ea565 100644 --- a/docs/spec/spec.md +++ b/docs/spec/spec.md @@ -8,7 +8,7 @@ _Edition_: Swan Lake ## Introduction This is the specification for the Constraint standard library of [Ballerina language](https://ballerina.io/), which -provides APIs to validate the values that have been assigned to Ballerina types. +provides annotations to add constraints to Ballerina types and API to validate the values with respect to the constraints defined in the respective types. The Constraint library specification has evolved and may continue to evolve in the future. The released versions of the specification can be found under the relevant GitHub tag. From 9c58851bfdca933e13b09490135ee6b9b64025de Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Mon, 14 Oct 2024 12:36:49 +0530 Subject: [PATCH 5/6] Add suggestions from review --- README.md | 2 +- docs/spec/spec.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dff39b1..bb5636f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Ballerina Constraint Library [![GitHub Last Commit](https://img.shields.io/github/last-commit/ballerina-platform/module-ballerina-constraint.svg?label=Last%20Commit)](https://github.com/ballerina-platform/module-ballerina-constraint/commits/master) [![GitHub issues](https://img.shields.io/github/issues/ballerina-platform/ballerina-standard-library/module/constraint.svg?label=Open%20Issues)](https://github.com/ballerina-platform/ballerina-standard-library/labels/module%2Fconstraint) -The Ballerina `constraint` library provides annotations to add constraints to Ballerina types and API to validate the values with respect to the constraints defined in the respective types. +The Ballerina `constraint` library provides annotations to add constraints to Ballerina types and an API to validate the values with respect to the constraints defined in the respective types. ### Constraint annotations diff --git a/docs/spec/spec.md b/docs/spec/spec.md index 33ea565..a76c31b 100644 --- a/docs/spec/spec.md +++ b/docs/spec/spec.md @@ -8,7 +8,7 @@ _Edition_: Swan Lake ## Introduction This is the specification for the Constraint standard library of [Ballerina language](https://ballerina.io/), which -provides annotations to add constraints to Ballerina types and API to validate the values with respect to the constraints defined in the respective types. +provides annotations to add constraints to Ballerina types and an API to validate the values with respect to the constraints defined in the respective types. The Constraint library specification has evolved and may continue to evolve in the future. The released versions of the specification can be found under the relevant GitHub tag. From 84179f42d6e2dd50ce86dda55d71e439f1a0c03c Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Mon, 14 Oct 2024 14:49:56 +0530 Subject: [PATCH 6/6] Update table according to the suggestion --- README.md | 17 +++++++++-------- ballerina/Module.md | 16 ++++++++-------- ballerina/Package.md | 16 ++++++++-------- docs/spec/spec.md | 16 ++++++++-------- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index bb5636f..323b699 100644 --- a/README.md +++ b/README.md @@ -14,14 +14,15 @@ The Ballerina `constraint` library provides annotations to add constraints to Ba This library provides the following annotations on Ballerina types to validate the values created with the respective types. -| Ballerina Type | Annotation | Supported Constraints | -|-----------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------| -| `int` | `@constraint:Int` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | -| `float` | `@constraint:Float` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | -| `int`|`float`|`decimal` | `@constraint:Number` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | -| `string` | `@constraint:String` | `length`, `minLength`, `maxLength`, `pattern` | -| `any[]` | `@constraint:Array` | `length`, `minLength`, `maxLength` | -| `constraint:Date` | `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | +| Annotation | Supported Constraints | Associated Ballerina Type(s) | +|----------------------|------------------------------------------------------------------------------------------------------------------------|------------------------------| +| `@constraint:Int` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `int` | +| `@constraint:Float` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `float` | +| `@constraint:Number` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `int`, `float`, `decimal` | +| `@constraint:String` | `length`, `minLength`, `maxLength`, `pattern` | `string` | +| `@constraint:Array` | `length`, `minLength`, `maxLength` | `any[]` | +| `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | `constraint:Date` | + The following example demonstrates how to apply constraint annotations to types. diff --git a/ballerina/Module.md b/ballerina/Module.md index 151ac66..a732754 100644 --- a/ballerina/Module.md +++ b/ballerina/Module.md @@ -8,14 +8,14 @@ The Ballerina `constraint` module facilitates APIs to do validations on the Ball This library provides the following annotations on Ballerina types to validate the values created with the respective types. -| Ballerina Type | Annotation | Supported Constraints | -|-----------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------| -| `int` | `@constraint:Int` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | -| `float` | `@constraint:Float` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | -| `int`|`float`|`decimal` | `@constraint:Number` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | -| `string` | `@constraint:String` | `length`, `minLength`, `maxLength`, `pattern` | -| `any[]` | `@constraint:Array` | `length`, `minLength`, `maxLength` | -| `constraint:Date` | `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | +| Annotation | Supported Constraints | Associated Ballerina Type(s) | +|----------------------|------------------------------------------------------------------------------------------------------------------------|------------------------------| +| `@constraint:Int` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `int` | +| `@constraint:Float` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `float` | +| `@constraint:Number` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `int`, `float`, `decimal` | +| `@constraint:String` | `length`, `minLength`, `maxLength`, `pattern` | `string` | +| `@constraint:Array` | `length`, `minLength`, `maxLength` | `any[]` | +| `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | `constraint:Date` | The following example demonstrates how to apply constraint annotations to types. diff --git a/ballerina/Package.md b/ballerina/Package.md index 1521131..12c7b10 100644 --- a/ballerina/Package.md +++ b/ballerina/Package.md @@ -8,14 +8,14 @@ The Ballerina `constraint` package facilitates APIs to do validations on the Bal This library provides the following annotations on Ballerina types to validate the values created with the respective types. -| Ballerina Type | Annotation | Supported Constraints | -|-----------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------| -| `int` | `@constraint:Int` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | -| `float` | `@constraint:Float` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | -| `int`|`float`|`decimal` | `@constraint:Number` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | -| `string` | `@constraint:String` | `length`, `minLength`, `maxLength`, `pattern` | -| `any[]` | `@constraint:Array` | `length`, `minLength`, `maxLength` | -| `constraint:Date` | `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | +| Annotation | Supported Constraints | Associated Ballerina Type(s) | +|----------------------|------------------------------------------------------------------------------------------------------------------------|------------------------------| +| `@constraint:Int` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `int` | +| `@constraint:Float` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `float` | +| `@constraint:Number` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `int`, `float`, `decimal` | +| `@constraint:String` | `length`, `minLength`, `maxLength`, `pattern` | `string` | +| `@constraint:Array` | `length`, `minLength`, `maxLength` | `any[]` | +| `@constraint:Date` | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT` | `constraint:Date` | The following example demonstrates how to apply constraint annotations to types. diff --git a/docs/spec/spec.md b/docs/spec/spec.md index a76c31b..53bcfa0 100644 --- a/docs/spec/spec.md +++ b/docs/spec/spec.md @@ -80,14 +80,14 @@ type User record {| The following table illustrates all the supported annotations with respect to the Ballerina types. -| Ballerina Type | Annotation | -|-----------------------------------|----------------------| -| `int` | `@constraint:Int` | -| `float` | `@constraint:Float` | -| `int`|`float`|`decimal` | `@constraint:Number` | -| `string` | `@constraint:String` | -| `any[]` | `@constraint:Array` | -| `constraint:Date` | `@constraint:Date` | +| Annotation | Associated Ballerina Type(s) | +|----------------------|------------------------------| +| `@constraint:Int` | `int` | +| `@constraint:Float` | `float` | +| `@constraint:Number` | `int`, `float`, `decimal` | +| `@constraint:String` | `string` | +| `@constraint:Array` | `any[]` | +| `@constraint:Date` | `constraint:Date` | ### 2.1. Constraint annotation on number types