Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
faridfr committed Sep 24, 2020
0 parents commit ae96a96
Show file tree
Hide file tree
Showing 42 changed files with 1,378 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# 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/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 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: 8af6b2f038c1172e61d418869363a28dffec3cb4
channel: stable

project_type: package
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## [0.0.1] - First version for develop
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Farid Froozan

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:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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.
136 changes: 136 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Inspection

Inspection , validation for flutter and dart . With **Inspection** you can validate your inputs seems like **Laravel Framework** in **Dart**

## How to install :

Add this to your package's pubspec.yaml file:

```
dependencies:
inspection: ^0.0.1
```

Then You can install packages from the command line:

```
$ flutter pub get
```

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.
Now in your Dart code, you can use:

```
import 'package:inspection/Inspection.dart';
```

## Basic Usage :

You have a form , you want to validate inputs everyday in programming . With **Inspection** you can validate your inputs seems like **Laravel Framework** in **Dart** . See examples below .

You except a required number with many conditions and rules :
in this case , we get help from **Inspection** helper function .

```
TextFormField(
validator: (input) => inspection(input, 'required|numeric|between:2000,6000|contains:22,33,55|ends_with:0'),
),
```

You can use **Inspection** class :

```
TextFormField(
validator: (input) => Inspection().inspect(value, 'required|float'),
),
```

And you can use helper function and **Inspection** class with List of rules , so you can make a global validation configuration List and you can change your rules in whole of the application whenever you want.

```
TextFormField(
validator: (input) => inspection(input, ['required','float']),
),
```

or

```
TextFormField(
validator: (input) => inspec(input, ['required','float']),
),
```

## Options :
You can set attribute name , custom message and locale . For example :

```
TextFormField(
validator: (input) => inspec(input, 'required|float',message: 'my custom message here !'),
),
```


## Rules :


| Rule | Description | Example |
|------------ |------------------------------------- |----------------------------- |
| have_alpha | The attribute must have alpha in it | inspect(input,'have_alpha') |
| alpha_num | The attribute must contains only letters and numbers | inspect(input,'alpha_num') |
| between | The attribute must be between :min and :max | inspect(input,'between:3,9') |
| contains | The attribute must contains one of your worlds | inspect(input,'contains:farid,farhad') |
| email | The attribute is not a valid email | inspect(input,'email') |
| ends_with | The attribute must ends with your pattern | inspect(input,'ends_with:bye') |
| gt | The attribute must grater than :gt | inspect(input,'gt:10') |
| gte | The attribute must grater than or equal to :gte | inspect(input,'gte:10') |
| hex | The attribute must be a valid hex code | inspect(input,'hex') |
| in | The attribute must be one of options in your list | inspect(input,'in:farid,farhad,john') |
| ip | The attribute must be a valid IP | inspect(input,'ip') |
| lt | The attribute must be less than :lt | inspect(input,'lt:100') |
| lte | The attribute must be less than or equal to :lte | inspect(input,'lte:100') |
| max | The attribute must have maximum :max character | inspect(input,'max:10') |
| min | The attribute must have at least :min character | inspect(input,'min:3') |
| not_in | The attribute must not exists in list :not_in | inspect(input,'not_in:saman,ahmad') |
| numeric | The attribute must be a number | inspect(input,'numeric') |
| alpha | The attribute must contains only letters | inspect(input,'alpha') |
| float | The attribute must be float | inspect(input,'float') |
| integer | The attribute must be integer | inspect(input,'integer') |
| number | The attribute must contains only number | inspect(input,'number') |
| regex | The attribute must match with regex | inspect(input,'regex:\^[a-zA-Z0-9]') |
| required | TThe attribute field is required | inspect(input,'required') |
| starts_with | The attribute must starts with your pattern | inspect(input,'starts_with:hi') |
| url | The attribute must be a valid url | inspect(input,'url') |


## Locales :

**Inspection** support two languages now . **english** woth code : **en** , and **persian** with code **fa** . You can set locale when initialize **inspection** or in optional parameter of **inspect** method. For example :

```
TextFormField(
validator: (input) => Inspection(inspectionLocale : 'fa').inspect(input, 'required|in:farid,farhad'),
),
```

## Custom Rules :
You can simply extend from **Inspection** class and develop you custom rule . For example :

```
class CustomInspection extends Inspection {
String myCustomRule(ruleString, inspectionCase) {
switch (ruleString) {
case 'myCustomRule':
// Your conditional codes here or copy from a Rule class and change it to your own .
return MyCustomeRuleClass(inspectionCase).stringValidation();
}
return null;
}
}
```

