https://github.com/kenjis/secure-validator
Secure Validator is a library for input validation.
https://github.com/kenjis/secure-validator
Last synced: 3 months ago
JSON representation
Secure Validator is a library for input validation.
- Host: GitHub
- URL: https://github.com/kenjis/secure-validator
- Owner: kenjis
- License: mit
- Created: 2015-06-20T02:24:51.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-07-16T01:23:48.000Z (over 10 years ago)
- Last Synced: 2025-06-21T09:09:31.878Z (7 months ago)
- Language: PHP
- Homepage:
- Size: 230 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Secure Validator
[](https://packagist.org/packages/kenjis/secure-validator) [](https://packagist.org/packages/kenjis/secure-validator) [](https://packagist.org/packages/kenjis/secure-validator) [](https://packagist.org/packages/kenjis/secure-validator)
[](https://scrutinizer-ci.com/g/kenjis/secure-validator/?branch=master)
[](https://coveralls.io/r/kenjis/secure-validator?branch=master)
[](https://travis-ci.org/kenjis/secure-validator)
Secure Validator is a library for input validation. It is based on [Sirius Validation](https://github.com/siriusphp/validation).
## Requirements
* PHP 5.4.0 or later
## Features
### Default Rules
Secure Validator promotes strict validation. It sets default validation rules to all fields.
* `ValidUtf8` checks if value is valid UTF-8 character encoding
* `IsString` checks if value is string
* `NoControl` checks if value does not have control characters (except for tab and newline)
And
* adds `MaxLength` 1 letter
That is you have to set (overwrite) max length rule to all fields. You don't forget it.
If a field does not match the default rules, you can remove the rules.
~~~php
$validator->remove('field', 'ValidUtf8');
~~~
### Fatal Rules
You can set a validation rule as *fatal* to detect abnormal input like an attack.
~~~php
$validator->add('field', 'maxlength', ['max' => 60, 'fatal' => true]);
~~~
If a fatal rule fails, exception `FatalValidationError` will be thrown immediately.
### Validated Data
You can get validated data only with `$validator->getValidated()`.
## How to Use
See [example.php](example.php) and [Sirius Validation Documentation](http://www.sirius.ro/php/sirius/validation/).
~~~php
$validator = new \Kenjis\Validation\Validator;
$validator->add('field', 'required | maxlength(max=60)');
if ($validator->validate($_POST)) {
// validation passed
} else {
// validation failed
}
~~~
See [Built-in validation rules](http://www.sirius.ro/php/sirius/validation/validation_rules.html).
### Added Method
#### `Validator::filter()`
Add filtering rule of Sirius\Filtration. See [Built-in filters](https://github.com/siriusphp/filtration/blob/master/docs/filters.md).
Validator will apply filters before validation.
~~~php
$validator->filter('field', 'StringTrim');
~~~
#### `Validator::getValidated()`
Get validated values.
~~~php
$allData = $validator->getValidated();
$field = $validator->getValidated('field');
~~~
#### `Validator::getInputValue()`
Get input value after filtering of specific field.
~~~php
$field = $validator->getInputValue('field');
~~~
### Tips
When you set `required`, if a rule fails, Sirius Validation will not apply any more rules to that field.