validator
Easy-to-use, Highly Configurable Python Data Validator. Inspired by Laravel Validator
Science Score: 10.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
○codemeta.json file
-
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
✓Committers with academic emails
5 of 22 committers (22.7%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (8.8%) to scientific vocabulary
Keywords
Repository
Easy-to-use, Highly Configurable Python Data Validator. Inspired by Laravel Validator
Basic Info
- Host: GitHub
- Owner: CSenshi
- License: mit
- Language: Python
- Default Branch: master
- Homepage: https://pypi.org/project/validator/
- Size: 230 KB
Statistics
- Stars: 46
- Watchers: 5
- Forks: 24
- Open Issues: 16
- Releases: 0
Topics
Metadata Files
README.md
Validator
Validator is a Python library for dealing with request validating.
Table of Contents
Installation
Use the package manager pip to install Validator.
bash
pip install validator
Usage
User should pass request dictionary and rules dictionary for validating data in the request.
Please see examples below:
```python from validator import validate
request = {"name": "John Doe", "age": 33, "mail": "john_doe@gmail.com"}
rules = {"name": "required", "age": "integer|min:18", "mail": "required|mail"}
result = validate(request, rules) # True
``
validate()` returns either True or False.
Another option is to use Validator class
```python
from validator import Validator
request = {...} rules = {...}
val = Validator(request, rules) result = val.validate() # True ```
Validated Data/Error Messages
Validator allows user to have a look at failed validations and passed validations. validated_data is extremly useful when request contains data that is not needed for initialization of model, you can get rid of them and validate at the same time. See examples below:
Validated Data
```python from validator import validate
request = {"firstname": "John", "lastname": "Doe", "age": 33, "mail": "johndoe@gmail.com", "token": "WpH0UPfy0AXzMtK2UWtJ", "cookiedata": "e9Uixp8hzUySy6bw3MuZ", "session_id": "ZB7q7uIVdWBKgSCSSWAa"}
rule = {"firstname": "required", "lastname": "required", "age": "required|min:18", "mail": "required|mail"}
result, validateddata, _ = validate(request, rule, returninfo=True) """ result = True validateddata = {"firstname": "John", "last_name": "Doe", "age": 33, "mail": "johndoe@gmail.com"} """ ```
Error Messages ```python from validator import validate
request = {"name": "", "mail": "john_doe"}
rule = {"name": "required", "mail": "mail"}
result, , errors = validate(request, rule, returninfo=True)
""" result = False errors = {"name": {"Required': "Field was empty"}, "mail": {"Mail': "Expected a Mail, Got: john_doe"}} """ ```
Or you can use Validator class for error messages as well as for validated data.
python
val = Validator(request, rules)
result = val.validate()
validated_data = val.get_validated_data()
errors = val.get_errors()
Validating Arrays
Validator comes with validate_many() function, which validates multiple requests. Function takes list of requests and one rule. This rule is checked for all the requests. If one or more requests fail validation function returns False, otherwise (if all pass) True. For more details see example below:
Validation Passes: ```python from validator import validate_many
requests = [{"name": "John"}, {"name": "Rob"}, {"name": "Tom"}, {"name": "Greg"}] rule = {"name": "required|min:3"}
result = validatemany(requests, rule) # True
``
We can also have a look at failed validations and error messages.validatemany()` takes third argument as boolean, indicating return of error messages.
Validation Fails: ```python from validator import validate_many
requests = [{"name": "John"}, {"name": ""}, {"name": "Yo"}, {"name": "Greg"}] rule = {"name": "required|min:3"}
result, errors = validatemany(requests, rule, returninfo=True) """ result = False errors = [{}, {"name": {"Min": "Expected Maximum: 3, Got: 0", "Required": "Field was empty"}}, {"name": {"Min": "Expected Maximum: 3, Got: 2"}}, {}] """ ```
Available Validation Rules
Validator comes with pre initialized rules. All of rules are listed in RULES.md file
Rules
Validator Rules can be used in different ways. Please see some examples below:
Strings
python
rule = {"name": "required",
"age": "integer|min:18",
"mail": "required|mail"}
Array of Strings
python
rule = {"name": ["required"],
"age": ["integer", "min:18"],
"mail": ["required", "mail"]}
Array of Rules
```python from validator import rules as R
rules = {"name": [R.Required()], "age": [R.Integer(), R.Min(18)], "mail": [R.Requried(), R.Mail()]} ```
Other Miscellaneous
```python from validator import rules as R
rules = {"name": R.Required(), # no need for Array Brackets if one rule "age": [R.Integer, R.Min(18)], "mail": [R.Requried, R.Mail]} # no need for class initialization with brakcets () # if no arguments are passed to rule ```
Rules Interconnection
Rules can affect each other. Let's take a look at Size rule. It takes 1 argument and checks if data is equal to given value (example: 'size:10').
- Case 1: checks for length of '18' to be 18. len('18') is 2, therefore it is False. ```python request = {"age" : "18"} rule = {"age" : "size:18"}
validate(request, rule) """ result = False errors = {"age": {"Size": "Expected Size:18, Got:2"}} """ ```
- Case 2: checks if int representation of '18' is equal to 18. (int('18') = 18), therefore it is True. ```python request = {"age" : "18"} rule = {"age" : "integer|size:18"}
validate(request, rule) # True ```
For more details please view Size Rule
Custom Rules
We give users ability to advance and use their own checkers. Write function and use is as a rule. See examples below: 1. Use defined functions: ```python3 from validator import validate
def func_age(x):
return x >= 18
req = {"age": 30}
rules = {"age": func_age}
validate(req, rules)
```
Use Lambda functions: ```python3 from validator import validate
req = {"age": 30} rules = {"age": lambda x: x >= 18}
validate(req, rules) ```
Any callable class (NOTE: Pass class instance and not class itself): ```python3 from validator import validate
class checker: def init(self): pass
def call(self, x): return x >= 456
req = {"age": 30} rules = {"age": checker()}
validate(req, rules) ```
Custom Rule: ```python3 from validator import validate from validator.rules import Rule
class AgeRule(Rule): def init(self, min): Rule.init(self) self.min = min
def check(self, arg): return self.min <= argreq = {"age": 30} rules = {"age": AgeRule(18)}
validate(req, rules) ```
Examples
We have written some examples for you to get started easier. Please view Examples folder, where you can find validator usages with frameworks like Flask, Django and etc.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please see CONTRIBUTING.md before making PR :)
License
Owner
- Name: Saba Pochkhua
- Login: CSenshi
- Kind: user
- Location: Tbilisi, Georgia
- Company: @reddot
- Website: linkedin.com/in/saba-pochkhua
- Repositories: 2
- Profile: https://github.com/CSenshi
GitHub Events
Total
- Watch event: 1
- Fork event: 1
Last Year
- Watch event: 1
- Fork event: 1
Committers
Last synced: almost 3 years ago
All Time
- Total Commits: 163
- Total Committers: 22
- Avg Commits per committer: 7.409
- Development Distribution Score (DDS): 0.454
Top Committers
| Name | Commits | |
|---|---|---|
| CSenshi | s****a@g****m | 89 |
| beqakd | b****6@f****e | 11 |
| Ioane Margiani | m****e@g****m | 10 |
| Nika Losaberidze | n****6@f****e | 10 |
| bnMikheili | m****6@f****e | 8 |
| bnMikheili | 3****i@u****m | 5 |
| beqakd | 3****d@u****m | 4 |
| z13z | z****5@f****e | 3 |
| Vato Kostava | v****6@f****e | 3 |
| Tornike Kechakmadze | 4****7@u****m | 2 |
| Rezga12 | 3****2@u****m | 2 |
| Nato Egnatashvili | 4****1@u****m | 2 |
| Gugatke | 4****e@u****m | 2 |
| dbejanishvili | 3****i@u****m | 2 |
| mateuszz0000 | m****k@g****m | 2 |
| Varun Chauhan | 4****2@u****m | 2 |
| Dimitri Rogava | 3****e@u****m | 1 |
| LashaBuxo | l****o@g****m | 1 |
| Benjamin Soyka | b****a@i****m | 1 |
| Bakur Tsutskhashvili | b****i@g****m | 1 |
| rjbroussard | r****d@g****m | 1 |
| Marshall Mamiya | 4****m@u****m | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 53
- Total pull requests: 48
- Average time to close issues: about 1 month
- Average time to close pull requests: 3 days
- Total issue authors: 14
- Total pull request authors: 20
- Average comments per issue: 0.98
- Average comments per pull request: 0.38
- Merged pull requests: 42
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- CSenshi (35)
- iko1133 (3)
- beqakd (3)
- dzhaugasharov (2)
- blkmajik (1)
- dolohow (1)
- darrivau (1)
- over-star (1)
- greyfade (1)
- luminescent (1)
- rjbroussard (1)
- karanAT (1)
- karolyjozsa (1)
- rafael-bm (1)
Pull Request Authors
- iko1133 (6)
- CSenshi (4)
- nikalosa (4)
- z13z (3)
- VatoKo (3)
- beqakd (3)
- bsoyka (3)
- tkech17 (3)
- negna1 (2)
- bakurits (2)
- bnMikheili (2)
- dbejanishvili (2)
- Rezga12 (2)
- gugatkesheladze (2)
- varunhc (2)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- pypi 16,924 last-month
- Total docker downloads: 143
- Total dependent packages: 4
- Total dependent repositories: 28
- Total versions: 26
- Total maintainers: 1
pypi.org: validator
Python Validator
- Homepage: https://github.com/CSenshi/Validator
- Documentation: https://validator.readthedocs.io/
- License: MIT
-
Latest release: 0.7.1
published over 4 years ago
Rankings
Maintainers (1)
Dependencies
- black * development
- flask * development
- jinja2 * development
- pytest * development
- requests * development
- python-dateutil *