All Projects → peakphp → array-validation

peakphp / array-validation

Licence: MIT License
Validation utilities for array structure

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to array-validation

fqdn
RFC-compliant FQDN validation and manipulation for Python.
Stars: ✭ 23 (+64.29%)
Mutual labels:  validation
react-form-validation-demo
React Form Validation Demo
Stars: ✭ 88 (+528.57%)
Mutual labels:  validation
openapi-schema-validator
OpenAPI schema validator for Python
Stars: ✭ 35 (+150%)
Mutual labels:  validation
ra-ra
A C++20 array / expression template library with some J/APL features
Stars: ✭ 22 (+57.14%)
Mutual labels:  array
python-valid8
Yet another validation lib ;). Provides tools for general-purpose variable validation, function inputs/outputs validation as well as class fields validation. All entry points raise consistent ValidationError including all contextual details, with dynamic inheritance of ValueError/TypeError as appropriate.
Stars: ✭ 24 (+71.43%)
Mutual labels:  validation
exvalibur
Elixir Validator Generator
Stars: ✭ 14 (+0%)
Mutual labels:  validation
ng2-multi-step-wizard-ui-router1
Series 3: Tutorials on creating an Angular 2 Multi-Step Wizard using UI-Router 1.0 and TypeScript 2.0.10
Stars: ✭ 33 (+135.71%)
Mutual labels:  validation
yform standalone validator
YForm-Erweiterung: Validieren von PHP Arrays ohne HTML-Formular
Stars: ✭ 23 (+64.29%)
Mutual labels:  validation
peppermint
Declarative data validation framework, written in Swift
Stars: ✭ 37 (+164.29%)
Mutual labels:  validation
valid8
Valid8 - Super Simple Bootstrap Form Valiation
Stars: ✭ 17 (+21.43%)
Mutual labels:  validation
verum-php
Server-Side Validation Library for PHP
Stars: ✭ 17 (+21.43%)
Mutual labels:  validation
arr-flatten
Recursively flatten an array or arrays. This is the fastest implementation of array flatten.
Stars: ✭ 55 (+292.86%)
Mutual labels:  array
array-diff-multidimensional
Compare the difference between two multidimensional arrays in PHP
Stars: ✭ 60 (+328.57%)
Mutual labels:  array
liquibase-linter
Quality control for your Liquibase scripts
Stars: ✭ 15 (+7.14%)
Mutual labels:  validation
ValidatedTextInputLayout
An extension to Android Design Support library's TextInputLayout with integrated validation support
Stars: ✭ 54 (+285.71%)
Mutual labels:  validation
MCUCapture
Utility for plotting array data from MCU RAM
Stars: ✭ 22 (+57.14%)
Mutual labels:  array
NZ-Bank-Account-Validator
A small, zero dependency NZ bank account validation library that runs everywhere.
Stars: ✭ 15 (+7.14%)
Mutual labels:  validation
alvito
Alvito - An Algorithm Visualization Tool for Python
Stars: ✭ 52 (+271.43%)
Mutual labels:  array
cuda memtest
Fork of CUDA GPU memtest 👓
Stars: ✭ 68 (+385.71%)
Mutual labels:  validation
SNAP
Easy data format saving and loading for GameMaker Studio 2.3.2
Stars: ✭ 49 (+250%)
Mutual labels:  array

Peak/ArrayValidation

version Total Downloads License

Installation

 composer require peak/array-validation

What is this?

This component help you to validate array structure by:

  • validating the type of any key values
  • ensuring a data structure with expected keys requirements
  • preventing structure pollution by allowing only a set of keys

This is especially useful when dealing with json data request, before using the data, you must validate his content so you can afterward check the value of those keys with your business logic without worrying about the type or presence of any key value.

How to use

8 Usages

1- General validation "à la carte" (stateless)

$validator = new Validator();

if ($validator->expectExactlyKeys($data, $keys) === true) {
    // ...
}

2- Validation with fluent interface (stateful)

