GithubHelp home page GithubHelp logo

einlinuus / php-validator Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 49 KB

PHP helper library to validate user input

Home Page: https://packagist.org/packages/einlinuus/php-validator

License: GNU General Public License v3.0

PHP 100.00%
composer-package php php-library validation validator

php-validator's Introduction

PHP Validator

Capabilities

  • Check for datatype
  • Check for length / amount
  • Check for regex
  • Check for min / max entries in array
  • Check for unique entries in array
  • Clean strings (remove double spaces, trim, etc.)
  • Check for valid email format
  • Check for valid URL format
  • Check for specific array shape
  • Custom validation functions
  • Custom transformation functions

Use cases

  • Validate user input
  • Validate API input
  • Transform input (e.g. convert IDs to objects)

Installation

This package can be installed via composer:

composer require einlinuus/php-validator

New to composer?

Composer is a dependency manager for PHP. Composer can install this package from the packagist.org registry. Once installed, you'll find a vendor directory in your project.

You can import all required files by importing the autoload.php file created by composer: require_once __DIR__ . "/vendor/autoload.php"; With the autoload-file included in your project, you now have access to all classes and functions provided by this package.

Usage

Simply create a new instance of the Validator class and pass the input data to the constructor.

Next, chain the validation methods inside a try-catch block. If any of the validation methods fail, a ValidatorException will be thrown.

After the validation methods, you can get the output data by calling the get() method. The output data is your input data transformed by the transformation methods. If no transformation methods are used, the output data will be the same as the input data.

$input = "hello world";

$v = new Validator($input);
try {
    $v->isString("Input must be a string")
        ->isLowercase("Input must be lowercase")
        ->min(3, "Input must be at least 3 characters long")
        ->max(12, "Input must be at most 10 characters long");
} catch (ValidatorException $e) {
    die("Invalid: " . $e->getMessage());
}

$validated = $v->get();
var_dump($validated); // string(11) "hello world"

You can find more examples in the examples folder.

php-validator's People

Contributors

einlinuus avatar

Watchers

 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.