## Contribute :
You can help me and contribute for :
- New rules
- More locales
- Better exceptions
35 changes: 35 additions & 0 deletions lib/Inspection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:inspection/InspectionCase.dart';
import 'package:inspection/Inspector.dart';

// Main package class :
// get optional configuration in constructor
class Inspection extends Inspector {
String inspectionLocale = 'en';

Inspection({String inspectionLocale}){
this.inspectionLocale = inspectionLocale;
}

// Main method :
// Go for validation . First split expected rules and then iterate in rules
inspect(dynamic input, dynamic rule,
{String name, String message, String locale}) {
InspectionCase inspectionCaseObject = new InspectionCase(
input, rule, name, message, locale ?? inspectionLocale);
return splitRules(inspectionCaseObject, myCustomRule: myCustomRule);
}
}

// Helper function for easy usage
inspection(dynamic input, dynamic rule,
{String name, String message, String locale}) {
return Inspection()
.inspect(input, rule, name: name, message: message, locale: locale);
}

// Helper function for easy usage
inspect(dynamic input, dynamic rule,
{String name, String message, String locale}) {
return Inspection()
.inspect(input, rule, name: name, message: message, locale: locale);
}
16 changes: 16 additions & 0 deletions lib/InspectionCase.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Structure of what we want to validate in our inspector
class InspectionCase {
dynamic input;
dynamic rule = '';
String name = '';
String message = '';
String locale = 'en';

InspectionCase(input, rule, name, message, locale) {
this.input = input;
this.rule = rule;
this.name = name;
this.message = message;
this.locale = locale ?? 'en';
}
}
48 changes: 48 additions & 0 deletions lib/Inspector.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:inspection/InspectionCase.dart';
import 'package:inspection/Rules.dart';

// Logic class :
// Split and iterate in expected rules
class Inspector extends Rules {
myCustomRule(ruleString, inspectionCase) {
return '';
}

// Split expected rules . Check type of input that can be list or string
splitRules(InspectionCase inspectionCase,
{Function(String, InspectionCase inspectionCase) myCustomRule}) {
if (inspectionCase.rule is List)
return iterateInRules(inspectionCase, myCustomRule: myCustomRule);
else {
inspectionCase.rule = inspectionCase.rule.split('|');
return iterateInRules(inspectionCase, myCustomRule: myCustomRule);
}
}

// Iterate in expected rules and check validation
iterateInRules(InspectionCase inspectionCase,
{Function(String, InspectionCase inspectionCase) myCustomRule}) {
for (var item in inspectionCase.rule) {
var inspectionResponse =
validation(inspectionCase, item, myCustomRule: myCustomRule);
if (inspectionResponse is String) if (inspectionResponse.length > 0)
return inspectionResponse;
}
}

// After split rules , it's time to validate inputs with a special rule . Maybe rules come from programmer callback function
validation(inspectionCase, inspectionCaseRule,
{Function(String, InspectionCase inspectionCase) myCustomRule}) {
inspectionCase.rule = inspectionCaseRule;
List rule = inspectionCaseRule.split(':');
String ruleString = rule[0];
if (myCustomRule != null) {
String customRulesOutput = myCustomRule(ruleString, inspectionCase);
if (customRulesOutput != null && customRulesOutput != '')
return customRulesOutput;
else
return checkRule(ruleString, inspectionCase);
} else
return checkRule(ruleString, inspectionCase);
}
}
29 changes: 29 additions & 0 deletions lib/Locales/en.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Map en() {
return {
'alpha_num': 'The :name must contains only letters and numbers',
'between': 'The :name must be between :min and :max',
'contains': 'The :name must contains :contains',
'email': 'The :name is not a valid email',
'ends_with': 'The :name must ends with :pattern pattern',
'gt': 'The :name must grater than :gt',
'gte': 'The :name must grater than or equal to :gte',
'have_alpha': 'The :name must have alpha in it',
'hex': 'The :name is not a valid hex code',
'in': 'The :name must be one of these options : :in',
'ip': 'The :name is not a valid IP',
'lt': 'The :name must be less than :lt',
'lte': 'The :name must be less than or equal to :lte',
'max': 'The :name must have maximum :max character',
'min': 'The :name must have at least :min character',
'not_in': 'The :name doesn\'t exists valid list :not_in',
'numeric': 'The :name must be a number',
'alpha': 'The :name must contains only letters',
'float': 'The :name must be float',
'integer': 'The :name must be integer',
'number': 'The :name must contains only number',
'regex': 'The :name format is invalid',
'required': 'The :name field is required',
'starts_with': 'The :name must starts with :pattern pattern',
'url': 'The :name is not a valid url',
};
}
Loading

0 comments on commit ae96a96

Please sign in to comment.