This repository has been archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 602e03a
Showing
17 changed files
with
5,352 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
vendor | ||
.idea |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,101 @@ | ||
# Tnapf/Validation | ||
|
||
Data validation plugin for Tnapf. | ||
|
||
# Installation | ||
|
||
``` | ||
composer require tnapf/validation | ||
``` | ||
|
||
## Array Structured Validations | ||
|
||
```php | ||
<?php | ||
|
||
use Tnapf\Validation\FilterVar; | ||
use Tnapf\Validation\MaxLength; | ||
use Tnapf\Validation\MinLength; | ||
use Tnapf\Validation\Regex; | ||
use function Tnapf\Validation\validateArray; | ||
|
||
$data = [ | ||
'name' => 'John Doe$$', | ||
'email' => 'malformed-email' | ||
]; | ||
|
||
$errors = validateArray([ | ||
'name' => [ | ||
new Regex('Name must only container letters.', '/^[a-zA-Z ]+$/'), | ||
new MaxLength('Name cannot exceed {max} characters.', 255), | ||
new MinLength('Name is required.', 0) | ||
], | ||
'email' => [ | ||
new FilterVar('Email is not valid.', FILTER_VALIDATE_EMAIL), | ||
new MaxLength('Email cannot exceed {max} characters.', 255), | ||
new MinLength('Email is required.', 0) | ||
], | ||
], $data); | ||
|
||
var_dump($errors); | ||
``` | ||
|
||
## Object Structured Validations | ||
|
||
```php | ||
$errors = validateModel(new class { | ||
#[Regex('Name must only container letters.', '/^[a-zA-Z ]+$/')] | ||
#[MaxLength('Name cannot exceed {max} characters.', 255)] | ||
#[MinLength('Name is required.', 0)] | ||
public string $name; | ||
|
||
#[FilterVar('Email is not valid.', FILTER_VALIDATE_EMAIL)] | ||
#[MaxLength('Email cannot exceed {max} characters.', 255)] | ||
#[MinLength('Email is required.', 0)] | ||
public string $email; | ||
}, [ | ||
'name' => 'John Doe$$', | ||
'email' => 'malformed-email' | ||
]); | ||
|
||
var_dump($errors); | ||
|
||
// or you can pass in a prefilled object | ||
|
||
$model = new class { | ||
#[Regex('Name must only container letters.', '/^[a-zA-Z ]+$/')] | ||
#[MaxLength('Name cannot exceed {max} characters.', 255)] | ||
#[MinLength('Name is required.', 0)] | ||
public string $name; | ||
|
||
#[FilterVar('Email is not valid.', FILTER_VALIDATE_EMAIL)] | ||
#[MaxLength('Email cannot exceed {max} characters.', 255)] | ||
#[MinLength('Email is required.', 0)] | ||
public string $email; | ||
}; | ||
|
||
$model->name = 'John Doe$$'; | ||
$model->email = 'malformed-email'; | ||
|
||
$errors = validateModel($model, []); | ||
|
||
var_dump($errors); | ||
``` | ||
|
||
## Getting validators from models | ||
|
||
```php | ||
use function Tnapf\Validation\getValidators; | ||
|
||
$validators = getValidators(new class { | ||
#[Regex('Name must only container letters.', '/^[a-zA-Z ]+$/')] | ||
#[MaxLength('Name cannot exceed {max} characters.', 255)] | ||
#[MinLength('Name is required.', 0)] | ||
public string $name; | ||
|
||
#[FilterVar('Email is not valid.', FILTER_VALIDATE_EMAIL)] | ||
#[MaxLength('Email cannot exceed {max} characters.', 255)] | ||
#[MinLength('Email is required.', 0)] | ||
public string $email; | ||
}); | ||
``` |
This file contains 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,35 @@ | ||
{ | ||
"name": "tnapf/validation", | ||
"type": "library", | ||
"autoload": { | ||
"psr-4": { | ||
"Tnapf\\Validation\\": "src/" | ||
}, | ||
"files": [ | ||
"src/helpers.php" | ||
] | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Robert Snedeker", | ||
"email": "rsnedeker20@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"tnapf/jsonmapper": "^v1.4.0", | ||
"friendsofphp/php-cs-fixer": "^3.16" | ||
}, | ||
"require-dev": { | ||
"pestphp/pest": "^2.34" | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"pestphp/pest-plugin": true | ||
} | ||
}, | ||
"scripts": { | ||
"test": "./vendor/bin/pest", | ||
"fix": "php-cs-fixer fix --using-cache=no", | ||
"fix:dry": "php-cs-fixer fix --using-cache=no --diff --dry-run" | ||
} | ||
} |
Oops, something went wrong.