Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
CommandString committed Apr 23, 2024
0 parents commit 602e03a
Show file tree
Hide file tree
Showing 17 changed files with 5,352 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
.idea
257 changes: 257 additions & 0 deletions .php-cs-fixer.dist.php

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions README.md
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;
});
```
35 changes: 35 additions & 0 deletions composer.json
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"
}
}
Loading

0 comments on commit 602e03a

Please sign in to comment.