Skip to content

Commit

Permalink
Add Readme.md, change to custom validation to take only one parameter…
Browse files Browse the repository at this point in the history
…, add error messages to php unit tests

Signed-off-by: Thibault JUNIN <[email protected]>
  • Loading branch information
thibaultjunin committed Mar 21, 2022
1 parent a65fa51 commit 4ae0d96
Show file tree
Hide file tree
Showing 7 changed files with 2,432 additions and 2,174 deletions.
41 changes: 41 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Validator

A simple PHP validation library

[![PHP Composer](https://github.com/stantabcorp/validator/actions/workflows/php.yml/badge.svg)](https://github.com/stantabcorp/validator/actions/workflows/php.yml)

## Installation

```bash
composer require stantabcorp/validator
```

## Using the library

```php
$validator = new Stantabcorp\Validator\Validator(["array" => ["to" => "validate"]]); // Init the library providing an array to validate.
$validator->required("array.to"); // Test if the key `to` in the array `array` is present.
$validator->array("array"); // Test if the key `array` is an array.

$validator->isValid(); // Return a boolean is the array is valid or not.

$validator->getErrors(); // Return a list of string containing the error messages.
```

### Custom validation

```php
$validator->customValidation("array", function (\Stantabcorp\Validator\CustomValidator $customValidator) {
$customValidator->getKey(); // The key (`array` in this case)
$customValidator->getValue(); // The associated value

// Mark the kay as invalid and add an error message.
$customValidator->addError("This is an error message");
});
```

## Testing

```bash
composer run-script test
```
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"adbario/php-dot-notation": "^2.2"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"symfony/var-dumper": "^6.0",
"ext-json": "*"
}
}
Loading

0 comments on commit 4ae0d96

Please sign in to comment.