Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit b060e40
Author: [email protected] <[email protected]>
Date:   Sat Aug 24 13:49:44 2019 +0530

    Updated Pubspec

commit 7790fb7
Author: [email protected] <[email protected]>
Date:   Sat Aug 24 13:47:58 2019 +0530

    Updated README

commit b884a6e
Author: Ibrahim Mubarak <[email protected]>
Date:   Sat Aug 17 16:27:37 2019 +0530

    Remove uses of dynamic

commit 9effb5a
Author: Ibrahim Mubarak <[email protected]>
Date:   Sat Aug 17 16:15:44 2019 +0530

    Add Analysis Options

commit aecb897
Author: Ibrahim Mubarak <[email protected]>
Date:   Sat Aug 17 15:18:18 2019 +0530

    Add InRule, NotInRule, Deprecated OneOfRule

commit f0c7dd8
Author: [email protected] <[email protected]>
Date:   Sat Aug 17 12:08:11 2019 +0530

    Updated description

commit 8dc0b51
Author: [email protected] <[email protected]>
Date:   Thu Aug 15 01:48:05 2019 +0530

    Bumped version to 0.2.1, updated pub description
  • Loading branch information
ibrahim-mubarak committed Aug 25, 2019
1 parent 456798a commit 8a36422
Show file tree
Hide file tree
Showing 25 changed files with 215 additions and 64 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## [v0.3.0]

### Added
- InRule, NotInRule

### Deprecated
- OneOfRule in favour of InRule


### Changed
- Minor Code Cleanup.

## [v0.2.0]

### Added
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flrx Validator

Validator on Steroids
It is a fluent API based FormField validator for flutter, that promotes code reuse. It is highly extensible, modular and testable.

