CSV validator library.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kalpaitch f9553115ee Update 'composer.json' 4 years ago
src/Deblan/CsvValidator deblan/csv upgraded to the v2 and more units tests 5 years ago
tests deblan/csv upgraded to the v2 and more units tests 5 years ago
.gitignore Insight analyse fixes 5 years ago
LICENSE Insight analyse fixes 5 years ago
README.md CI 5 years ago
composer.json Update 'composer.json' 4 years ago
example.php Insight analyse fixes 5 years ago
phpci.yml phpci file 5 years ago
phpunit.xml init 7 years ago



CSV validator library

That uses the constraints of Symfony framework: http://symfony.com/doc/current/reference/constraints.html.


You need composer:

composer require deblan/csv-validator



use Deblan\Csv\CsvParser;
use Deblan\CsvValidator\Validator;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Date;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

require __DIR__.'/vendor/autoload.php';

// Initialisation of the validator
$validator = new Validator();

// The first field must contain an email
$validator->addFieldConstraint(0, new Email());

// The second field must contain a date
$validator->addFieldConstraint(1, new Date());

// Validate the legend
$validator->setExpectedHeaders(['foo', 'bar', 'bim']);

// An line must contain 3 columns
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
    if (count($data) !== 6) { // 6 because of the legend (3 fields * 2)
        $context->addViolation('The line must contain 3 columns');

// Initialisation of the parser
$parser = new CsvParser();


if ($validator->isValid() === false) {
    foreach ($validator->getErrors() as $error) {
        $line = $error->getLine();
        $column = $error->getColumn();
        $message = $error->getViolation()->getMessage();

        echo <<<EOF
    <li>Line: $line</li>
    <li>Column: $column</li>


Run example.php and see results:

    <li>Line: 1</li>
    <li>Column: </li>
        <p>Invalid legend.</p>
    <li>Line: 4</li>
    <li>Column: </li>
        <p>The line must contain 3 columns</p>
    <li>Line: 2</li>
    <li>Column: 1</li>
        <p>This value is not a valid email address.</p>
    <li>Line: 3</li>
    <li>Column: 2</li>
        <p>This value is not a valid date.</p>


  • Simon Vieille
  • Gautier Deruette