$data = [ // data
    'tags' => [],
    'name' => 'foobar'
];
$validation = new Validation($data);

$validation
    ->expectExactlyKeys(['tags', 'name'])
    ->expectKeyToBeArray('tags');
    ->expectKeyToBeString('name');

if ($validation->hasErrors()) {
    // $lastError = $validation->getLastError();
    // $errors = $validation->getErrors();
}

3- Strict validation with fluent interface (stateful)

$validation = new StrictValidation($data);

// will throw an exception if any of tests below fail
$validation
    ->expectOnlyKeys(['id', 'title', 'description', 'isPrivate', 'tags'])
    ->expectAtLeastKeys(['id', 'title', 'description'])
    ->expectKeyToBeInteger('id')
    ->expectKeysToBeString(['title', 'description'])
    ->expectKeyToBeBoolean('isPrivate')
    ->expectKeyToBeArray('tags');

// if we reach this point, it means the array structure is
// valid according to the validation rules above.

4- Create a ValidationDefinition for later usage

$vDef = new ValidationDefinition();
$vDef
    ->expectOnlyKeys(['title', 'content', 'description'])
    ->expectAtLeastKeys(['title', 'content'])
    ->expectKeysToBeString(['title', 'content', 'description']);

$validation = new ValidationFromDefinition($vDef, $data);

if ($validation->hasErrors()) {
    // $validation->getErrors();
}

5- Create a validation Schema for later usage

Schema is just another way to write validation definitions. This format is ideal when you want to store schemas in file (ex: json, php array file, yml, etc.)

$mySchema = [
    'title' => [
        'type' => 'string',
        'required' => true
    ],
    'content' => [
        'type' => 'string',
        'nullable' => true,
    ],
];

$schema = new Schema(new SchemaCompiler(), $mySchema, 'mySchemaName');

$validation = new ValidationFromSchema($schema, $data);

if ($validation->hasErrors()) {
    // $validation->getErrors();
}

6- Strict validation using ValidationDefinition

// all validation definitions are executed at object creation and an exception is thrown if any of tests failed
new StrictValidationFromDefinition($validationDefinition, $arrayToValidate);

7- Strict validation using Schema

// all validation definitions are executed at object creation and an exception is thrown if any of tests failed
new StrictValidationFromSchema($schema, $arrayToValidate);

8- Validation and Strict Validation with ValidationBuilder

$validation = new ValidationBuilder();
$validation
    ->expectOnlyKeys(['title', 'content', 'description'])
    ->expectAtLeastKeys(['title', 'content'])
    ->expectKeysToBeString(['title', 'content', 'description']);

if ($validation->validate($data) === false)  {
    // $validation->getErrors();
    // $validation->getLastError();
}

and with strict validation:

//  will throw an exception if any of tests fail
$validation->strictValidate($data);

Validation methods

interface ValidationInterface
{
    public function expectExactlyKeys(array $keys);
    public function expectOnlyOneFromKeys( array $keys);
    public function expectAtLeastKeys(array $keys);
    public function expectOnlyKeys(array $keys);
    public function expectNKeys(int $n);
    public function expectKeyToBeArray(string $key, bool $acceptNull = false);
    public function expectKeysToBeArray(array $keys, bool $acceptNull = false);
    public function expectKeyToBeInteger(string $key, bool $acceptNull = false);
    public function expectKeysToBeInteger(array $keys, bool $acceptNull = false);
    public function expectKeyToBeFloat(string $key, bool $acceptNull = false);
    public function expectKeysToBeFloat(array $keys, bool $acceptNull = false);
    public function expectKeyToBeString(string $key, bool $acceptNull = false);
    public function expectKeysToBeString(array $keys, bool $acceptNull = false);
    public function expectKeyToBeBoolean(string $key, bool $acceptNull = false);
    public function expectKeysToBeBoolean(array $keys, bool $acceptNull = false);
    public function expectKeyToBeObject(string $key, bool $acceptNull = false);
    public function expectKeysToBeObject(array $keys, bool $acceptNull = false);
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].