![Pub](https://img.shields.io/pub/v/flrx_validator)
[![Build Status](https://api.cirrus-ci.com/github/flrx/validator.svg)](https://cirrus-ci.com/github/flrx/validator)
Expand All @@ -13,7 +13,7 @@ Add the following to your `pubspec.yaml` file

```yaml
dependencies:
flrx_validator: ^0.2.0
flrx_validator: ^0.3.0
```
## Usage
Expand All @@ -22,6 +22,21 @@ dependencies:
For more info on Flrx Validator and Rules you can see the [**documentation**](https://flrx.github.io/validator).
### Built In Rules
| Rule | Arguments | Description |
|-----------------|--------------|-------------|
| `AnyRule` | ruleList | Takes A list of rules, Passes when any of the rule passes, Useful for Validation of Username/Email Fields or similar fields|
| `EmailRule` | - | Passes when String is an E-Mail |
| `InRule` | acceptedList | Passes when the value is included in the given list of values. |
| `MaxLengthRule` | maxLength | Passes when the String length is less than the given length |
| `MinLengthRule` | minLength | Passes when the String length is more than the given length |
| `NotInRule` | rejectedList | Passes when the value is not included in the given list of values. |
| `RegexRule` | regex | Passes when the value matches the given Regex. |
| `RequiredRule` | - | Passes when the value is not null or empty. |

Can't see a rule you need? Raise an issue or create a Pull Request.

### Example

```dart
Expand Down
25 changes: 25 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
include: package:pedantic/analysis_options.yaml

linter:
rules:
- always_declare_return_types
- always_put_control_body_on_new_line
- always_put_required_named_parameters_first
- always_specify_types
- annotate_overrides
- avoid_bool_literals_in_conditional_expressions
- close_sinks
- package_prefixed_library_names
- prefer_const_declarations
- prefer_const_literals_to_create_immutables
- prefer_interpolation_to_compose_strings
- prefer_typing_uninitialized_variables
- prefer_void_to_null
- sort_pub_dependencies
- unnecessary_await_in_return
- unnecessary_brace_in_string_interps
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_null_aware_assignments
- unnecessary_overrides
- unnecessary_statements
48 changes: 28 additions & 20 deletions doc/.vuepress/config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
module.exports = {
title: "Flrx Validator",
description: "Validator on Steroids",
base: "/validator/",
themeConfig: {
displayAllHeaders: true, // Default: false,
sidebar: ["/", "/validator", "/rule"],
nav: [
{
text: "Changelog",
link: "https://github.com/flrx/validator/blob/master/CHANGELOG.md"
},
],
/* Repository Config */
repo: "flrx/validator",
repoLabel: "Github",
docsDir: "doc",
docsBranch: "master",
editLinks: true,
editLinkText: "Help us improve this page!"
},
title: "Flrx Validator",
description: "Validator on Steroids",
base: "/validator/",
themeConfig: {
displayAllHeaders: true, // Default: false,
sidebar: ["/", "/validator", "/rule"],
algolia: {
apiKey: '1cc6df72a7d648335129bdef99facc02',
indexName: 'flrx_validator'
},
nav: [
{
text: "Changelog",
link: "https://github.com/flrx/validator/blob/master/CHANGELOG.md"
},
{
text: "API Docs",
link: "https://pub.dev/documentation/flrx_validator/latest/"
},
],
/* Repository Config */
repo: "flrx/validator",
repoLabel: "Github",
docsDir: "doc",
docsBranch: "master",
editLinks: true,
editLinkText: "Help us improve this page!"
},
};
2 changes: 1 addition & 1 deletion doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ Add the following to your `pubspec.yaml` file:

```yaml
dependencies:
flrx_validator: ^0.2.0
flrx_validator: ^0.3.0
```
34 changes: 33 additions & 1 deletion doc/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Validator<String>()
Value is not a valid email address
```

### OneOfRule
### OneOfRule (Deprecated, See [InRule](#inrule))

This `Rule` checks whether the input provided is present in the the values provided to the `OneOfRule`.

Expand All @@ -106,6 +106,38 @@ Validator<String>()
Entity is not in the list of accepted values.
```

### InRule

This `Rule` checks if the value provided is in the list of accepted values.

```dart
Validator<String>()
.add(InRule(['value1', 'value2', 'value3']))
.build()
```

**Output**

```
Entity is not in the list of accepted values.
```

### NotInRule

This `Rule` checks if the value provided is not in the list of rejected values.

```dart
Validator<String>()
.add(NotInRule(['value1', 'value2', 'value3']))
.build()
```

**Output**

```
Entity is in list of rejected values.
```

### AnyRule

This `Rule` checks whether the input provided passes any of the `Rule`s provided to `AnyRule`.
Expand Down
6 changes: 3 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class MyApp extends StatelessWidget {
return MaterialApp(
title: _title,
home: SelectTypeOfForm(),
routes: {
'/material-form': (context) => MaterialForm(),
'/cupertino-form': (context) => CupertinoForm()
routes: <String, Widget Function(BuildContext)>{
'/material-form': (BuildContext context) => MaterialForm(),
'/cupertino-form': (BuildContext context) => CupertinoForm()
},
);
}
Expand Down
8 changes: 4 additions & 4 deletions example/lib/material_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MaterialForm extends StatefulWidget {
}

class _MaterialFormState extends State<MaterialForm> {
final _formKey = GlobalKey<FormState>();
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

@override
Widget build(BuildContext context) {
Expand All @@ -26,7 +26,7 @@ class _MaterialFormState extends State<MaterialForm> {
children: <Widget>[
TextFormField(
validator:
Validator().add(RequiredRule()).add(EmailRule()).build(),
Validator<String>().add(RequiredRule()).add(EmailRule()).build(),
decoration: InputDecoration(hintText: 'Email'),
),
buildDropdown(),
Expand All @@ -46,9 +46,9 @@ class _MaterialFormState extends State<MaterialForm> {

Widget buildDropdown() {
return DropdownButtonFormField<String>(
validator: Validator().add(RequiredRule()).build(),
validator: Validator<String>().add(RequiredRule()).build(),
value: "",
items: <DropdownMenuItem<String>>[
items: const <DropdownMenuItem<String>>[
DropdownMenuItem<String>(
child: Text('Please select an Item'),
value: "",
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.1.0"
version: "0.2.1"
flutter:
dependency: "direct main"
description: flutter
Expand Down
4 changes: 2 additions & 2 deletions lib/rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ abstract class Rule<T> {
/// This function is takes the default entity and value along with
/// the key, value pairs provided by the [getRuleSpecificParams()] function.
Map<String, String> _getResultantMap(String entityName, T value) {
return {
return <String, String>{
'entity': entityName,
'value': value.toString(),
...getRuleSpecificParams()
};
}

/// Returns a Map of values that are specific to the child [Rule].
Map<String, String> getRuleSpecificParams() => {};
Map<String, String> getRuleSpecificParams() => <String, String>{};
}
2 changes: 1 addition & 1 deletion lib/rules/email_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class EmailRule extends RegexRule {
static const String emailRegex =
r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$";

static const _emailValidationMessage = ":value is not a valid email address";
static const String _emailValidationMessage = ":value is not a valid email address";

EmailRule({String validationMessage})
: super(emailRegex,
Expand Down
15 changes: 15 additions & 0 deletions lib/rules/in_rule.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flrx_validator/rule.dart';

/// A [Rule] subclass validating if the input is one of the element in the [acceptedList].
class InRule<T> extends Rule<T> {
final List<T> acceptedList;

InRule(this.acceptedList, {String validationMessage})
: super(validationMessage);

@override
String onValidate(String entityName, T value) {
String validationMessage = ":entity is not in list of accepted values";
return acceptedList.contains(value) ? null : validationMessage;
}
}
2 changes: 1 addition & 1 deletion lib/rules/max_length_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ class MaxLengthRule extends Rule<String> {

@override
Map<String, String> getRuleSpecificParams() {
return {"maxLength": maxLength.toString()};
return <String, String>{"maxLength": maxLength.toString()};
}
}
2 changes: 1 addition & 1 deletion lib/rules/min_length_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class MinLengthRule extends Rule<String> {

@override
Map<String, String> getRuleSpecificParams() {
return {"minLength": minLength.toString()};
return <String, String>{"minLength": minLength.toString()};
}
}
15 changes: 15 additions & 0 deletions lib/rules/not_in_rule.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flrx_validator/rule.dart';

/// A [Rule] subclass validating if the input is one of the element in the [rejectionList].
class NotInRule<T> extends Rule<T> {
final List<T> rejectionList;

NotInRule(this.rejectionList, {String validationMessage})
: super(validationMessage);

@override
String onValidate(String entityName, T value) {
String validationMessage = ":entity is in list of rejected values";
return rejectionList.contains(value) ? validationMessage : null;
}
}
12 changes: 6 additions & 6 deletions lib/rules/one_of_rule.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'package:flrx_validator/rule.dart';
import 'package:flrx_validator/rules/in_rule.dart';

/// A [Rule] subclass validating if the input is one of the element in the [acceptedList].
class OneOfRule<T> extends Rule<T> {
final List<T> acceptedList;
@Deprecated('Use InRule instead')
class OneOfRule<T> extends InRule<T> {

OneOfRule(this.acceptedList, {String validationMessage})
: super(validationMessage);
OneOfRule(List<T> acceptedList, {String validationMessage})
: super(acceptedList, validationMessage: validationMessage);

@override
String onValidate(String entityName, T value) {
String validationMessage = ":entity is not in list of accepted values";
return acceptedList.contains(value) ? null : validationMessage;
return super.onValidate(entityName, value);
}
}
2 changes: 1 addition & 1 deletion lib/rules/regex_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RegexRule extends Rule<String> {
final String regex;

@override
String onValidate(String entityName, value) {
String onValidate(String entityName, String value) {
RegExp regExp = RegExp(regex);
if (!regExp.hasMatch(value)) {
return ":value is not a valid pattern";
Expand Down
3 changes: 2 additions & 1 deletion lib/src/string_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class StringUtils {
if (match.groupCount > 0) {
String key = match.group(0).substring(1);
String value = valueMap[key];
if (value == null)
if (value == null) {
throw ArgumentError("Value cannot be null. $key is passed as null");
}
return value;
}
return '';
Expand Down
6 changes: 3 additions & 3 deletions lib/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ class Validator<T> {
String Function(String, Map<String, String>) transformMessage;

/// List of all registered rules.
List<Rule<T>> rulesList = [];
List<Rule<T>> rulesList = <Rule<T>>[];

/// Registers a single [Rule] with the [Validator].
Validator add(Rule rule) {
Validator<T> add(Rule<T> rule) {
rulesList.add(rule);
return this;
}

/// Registers a List of [Rule] with the [Validator].
Validator addAll(List<Rule<T>> rule) {
Validator<T> addAll(List<Rule<T>> rule) {
rulesList.addAll(rule);
return this;
}
Expand Down
6 changes: 5 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: flrx_validator
description: >
A powerful, extensible validator package to get validation messages based on a list of rules.
version: 0.2.0
version: 0.3.0
author: Flrx Team <[email protected]>
homepage: https://flrx.github.io/validator/
repository: https://github.com/flrx/validator
issue_tracker: https://github.com/flrx/validator/issues
documentation: https://flrx.github.io/validator/

environment:
sdk: ">=2.2.2 <3.0.0"
Expand All @@ -14,3 +17,4 @@ dependencies:
dev_dependencies:
test: ^1.6.5
test_coverage: ^0.3.0+1
pedantic: ^1.8.0
Loading

0 comments on commit 8a36422

Please sign in to comment.