An extensible validation library for your data with sane defaults.

Overview

Hird

Latest Stable Version

Hirds, also known as housecarls, was a gathering of hirdmen, who functioned as the king's personal guards during the viking age and the early middle ages.

Hird

Hird is an extensible validation library for your data with sane defaults.

Installation

You can install the package via composer:

composer require askonomm/hird

Usage

Hird takes in an array of $fields and an array of $rules.

The key of each item in the $fields array must correspond to the the key of each item in the $rules array, so that Hird would know how to connect the two to each other.

The $rules must have a value that is a string where the rules are separated by a | character, and each rule must match the key of the implemented validator, such as len, email or one that you have implemented yourself. Additionally, each rule can take in a modifier, where the name of the rule and the modifier is separated by a : character.

For example, say we have a validator called len which takes a modifier that lets that validator validate the length of a string, in such a case we'd write that rule as len:8, which would indicate using a len validator and passing a modifier with the value 8 to it.

Example usage

An example usage of Hird looks like this:

use Askonomm\Hird\Hird;

$fields = ['email' => '[email protected]'];
$rules = ['email' => 'required|email|len:5'];
$hird = new Hird($fields, $rules);

if ($hird->fails()) {
    return $hird->errors();
}

From the above example, you can see that there are two Hird methods being used such as $hird->fails() and $hird->errors(). The $hird->fails() method will run the validation and return a boolean depending on whether the validation failed or not, true if it did. The $hird->errors() method will return an array of all the errors that occured, as defined by the validators.

You can also get the first error rather than all errors by using the method $hird->firstError().

If you wish to run the validation without needing to call $hird->fails(), you can instead call $hird->validate().

Built-in validators

There are a number of built-in validators available for use by default. If you want to remove a built-in validator, you can remove one using the $hird->removeValidator('rule-name') method.

email

The email validator validates an e-mail address, and it is registered as the email rule.

use Askonomm\Hird\Hird;

$fields = ['email' => '[email protected]'];
$rules = ['email' => 'email'];
$hird = new Hird($fields, $rules);

len

The len validator validates the length of a string, and it is registered as the len rule. The len validator also accepts, and requires, a modifier. A modifier can be passed to a rule by appending a color character : to it, and passing the modifier after it, like len:8.

use Askonomm\Hird\Hird;

$fields = ['password' => 'SuperSecretPassword'];
$rules = ['password' => 'len:10'];
$hird = new Hird($fields, $rules);

required

The required validator validates the presence of value, and it is registered as the required rule. It will pass validation if the value is set and the value is not an empty string.

use Askonomm\Hird\Hird;

$fields = ['password' => 'SuperSecretPassword'];
$rules = ['password' => 'required'];
$hird = new Hird($fields, $rules);

date-format

The date-format validator validates the string format of a date, and is registered as the date-format rule. It will pass validation if the value is set and the value is in the format specified by the rule.

use Askonomm\Hird\Hird;

$fields = ['date' => '2020-09-17'];
$rules = ['date' => 'date-format:Y-m-d'];
$hird = new Hird($fields, $rules);

Creating validators

You can also create your own validators, or replace existing ones if you're not happy with them.

Note: To replace an existing one, first remove the built-in validator via $hird->removeValidator('rule-name') and then add your own via $hird->registerValidator('rule-name', $validator).

A validator is a class that implements the Validator interface. A full example of a correct validator would look something like this:

use Askonomm\Hird\Validators\Validator;

class EmailValidator implements Validator
{
    /**
     * Returns a boolean `true` when given `$value` is a valid e-mail
     * address. Returns `false` otherwise.
     *
     * @param mixed $value
     * @param mixed $modifier
     * @return boolean
     */
    public static function validate(string $field, mixed $value, mixed $modifier = null): bool
    {
        return filter_var($value, FILTER_VALIDATE_EMAIL);
    }

    /**
     * Composes the error message in case the validation fails.
     *
     * @param string $field
     * @param mixed $modifier
     * @return string
     */
    public static function composeError(string $field, mixed $modifier = null): string
    {
        return "${field} is not a valid e-mail address.";
    }
}

You can see that there are two methods, one for validating the $value and the other for composing an error message if the validation fails. Both functions take in a $modifier argument, which will only have value if the validator is using modifiers. For example, the len validator is using modifiers to determine how long of a string should be required, by passing the rule in as len:{number-of-characters}.

Once you've created the class for your validator, you can register it by calling $hird->registerValidator('rule-name', (new YourValidatorClass)).

You might also like...
Validation rules for Money and Currency

money-validation-laravel Validation rules for Money and Currency Installation composer require brokeyourbike/money-validation-laravel Usage Package us

