The component provides an array-based DSL to construct complex validation chains.

Overview

Spiral Validator

PHP Latest Version on Packagist GitHub Tests Action Status Total Downloads

The component provides an array-based DSL to construct complex validation chains.

Requirements

Make sure that your server is configured with following PHP version and extensions:

  • PHP 8.1+
  • Spiral framework 3.0+

Installation

You can install the package via composer:

composer require spiral/validator

After package install you need to register bootloader from the package.

protected const LOAD = [
    // ...
    \Spiral\Validator\Bootloader\ValidatorBootloader::class,
];

Note: if you are using spiral-packages/discoverer, you don't need to register bootloader by yourself.

Example of usage

<?php

declare(strict_types=1);

namespace App\Filters;

use Spiral\Filters\Model\FilterInterface;
use Spiral\Filters\Model\HasFilterDefinition;
use Spiral\Validator\FilterDefinition;
use Spiral\Filters\Attribute\Input\Post;
use Spiral\Filters\Attribute\Input\File;

class CreatePostFilter implements FilterInterface, HasFilterDefinition
{
    #[Post(key: 'title')]
    public string $title;
    
    #[Post(key: 'text')]
    public string $text;
    
    #[File]
    public UploadedFile $image;
    
    // ...

    public function filterDefinition(): FilterDefinitionInterface
    {
        return new FilterDefinition(
            validationRules: [
                'title' => [
                    ['notEmpty'],
                    ['string::length', 50]
                ],
                'text' => [['notEmpty']],
                'image' => [['image::valid'], ['file::size', 1024]]
                
                // ...
            ]
        );
    }
}

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Slim Framework view helper built on top of the Twig templating component

Slim Framework Twig View This is a Slim Framework view helper built on top of the Twig templating component. You can use this component to create and

QPM, the process management framework in PHP, the efficient toolkit for CLI development. QPM provides basic daemon functions and supervision mechanisms to simplify multi-process app dev.

