-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add new validators, Flutter form integration, example app, and CI #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c8356fd
feat: Add a Flutter example project, introduce new validators and the…
divyanshub024 5940bc2
feat: Introduce new validators, add Flutter `TextFormField` integrati…
divyanshub024 745fb7f
docs: Overhaul README with a new banner, updated content, and refresh…
divyanshub024 718ff0a
refactor: update validator error messages for improved clarity and co…
divyanshub024 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| name: Test and Analyze | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ "main" ] | ||
| pull_request: | ||
| branches: [ "main" ] | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: subosito/flutter-action@v2 | ||
| with: | ||
| channel: 'stable' | ||
| - name: Install dependencies | ||
| run: dart pub get | ||
| - name: Analyze code | ||
| run: dart analyze | ||
| - name: Run tests | ||
| run: dart test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,89 +1,167 @@ | ||
| # Flutter Validators | ||
| <p align="left"> | ||
| <a href="https://img.shields.io/badge/License-MIT-green"><img src="https://img.shields.io/badge/License-MIT-green" alt="MIT License"></a> | ||
| <a href="https://github.com/divyanshub024/flutter_validators/stargazers"><img src="https://img.shields.io/github/stars/divyanshub024/flutter_validators?style=flat&logo=github&colorB=green&label=stars" alt="stars"></a> | ||
| <a href="https://pub.dev/packages/flutter_validators"><img src="https://img.shields.io/pub/v/flutter_validators.svg" alt="Pub"></a> | ||
| <p align="center"> | ||
| <img src="https://raw.githubusercontent.com/divyanshub024/flutter_validators/main/assets/banner.png" alt="Flutter Validators Banner" /> | ||
| </p> | ||
|
|
||
| A dart package of [String](https://api.dart.dev/stable/2.14.0/dart-core/String-class.html) validators and sanitizers. | ||
| Inspired by [validator.js](https://github.com/validatorjs/validator.js) | ||
| <p align="center"> | ||
| <h1 align="center">Flutter Validators</h1> | ||
| </p> | ||
|
|
||
| ## Installation | ||
| <p align="center"> | ||
| <strong>The most comprehensive string validation package for Dart & Flutter.</strong> | ||
| </p> | ||
|
|
||
| Run this command: | ||
| With Dart: | ||
| <p align="center"> | ||
| <a href="https://pub.dev/packages/flutter_validators"><img src="https://img.shields.io/pub/v/flutter_validators.svg?logo=dart&color=blue" alt="Pub Version"></a> | ||
| <a href="https://github.com/divyanshub024/flutter_validators/actions"><img src="https://github.com/divyanshub024/flutter_validators/actions/workflows/test.yml/badge.svg" alt="CI"></a> | ||
| <a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-green" alt="MIT License"></a> | ||
| <a href="https://github.com/divyanshub024/flutter_validators/stargazers"><img src="https://img.shields.io/github/stars/divyanshub024/flutter_validators?style=flat&logo=github&colorB=green&label=stars" alt="Stars"></a> | ||
| </p> | ||
|
|
||
| ``` | ||
| dart pub add flutter_validators | ||
| ``` | ||
| <p align="center"> | ||
| Inspired by <a href="https://github.com/validatorjs/validator.js">validator.js</a> · 20+ validators · Works with Flutter Forms out of the box | ||
| </p> | ||
|
|
||
| This will add a line like this to your package's pubspec.yaml (and run an implicit `dart pub get`): | ||
| --- | ||
|
|
||
| ``` | ||
| A pure Dart package with 20+ string validators and sanitizers, from emails and URLs to credit cards and UUIDs. Use them as simple functions, convenient `String` extensions, or plug them directly into Flutter's `TextFormField` with the built-in `Validator` class. Zero dependencies, fully tested. | ||
|
|
||
| --- | ||
|
|
||
| ## 📦 Installation | ||
|
|
||
| ```yaml | ||
| dependencies: | ||
| flutter_validators: ^1.0.0 | ||
| flutter_validators: ^1.1.0 | ||
| ``` | ||
|
|
||
| ## Usage | ||
| Then run: | ||
|
|
||
| Import the package in your Dart file: | ||
| ``` | ||
| import 'package:flutter_validators/flutter_validators.dart'; | ||
| ```sh | ||
| dart pub get | ||
| ``` | ||
|
|
||
| Now, you can call the respective validator method. | ||
| --- | ||
|
|
||
| ``` | ||
| var email = 'foo@bar.com'; | ||
| ## 🚀 Quick Start | ||
|
|
||
| print(isEmail(email)); // true | ||
| ```dart | ||
| import 'package:flutter_validators/flutter_validators.dart'; | ||
| ``` | ||
|
|
||
| To have more streamlined code use built-in String extension methods | ||
| ### Use as String Extensions | ||
|
|
||
| ``` | ||
| print('foo@bar.com'.isEmail); | ||
| ```dart | ||
| 'foo@bar.com'.isEmail; // true | ||
| 'https://google.com'.isURL; // true | ||
| '4111111111111111'.isCreditCard; // true | ||
| 'abc123'.isAlphanumeric; // true | ||
| ``` | ||
|
|
||
| ## Validators | ||
| ### Use as Top-Level Functions | ||
|
|
||
| Here is a list of the validators currently available. | ||
|
|
||
| | Validator | Description | | ||
| | ----------- | ----------- | | ||
| | isAscii | Check if a string contaibs ASCII chars only. | | ||
| | isBase32 | Check if a string is base32 encoded. | | ||
| | isBase58 | Check if a string is base58 encoded. | | ||
| | isBoolean | Check if String is a boolean. | | ||
| | isEmail | Check if string is a valid email. | | ||
| | equals | Check if both Strings are equal. | | ||
| | isInt | Check if string is a valid integer. | | ||
| | isJson | Check if string is a valid JSON. | | ||
| | isPhone | Check if string is a valid phone number. | | ||
| ```dart | ||
| isEmail('foo@bar.com'); // true | ||
| isURL('https://google.com'); // true | ||
| isIP('192.168.1.1'); // true | ||
| ``` | ||
|
|
||
| ## License (MIT) | ||
| --- | ||
|
|
||
| ## 📝 Flutter Form Integration | ||
|
|
||
| The `Validator` class returns `String? Function(String?)` closures — exactly what `TextFormField.validator` expects. Each method accepts a custom `errorMessage`. | ||
|
|
||
| ```dart | ||
| Form( | ||
| child: Column( | ||
| children: [ | ||
| TextFormField( | ||
| decoration: const InputDecoration(labelText: 'Email'), | ||
| validator: Validator.email(errorMessage: 'Enter a valid email'), | ||
| ), | ||
| TextFormField( | ||
| decoration: const InputDecoration(labelText: 'Website'), | ||
| validator: Validator.url(), | ||
| ), | ||
| TextFormField( | ||
| decoration: const InputDecoration(labelText: 'Age'), | ||
| validator: Validator.numeric(errorMessage: 'Must be a number'), | ||
| ), | ||
| ], | ||
| ), | ||
| ) | ||
| ``` | ||
|
|
||
| > **Tip:** Use `Validator.required()` alongside other validators to enforce non-empty fields. | ||
|
|
||
| See the [`example/`](example/) directory for a complete working app. | ||
|
|
||
| --- | ||
|
|
||
| ## 📋 All Validators | ||
|
|
||
| Every validator is available **both** as a top-level function and as a `String` extension. | ||
|
|
||
| | Validator | Extension | Description | | ||
| |---|---|---| | ||
| | `isEmail(str)` | `str.isEmail` | Valid email address | | ||
| | `isURL(str)` | `str.isURL` | Valid HTTP/HTTPS URL | | ||
| | `isIP(str, [version])` | `str.isIP` / `str.isIPv4` / `str.isIPv6` | Valid IP address (v4 or v6) | | ||
| | `isUUID(str)` | `str.isUUID` | Valid UUID | | ||
| | `isCreditCard(str)` | `str.isCreditCard` | Credit card number (Luhn algorithm) | | ||
| | `isDate(str)` | `str.isDate` | Parseable date string | | ||
| | `isJson(str)` | `str.isJson` | Valid JSON | | ||
| | `isInt(str)` | `str.isInt` | Valid integer | | ||
| | `isNumeric(str)` | `str.isNumeric` | Valid number (int or float) | | ||
| | `isAlpha(str)` | `str.isAlpha` | Letters only (a–z, A–Z) | | ||
| | `isAlphanumeric(str)` | `str.isAlphanumeric` | Letters and numbers only | | ||
| | `isAscii(str)` | `str.isAscii` | ASCII characters only | | ||
| | `isBase32(str)` | `str.isBase32` | Base32 encoded | | ||
| | `isBase58(str)` | `str.isBase58` | Base58 encoded | | ||
| | `isBoolean(str)` | `str.isBoolean` | Boolean string (`true`/`false`/`1`/`0`) | | ||
| | `isHexColor(str)` | `str.isHexColor` | Hex color code (`#fff`, `ff0000`) | | ||
| | `isPhone(str)` | `str.isPhone` | Valid phone number | | ||
| | `isLength(str, min, [max])` | `str.isLength(min, [max])` | Length within range | | ||
| | `equals(str, comparison)` | `str.equals(comparison)` | Exact string match | | ||
|
|
||
| --- | ||
|
|
||
| ## 🏗️ Form Validator API Reference | ||
|
|
||
| All methods on the `Validator` class return `String? Function(String?)`: | ||
|
|
||
| ```dart | ||
| Validator.required({String errorMessage}) | ||
| Validator.email({String errorMessage}) | ||
| Validator.url({String errorMessage}) | ||
| Validator.ip({int? version, String errorMessage}) | ||
| Validator.date({String errorMessage}) | ||
| Validator.numeric({String errorMessage}) | ||
| Validator.integer({String errorMessage}) | ||
| Validator.alpha({String errorMessage}) | ||
| Validator.alphanumeric({String errorMessage}) | ||
| Validator.phone({String errorMessage}) | ||
| Validator.creditCard({String errorMessage}) | ||
| Validator.json({String errorMessage}) | ||
| Validator.uuid({String errorMessage}) | ||
| Validator.hexColor({String errorMessage}) | ||
| Validator.ascii({String errorMessage}) | ||
| Validator.base32({String errorMessage}) | ||
| Validator.base58({String errorMessage}) | ||
| Validator.boolean({String errorMessage}) | ||
| Validator.equals(String comparison, {String errorMessage}) | ||
| Validator.length(int min, {int? max, String errorMessage}) | ||
| ``` | ||
| MIT License | ||
|
|
||
| Copyright (c) 2021 Divyanshu Bhargava | ||
| --- | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| ## 🤝 Contributing | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| Contributions, issues, and feature requests are welcome! | ||
| Feel free to check the [issues page](https://github.com/divyanshub024/flutter_validators/issues). | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. | ||
| --- | ||
|
|
||
| ``` | ||
| ## 📄 License | ||
|
|
||
| This project is [MIT](LICENSE) licensed. | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Miscellaneous | ||
| *.class | ||
| *.log | ||
| *.pyc | ||
| *.swp | ||
| .DS_Store | ||
| .atom/ | ||
| .build/ | ||
| .buildlog/ | ||
| .history | ||
| .svn/ | ||
| .swiftpm/ | ||
| migrate_working_dir/ | ||
|
|
||
| # IntelliJ related | ||
| *.iml | ||
| *.ipr | ||
| *.iws | ||
| .idea/ | ||
|
|
||
| # The .vscode folder contains launch configuration and tasks you configure in | ||
| # VS Code which you may wish to be included in version control, so this line | ||
| # is commented out by default. | ||
| #.vscode/ | ||
|
|
||
| # Flutter/Dart/Pub related | ||
| **/doc/api/ | ||
| **/ios/Flutter/.last_build_id | ||
| .dart_tool/ | ||
| .flutter-plugins-dependencies | ||
| .pub-cache/ | ||
| .pub/ | ||
| /build/ | ||
| /coverage/ | ||
|
|
||
| # Symbolication related | ||
| app.*.symbols | ||
|
|
||
| # Obfuscation related | ||
| app.*.map.json | ||
|
|
||
| # Android Studio will place build artifacts here | ||
| /android/app/debug | ||
| /android/app/profile | ||
| /android/app/release |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # This file tracks properties of this Flutter project. | ||
| # Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
| # | ||
| # This file should be version controlled and should not be manually edited. | ||
|
|
||
| version: | ||
| revision: "2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa" | ||
| channel: "stable" | ||
|
|
||
| project_type: app | ||
|
|
||
| # Tracks metadata for the flutter migrate command | ||
| migration: | ||
| platforms: | ||
| - platform: root | ||
| create_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| base_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| - platform: android | ||
| create_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| base_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| - platform: ios | ||
| create_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| base_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| - platform: linux | ||
| create_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| base_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| - platform: macos | ||
| create_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| base_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| - platform: web | ||
| create_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| base_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| - platform: windows | ||
| create_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
| base_revision: 2c9eb20739dfec95e2c74bd3dfa4601b0a8a36aa | ||
|
|
||
| # User provided section | ||
|
|
||
| # List of Local paths (relative to this file) that should be | ||
| # ignored by the migrate tool. | ||
| # | ||
| # Files that are not part of the templates will be ignored by default. | ||
| unmanaged_files: | ||
| - 'lib/main.dart' | ||
| - 'ios/Runner.xcodeproj/project.pbxproj' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # example | ||
|
|
||
| A new Flutter project. | ||
|
|
||
| ## Getting Started | ||
|
|
||
| This project is a starting point for a Flutter application. | ||
|
|
||
| A few resources to get you started if this is your first Flutter project: | ||
|
|
||
| - [Learn Flutter](https://docs.flutter.dev/get-started/learn-flutter) | ||
| - [Write your first Flutter app](https://docs.flutter.dev/get-started/codelab) | ||
| - [Flutter learning resources](https://docs.flutter.dev/reference/learning-resources) | ||
|
|
||
| For help getting started with Flutter development, view the | ||
| [online documentation](https://docs.flutter.dev/), which offers tutorials, | ||
| samples, guidance on mobile development, and a full API reference. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.