2017 © Pedro Peláez
 

library validate

Data validation library for MobileXCo

image

denis/validate

Data validation library for MobileXCo

  • Friday, November 28, 2014
  • by d3berger
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Validate

A sample data validation library. The goal was to create a data validation library as a composer module. This was a test requested by Mobile X Co., (*1)

Installing

This library can be installed with composer. To get composer:, (*2)

curl -sS https://getcomposer.org/installer | php

Add this to your composer.json file., (*3)

{
    "require" : {
        "denis/validate": "dev-master"
    }
}

Then run composer install., (*4)

php composer.phar install

How to use

The schema is supplied in json format to the parse method. The isValid method is then called which returns a json response. The response will indicate if the data string is valid compared to the schema provided. If there are errors they will be in the response., (*5)

require_once 'vendor/autoload.php';

use MobileXCo\Validate;

$validate = new Validate();

$validate->parse($schema);

$result = $validate->isValid($data);

Data types supported

  • string (A string)
  • int (An integer)
  • email (Must be a string. Example "denis@gmail.com")
  • date (Must be a string. Example "2014-09-13")

Extra constraints

Elements can be marked as required by adding the required parameter. Type string and int can have a min and max length., (*6)

Testing

Testing with PHPUnit can be found under test folder., (*7)

Schema Example

{
    "id": {
        "type": "int",
        "required": true
    },
    "name": {
        "type": "string",
        "min": 5,
        "max": 30,
        "required": true
    },
    "description": {
        "type": "string",
        "max": 50
    },
    "price": {
        "type": "int",
        "min": 1
    },
    "orderOn": {
        "type": "date"
    },
    "address": {
        "type":"email"
    }
}

Data Examples

{
    "id": 25,
    "description": "Blue car."
}

isValid -> false, error: [name is required], (*8)

{
    "id": 21,
    "name": "Red car"
}

isValid -> true, error: [], (*9)

The Versions

28/11 2014

dev-master

9999999-dev

Data validation library for MobileXCo

  Sources   Download

The Requires

  • php >=5.3.0

 

by Denis Bergeron