{"id":28829480,"url":"https://github.com/ballerina-platform/module-ballerina-constraint","last_synced_at":"2026-01-31T21:14:49.707Z","repository":{"id":39609300,"uuid":"490118894","full_name":"ballerina-platform/module-ballerina-constraint","owner":"ballerina-platform","description":"Ballerina Constraint Module","archived":false,"fork":false,"pushed_at":"2025-03-16T17:10:16.000Z","size":687,"stargazers_count":107,"open_issues_count":0,"forks_count":18,"subscribers_count":62,"default_branch":"main","last_synced_at":"2025-06-19T05:16:08.625Z","etag":null,"topics":["ballerina","hacktoberfest","integration","wso2"],"latest_commit_sha":null,"homepage":"","language":"Ballerina","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ballerina-platform.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-09T03:13:40.000Z","updated_at":"2025-04-19T14:05:19.000Z","dependencies_parsed_at":"2022-07-13T09:10:47.401Z","dependency_job_id":"0ea2ca3a-b55c-4fc8-aac1-74821d603a3d","html_url":"https://github.com/ballerina-platform/module-ballerina-constraint","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/ballerina-platform/module-ballerina-constraint","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ballerina-platform%2Fmodule-ballerina-constraint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ballerina-platform%2Fmodule-ballerina-constraint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ballerina-platform%2Fmodule-ballerina-constraint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ballerina-platform%2Fmodule-ballerina-constraint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ballerina-platform","download_url":"https://codeload.github.com/ballerina-platform/module-ballerina-constraint/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ballerina-platform%2Fmodule-ballerina-constraint/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28955278,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-31T18:30:42.805Z","status":"ssl_error","status_checked_at":"2026-01-31T18:30:19.593Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ballerina","hacktoberfest","integration","wso2"],"created_at":"2025-06-19T05:13:39.141Z","updated_at":"2026-01-31T21:14:49.701Z","avatar_url":"https://github.com/ballerina-platform.png","language":"Ballerina","funding_links":[],"categories":[],"sub_categories":[],"readme":"Ballerina Constraint Library\n===================\n\n  [![Build](https://github.com/ballerina-platform/module-ballerina-constraint/actions/workflows/build-timestamped-master.yml/badge.svg)](https://github.com/ballerina-platform/module-ballerina-constraint/actions/workflows/build-timestamped-master.yml)\n  [![codecov](https://codecov.io/gh/ballerina-platform/module-ballerina-constraint/branch/main/graph/badge.svg)](https://codecov.io/gh/ballerina-platform/module-ballerina-constraint) \n  [![Trivy](https://github.com/ballerina-platform/module-ballerina-constraint/actions/workflows/trivy-scan.yml/badge.svg)](https://github.com/ballerina-platform/module-ballerina-constraint/actions/workflows/trivy-scan.yml)\n  [![GraalVM Check](https://github.com/ballerina-platform/module-ballerina-constraint/actions/workflows/build-with-bal-test-graalvm.yml/badge.svg)](https://github.com/ballerina-platform/module-ballerina-constraint/actions/workflows/build-with-bal-test-graalvm.yml)\n  [![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)\n  [![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)\n\nThe 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.\n\n### Constraint annotations\n\nThis library provides the following annotations on Ballerina types to validate the values created with the respective types.\n\n| Annotation           | Supported Constraints                                                                                                  | Associated Ballerina Type(s) |\n|----------------------|------------------------------------------------------------------------------------------------------------------------|------------------------------|\n| `@constraint:Int`    | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `int`                        |\n| `@constraint:Float`  | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `float`                      |\n| `@constraint:Number` | `minValue`, `maxValue`, `minValueExclusive`, `maxValueExclusive`, `maxDigits`, `maxIntegerDigits`, `maxFractionDigits` | `int`, `float`, `decimal`    |\n| `@constraint:String` | `length`, `minLength`, `maxLength`, `pattern`                                                                          | `string`                     |\n| `@constraint:Array`  | `length`, `minLength`, `maxLength`                                                                                     | `any[]`                      |\n| `@constraint:Date`   | `option` - `PAST` or `FUTURE` or `PAST_OR_PRESENT` or `FUTURE_OR_PRESENT`                                              | `constraint:Date`            |\n\n\nThe following example demonstrates how to apply constraint annotations to types.\n\n```ballerina\n@constraint:String { pattern: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,}$` }\npublic type Email string;\n\n@constraint:Date { option: constraint:PAST }\npublic type DOB time:Date;\n\npublic type Person record {|\n    @constraint:String { minLength: 5, maxLength: 10 }\n    string name;\n    @constraint:Int { minValue: 18, maxValue: 60 }\n    int age;\n    Email email;\n    @constraint:Array { minLength: 1, maxLength: 5 }\n    string[] phoneNumbers;\n    DOB dob;\n|};\n```\n\n### Constraint validation\n\nThe `validate` function in this library can be used to validate the values with respect to the constraints defined in the respective types.\n\nThe 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.\n\n```ballerina\npublic function main() returns error? {\n    Person person = {\n        name: \"John\",\n        age: 25,\n        email: \"john@mail.com\",\n        phoneNumbers: [\"1234567890\", \"0987654321\"],\n        dob: { year: 1996, month: 5, day: 15 }\n    };\n    \n    Person|error personValidated = constraint:validate(person);\n    if personValidated is error {\n        io:println(\"Validation failed: \" + personValidated.message());\n    } else {\n        io:println(\"Validation successful\");\n    }\n}\n```\n\n### Custom error messages on validation failures\n\nOptionally a custom error message can be provided for each constraint. The following example demonstrates how to provide custom error messages for constraints.\n\n```ballerina\n@constraint:String { \n    pattern: {\n        value: re `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,}$`,\n        message: \"Invalid email address\"\n    }\n}\npublic type Email string;\n\n@constraint:Date {\n    option: {\n        value: constraint:PAST,\n        message: \"Date of birth should be in the past\"\n    },\n    message: \"Invalid date found\"\n}\npublic type DOB time:Date;\n\npublic type Person record {|\n    @constraint:String { \n        minLength: 5, \n        maxLength: {\n            value: 10,\n            message: \"Name should be less than 10 characters\"\n        }\n    }\n    string name;\n    @constraint:Int {\n        minValue: {\n            value: 18,\n            message: \"Age should be greater than 18\"\n        },\n        maxValue: 60\n    }\n    int age;\n    Email email;\n    @constraint:Array { \n        minLength: {\n            value: 1,\n            message: \"At least one phone number should be provided\"\n        },\n        maxLength: 5 \n    }\n    string[] phoneNumbers;\n    DOB dob;\n|};\n```\n\n## Issues and projects\n\nIssues 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).\n\nThis repository only contains the source code for the module.\n\n## Build from the source\n\n### Set up the prerequisites\n\n1. Download and install Java SE Development Kit (JDK) version 21 (from one of the following locations).\n\n   * [Oracle](https://www.oracle.com/java/technologies/downloads/)\n   \n   * [OpenJDK](https://adoptium.net)\n   \n        \u003e **Note:** Set the `JAVA_HOME` environment variable to the path name of the directory into which you installed JDK.\n\n2. Export your GitHub Personal Access Token (PAT) with the `read package` permission as follows:\n\n    ```\n    export packageUser=\u003cUsername\u003e\n    export packagePAT=\u003cPersonal Access Token\u003e\n    ```\n\n3. Download and install [Docker](https://www.docker.com/).\n\n### Build the source\n\nExecute the commands below to build from the source.\n\n1. To build the package:\n    ```    \n    ./gradlew clean build\n    ```\n2. To run the tests:\n    ```\n    ./gradlew clean test\n    ```\n\n3. To run a group of tests\n    ```\n    ./gradlew clean test -Pgroups=\u003ctest_group_names\u003e\n    ```\n\n4. To build the without the tests:\n    ```\n    ./gradlew clean build -x test\n    ```\n\n5. To debug package implementation:\n    ```\n    ./gradlew clean build -Pdebug=\u003cport\u003e\n    ```\n\n6. To debug with Ballerina language:\n    ```\n    ./gradlew clean build -PbalJavaDebug=\u003cport\u003e\n    ```\n\n7. Publish the generated artifacts to the local Ballerina central repository:\n    ```\n    ./gradlew clean build -PpublishToLocalCentral=true\n    ```\n\n8. Publish the generated artifacts to the Ballerina central repository:\n    ```\n    ./gradlew clean build -PpublishToCentral=true\n    ```\n\n## Contribute to Ballerina\n\nAs an open source project, Ballerina welcomes contributions from the community.\n\nFor more information, go to the [contribution guidelines](https://github.com/ballerina-platform/ballerina-lang/blob/master/CONTRIBUTING.md).\n\n## Code of conduct\n\nAll contributors are encouraged to read the [Ballerina Code of Conduct](https://ballerina.io/code-of-conduct).\n\n## Useful links\n\n* For more information go to the [`constraint` library](https://lib.ballerina.io/ballerina/constraint/latest).\n* For example demonstrations of the usage, go to [Ballerina By Examples](https://ballerina.io/learn/by-example/).\n* Chat live with us via our [Discord server](https://discord.gg/ballerinalang).\n* Post all technical questions on Stack Overflow with the [#ballerina](https://stackoverflow.com/questions/tagged/ballerina) tag.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fballerina-platform%2Fmodule-ballerina-constraint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fballerina-platform%2Fmodule-ballerina-constraint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fballerina-platform%2Fmodule-ballerina-constraint/lists"}