auraphp / Aura.Filter

Validate and sanitize arrays and objects.
MIT License
159 stars 33 forks source link

Fix Subfilter support #156

Open harikt opened 2 years ago

harikt commented 2 years ago

Related to https://github.com/auraphp/Aura.Filter/issues/155 .

Support maximum of 2.

<?php
require __DIR__ . '/vendor/autoload.php';
$filter_factory = new \Aura\Filter\FilterFactory();
$filter = $filter_factory->newSubjectFilter();

$filter->validate('id')->is('int');
$filter->validate('url')->is('url');

$user_spec = $filter->subfilter('user'); // add a "SubSpec"
$user_filter = $user_spec->filter();  // Get the "SubSpec" SubjectFilter

$user_filter->validate('given-name')->isNotBlank();
$user_filter->validate('age')->is('int');
$user_filter->validate('gender')->is('strlen', 1);

$data = (object) [];

$result = $filter->apply($data);
$messages = $filter->getFailures()->getMessages();

var_dump($messages);

RESULT :

array(3) {
  ["id"]=>
  array(1) {
    [0]=>
    string(31) "id should have validated as int"
  }
  ["url"]=>
  array(1) {
    [0]=>
    string(32) "url should have validated as url"
  }
  ["user"]=>
  array(3) {
    ["given-name"]=>
    array(1) {
      [0]=>
      string(37) "given-name should not have been blank"
    }
    ["age"]=>
    array(1) {
      [0]=>
      string(32) "age should have validated as int"
    }
    ["gender"]=>
    array(1) {
      [0]=>
      string(41) "gender should have validated as strlen(1)"
    }
  }
}
harikt commented 2 years ago

The original PR was from @jakejohns https://github.com/auraphp/Aura.Filter/pull/131 .

I spend quite sometime to make this happen. There are some glitches. But I am not sure how we can overcome that.

If anyone have better ways we can consider that also.

harikt commented 2 years ago

Anyone else interested in contributing can comment on this PR. I am happy to hear criticims. So no issues for raising the bar.