高性能的验证器组件(Validation),适用于 Hyperf 或 Laravel 框架,可获得数百倍的性能提升

验证器 简介 兼容 Hyperf/Laravel Validation 规则 部分场景可获得约 500 倍性能提升 验证器可多次复用不同数据,无状态设计 规则可全局复用 智能合并验证规则 安装 环境要求 PHP = 8.0 mbstring 扩展 ctype 扩展 安装命令 composer re

Extension for the Laravel validation class

Intervention Validation Intervention Validation is an extension library for Laravel's own validation system. The package adds rules to validate data l

Extra validation rules for dealing with images in Laravel 5.

Image-Validator Extra validation rules for dealing with images in Laravel 5. NOTE: As of Laravel version 5.2, there are now built-in validation rules

laminas-password-validator provides a validator for character-set based input validation.

laminas-password-validator laminas-password-validator provides a validator for character-set based input validation. Installation composer require pra

Laravel Validation Service

Laravel Validation Service Installation Add "prettus/laravel-repository": "1.1.*" to composer.json "prettus/laravel-validation": "1.1.*" Create a vali

This package contains validatiors of data used in Poland (PESEL, NIP, REGON etc.)

This package contains validatiors of data used in Poland (PESEL, NIP, REGON etc.)

PHP library - Validators for standards from ISO, International Finance, Public Administrations, GS1, Manufacturing Industry, Phone numbers & Zipcodes for many countries

IsoCodes PHP library - Validators for standards from ISO, International Finance, Public Administrations, GS1, Book Industry, Phone numbers & Zipcodes

A simple filtering library for PHP

Filterus - A flexible PHP 5.3 filter package Filter Methods: Each filter class has two primary methods: $filter-filter($var) - returns a modified ver

Releases(v1.1.1)
Owner
Asko Nõmm
Clojure(Script) developer. Get in touch via [email protected]
Asko Nõmm
Abstracts HTTP request input handling, providing an easy interface for data hydration and validation

Linio Input Linio Input is yet another component of the Linio Framework. It aims to abstract HTTP request input handling, allowing a seamless integrat

Linio 41 Dec 12, 2021
🔒 Laravel validation rule that checks if a password has been exposed in a data breach.

?? Laravel Password Exposed Validation Rule This package provides a Laravel validation rule that checks if a password has been exposed in a data breac

Jordan Hall 85 Apr 26, 2022
🔒 Laravel validation rule that checks if a password has been exposed in a data breach.

?? Laravel Password Exposed Validation Rule This package provides a Laravel validation rule that checks if a password has been exposed in a data breac

Jordan Hall 85 Apr 26, 2022
Valitron is a simple, elegant, stand-alone validation library with NO dependencies

Valitron: Easy Validation That Doesn't Suck Valitron is a simple, minimal and elegant stand-alone validation library with NO dependencies. Valitron us

Vance Lucas 1.5k Dec 30, 2022
Lightweight and feature-rich PHP validation and filtering library. Support scene grouping, pre-filtering, array checking, custom validators, custom messages. 轻量且功能丰富的PHP验证、过滤库。支持场景分组,前置过滤,数组检查,自定义验证器,自定义消息。

PHP Validate 一个简洁小巧且功能完善的php验证、过滤库。 简单方便,支持添加自定义验证器 支持前置验证检查, 自定义如何判断非空 支持将规则按场景进行分组设置。或者部分验证 支持在进行验证前对值使用过滤器进行净化过滤内置过滤器 支持在进行验证前置处理和后置处理独立验证处理 支持自定义每

Inhere 246 Jan 5, 2023
[READ-ONLY] Validation library from CakePHP. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Validation Library The validation library in CakePHP provides features to build validators that can validate arbitrary arrays of data with eas

CakePHP 39 Oct 11, 2022
Light and extendable schema validation library

Light PHP validation library For everyone who uses MongoDB or other NoSQL solution and cares about what client sends to his/her database and looking f

Alexander Serkin 43 Sep 28, 2022
FyreValidation is a free, open-source validation library for PHP.

FyreValidation FyreValidation is a free, validation library for PHP. Table Of Contents Installation Validators Rules Error Messages Installation Using

Elusive 0 Jan 15, 2022
File uploads with validation and storage strategies

Upload This component simplifies file validation and uploading. Usage Assume a file is uploaded with this HTML form: <form method="POST" enctype="mult

Brandon Savage 1.7k Dec 27, 2022
One Line Validation, For CodeIgniter 4

One Line Validation (OLV) is a package made for CodeIgniter 4 to provide a fast and single line validation experience ideal for CDN and/or API services consuming the Validation System from CodeIgniter 4.

AJ Meireles 2 Sep 21, 2021