Giter VIP home page Giter VIP logo

embryo-validation's Introduction

Embryo Validation

PHP validation for PSR-7 request.

Features

  • PSR compatible.
  • Used for $request->getParsedBody and $request->getUploadedFiles.
  • Sanitize values.

Requirements

Installation

Using Composer:

$ composer require davidecesarano/embryo-validation

Example

You may quickly test this using the built-in PHP server going to http://localhost:8000.

$ cd example
$ php -S localhost:8000

Usage

Writing simple validation logic

Let's assume we have POST request. With PSR we have the $_POST parameters in $request->getParsedBody() and the $_FILES files in $request->getUploadedFiles(). Use Embryo Validation for to validate parameters values.

$request = (new Embryo\Http\Factory\ServerRequestFactory)->createServerRequestFromServer();
$validation = new Embryo\Validation\Validation($request);

$validation->name('title')->type('text')->required();
$validation->name('body')->type('any')->required();
if ($validation->isSuccess()) {
    // ...
} else {
    print_r($validation->getErrors());
}

If the validation rules pass, your code will keep executing normally; however, if validation fails, you can display errors.

Methods

name(string $name)

Set field name. If name not exists in $request->getParsedBody() or in $request->getUploadedFiles() return null.

type(string $type)

Set field type. If value not match at type return an error. The types are:

  • text. Value may be anything, it will be sanitized with FILTER_SANITIZE_STRING.
  • email. Value must be an email.
  • file. Value must be an array of UploadedFileInterface objects or an UploadedFileInterface object.
  • array. Value must be an array.
  • datetime. Value must be a DateTime format.
  • number. Value must be a generic number.
  • int. Value must be an integer.
  • float. Value must be a float.
  • url. Value must be a url.
  • slug. Value must be a slug.
  • boolean. Value must be a boolean.
  • any. Value may be anything, without sanitization (this is useful for html code).

required()

Set field require. The value must not be empty.

pattern(string $type)

Set field pattern. The value must match regular expression.

equal($value)

Set field value. The value must be the same.

match(... $value)

Set field matches. The value must match at one of the values.

length(int $min, int $max)

Set field length. The value may not be less than $min and greater than $max.

maxSize(int $size)

Set field file max size. The size of the file must not be greater than $size in bytes.

accept(... $ext)

Set allowed extensions for file field. The extension must match at one of the values.

result(): array

Return validation result. This method return an array like so:

    return [
        'status' => 200,
        'data' => [
            'title' => 'Hello World!',
            'body' => 'This is a post...',
            'name' => ''
        ],
        'errors' => [
            'name' => [
                'The name field is required.'
            ]
        ],
        'errorList' => [
            'The name field is required.'
        ]
    ];

If validation fails, status is 400, otherwise is 200.

isSuccess(): bool

Return true if validation pass, otherwise return false.

getErrors(): array

Return errors multidimensional array where key is field name and value is an errors array.

getErrorList(): array

Return errors array.

getData([string $key])

Return sanitized data array. If the value is an instance of UploadedFileInterface, you must use, for example, $file->getClientFilename() for returning file name. You may returning single item:

    echo $validation->getData('title'); // Hello World!

embryo-validation's People

Contributors

davidecesarano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.