QPM QPM全名是 Quick(or Q's) Process Management Framework for PHP. PHP 是强大的web开发语言,以至于大家常常忘记PHP 可以用来开发健壮的命令行(CLI)程序以至于daemon程序。 而编写daemon程序免不了与各种进程管理打交道。Q

This package provides some basic methods to implement a self updating functionality for your Laravel application. Already bundled are some methods to provide a self-update mechanism via Github or some private repository via http.

This package provides some basic methods to implement a self updating functionality for your Laravel 5 application. Already bundled are some methods to provide a self-update mechanism via Github.

The package provides definition syntax. Definition is describing a way to create and configure a service or an object.
The package provides definition syntax. Definition is describing a way to create and configure a service or an object.

Yii Definitions The package ... Requirements PHP 7.4 or higher. Installation The package could be installed with composer: composer require yiisoft/de

Provides database storage and retrieval of application settings, with a fallback to the config classes.

Provides database storage and retrieval of application settings, with a fallback to the config classes.

This extension provides a view renderer for Pug templates for Yii framework 2.0 applications.

This extension provides a view renderer for Pug templates for Yii framework 2.0 applications.

🎲 This project provides an integration for the Doctrine ORM and the Hyperf framework

Hyperf 🤝 Doctrine This project provides an integration for the Doctrine ORM and the Hyperf framework. Install composer require leocavalcante/hyperf-d

This extension provides Flysystem integration for the Yii framework

This extension provides Flysystem integration for the Yii framework. Flysystem is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one.

Comments
  • ArrayChecker add methods [isList, isAssoc]

    ArrayChecker add methods [isList, isAssoc]

    $data = [
      'listArray' => [1,2,3,4],
      'assocArray' => ['foo' => 1, 'bar' => 2]
    ];
    $validator->validate($data, [
      'listArray' => [
        ['array::isList']
      ],
      'assocArray' => [
        ['array::isAssoc']
      ]
    ]);
    
    enhancement 
    opened by gam6itko 1
  • StringChecker add methods [empty, notEmpty]

    StringChecker add methods [empty, notEmpty]

    I need a method that checks only strings on emptiness. If it's not string I don't care which value it contains. The checker type::notEmpty can't help me because it consider null as empty.

    $data = [
      'nullValue' => null,
      'emptyString' => '',
      'notEmptyString' => 'hi all',
    ];
    $validator->validate($data, [
      'nullValue' => [
        ['string::empty'] // false
      ],
      'emptyString' => [
        ['string::empty'] // true
      ],
      'emptyString' => [
        ['string::notEmpty'] // false
      ],
      'notEmptyString' => [
        ['string::empty'] // false
      ],
      'notEmptyString' => [
        ['string::notEmpty'] // true
      ],
    ]);
    
    enhancement 
    opened by gam6itko 1
  • ArrayChecker add method expectedValues witch limits available values

    ArrayChecker add method expectedValues witch limits available values

    $data = [
       'person' => ['good']
    ];
    $validator->validate($data, [
      'person' => [
        ['array::expectedValues', ['good', 'bad', 'ugly']]
      ]
    ]);
    
    enhancement 
    opened by gam6itko 1
Releases(1.1.0)
  • 1.1.0(Sep 29, 2022)

    What's Changed

    • Added default validator by @msmakouz in https://github.com/spiral/validator/pull/5

    Full Changelog: https://github.com/spiral/validator/compare/1.04...1.1.0.4...1.1.0

    Source code(tar.gz)
    Source code(zip)
  • 1.0.4(Sep 20, 2022)

    What's Changed

    • ArrayChecker::of checker can be an array by @gam6itko in https://github.com/spiral/validator/pull/4

    New Contributors

    • @gam6itko made their first contribution in https://github.com/spiral/validator/pull/4

    Full Changelog: https://github.com/spiral/validator/compare/1.0.3...1.0.4

    Source code(tar.gz)
    Source code(zip)
  • 1.0.3(Sep 16, 2022)

    What's Changed

    • Removed framework from dev dependencies by @msmakouz in https://github.com/spiral/validator/pull/3

    Full Changelog: https://github.com/spiral/validator/compare/1.0.2...1.0.3

    Source code(tar.gz)
    Source code(zip)
  • 1.0.2(Jul 28, 2022)

    What's Changed

    • Changing Dto to Model in namespaces by @msmakouz in https://github.com/spiral/validator/pull/2

    Full Changelog: https://github.com/spiral/validator/compare/1.0.1...1.0.2

    Source code(tar.gz)
    Source code(zip)
  • 1.0.1(Jul 19, 2022)

    What's Changed

    • Added Nylohm PSR-7/PSR-17 bridge support.
    • Fixed problem with getting data for validation from FilterBag

    Full Changelog: https://github.com/spiral/validator/compare/1.0.0...1.0.1

    Source code(tar.gz)
    Source code(zip)
Owner
Spiral Scout
Spiral Scout is a full-service digital agency, providing design, development and online marketing services to businesses around San Francisco and beyond.
Spiral Scout
This component may look complex, weird and full of hacks but it is a game changer for how we run PHP applications.

PHP Runtimes In early 2021, Symfony created a "Runtime component". This component may look complex, weird and full of hacks but it is a game changer f

Runtime 321 Dec 25, 2022
FreeSWITCH's Event Socket Layer is a TCP control interface enabling the development of complex dynamic dialplans/workflows

Asynchronous Event Socket Layer library for PHP Quickstart FreeSWITCH's Event Socket Layer is a TCP control interface enabling the development of comp

rtckit 3 Oct 11, 2022
Hello, this is simple attribute validation for PHP Models, based on the new features, presented in PHP 8

Hello, this is simple attribute validation for PHP Models, based on the new features, presented in PHP 8 It works as a standalone and can be use in custom projects or in libraries like Symfony and Laravel.

Ivan Grigorov 88 Dec 30, 2022
An Hydrator class that can be used for filling object from array and extracting data from objects back to arrays.

Hydrator namespace: Meow\Hydrator Library that can hydrate (fill object with data from array) and extract data from object back to array. Installation

Meow 2 Feb 3, 2022
🎁 Datagrid component project skeleton based on Nette Framework

?? Datagrid component project skeleton based on Nette Framework

Contributte 4 Dec 14, 2022
This package provides a high performance HTTP server to speed up your Laravel/Lumen application based on Swoole.

This package provides a high performance HTTP server to speed up your Laravel/Lumen application based on Swoole.

Swoole Taiwan 3.9k Jan 8, 2023
The Runtime Component enables decoupling applications from global state.

Runtime Component Symfony Runtime enables decoupling applications from global state. This Component is experimental. Experimental features are not cov

Symfony 409 Jan 3, 2023
Prado - Component Framework for PHP 7

Prado PHP Framework PRADO is a component-based and event-driven programming framework for developing Web applications in PHP 7. PRADO stands for PHP R

The PRADO Group 182 Dec 17, 2022
The Semaphore Component manages semaphores, a mechanism to provide exclusive access to a shared resource.

Semaphore Component The Semaphore Component manages semaphores, a mechanism to provide exclusive access to a shared resource. Resources Documentation

Symfony 29 Nov 16, 2022
Main component controls and coordinates saga participants

Saga Orchestrator Table Of Contents Introduction Requirements About package Installation Getting started Configuration Saga creation Internal License

null 17 Jan 1, 2023