auraphp / Aura.Filter

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

Multidimensional Subjects and Variable Length Members #117

Closed jakejohns closed 7 years ago

jakejohns commented 8 years ago

Multidimensional Subjects, Variable Length Members, and the possible case for injecting a factory into a subject filter?

I have a couple use cases which I have solved in various hacky ways and am curious if others have insight into better solutions, or if a PR is in order.

Multi dimensional

I think it might be useful if the SubjectFilter had the FilterFactory injected into it, so that I could create new 'subfilters' (probably just another SubjectFilter) to validate members of the subject which are themselves data structures (array/objects) which have their own criteria.

// Given something like this:
$data = [
    'id' => 1,
    'user' => [
        'name' => 'Foo',
        'age' => 42,
    ],
    'url' => 'http://example.com'
];

// I want to be able to do something like this:
$subject = $factory->newSubjectFilter('My\Filter');

// .. in My\Filter::init()
$this->validate('id')->is('int');
$this->validate('url')->is('url');

$user = $this->subfilter('user'); // create a "sub-filter" ?
// $user = $this->subfilter('user', 'My\Filter\User'); // maybe pass class
$user->validate('given-name')->isNotBlank();
$user->validate('age')->is('int');

// or if not injecting the factory, and providing a method, perhaps a way to
// just inject a filter to a spec? but I don't think I like this as much.
$subject = $factory->newSubjectFilter('My\Filter');
$name = $factory->newSubjectFilter('My\Filter\Name');
$subject->setSubFilter('name', $name);

Variable length arrays

Additionally, I often have a situation where user fills out a HTML form with some JS crap to add additional fields of some kind, ie:

<!-- //... other inputs and stuff -->
<div>
    <label for="date1">Date</label>
    <input id="date1" name="dates[]" />
</div>
<div>
    <label for="date2">Date</label>
    <input id="date2" name="dates[]" />
</div>
<button>click to add another date</button>

I think I want to be able to do something like this:

// My\Filter::init()

//.. init validation/sanitize other inputs...

// some kind of array_filter function?
// ie. unset blank values?
$this->sanitize('dates')->to('nonblank');

$this->validate('dates')->is(
    'array',  // subject? collection? countable?
    $minCount,
    $maxCount
);

// This is a little weird because I don't think this would be a value filter or
// a subjct filter?
$dateSpec = $this->newSubjectValueFilter();

// set some rules
$dateSpec->validate()->isNotBlank();
$dateSpec->validate()->is('dateTime');
$dateSpec->sanitize()->to('dateTime', 'Y-m-d');

// use spec for each
$this->filterEach('dates', $dateSpec);

One weird thing here is that $dateSpec isn't really a SubjectFilter as it's not a collection of fields itself, but also not a ValueFilter as I don't think you can configure a spec for a value filter like you do a subject filter, it's more a one off thing, right?

... so anyway. Any thoughts here? Am I approaching this wrong? People solve similar problems in different ways? Or maybe have similar issues and think we should try to hash out a solution?

designermonkey commented 8 years ago

I have just stumbled across this kind of issue myself today, and was about to ask for advice here on how to handle multidimensional forms.

In my case, I have

fields[groups][][name]
fields[groups][][ingredients][][quantity]
fields[groups][][ingredients][][measurement]
fields[groups][][ingredients][][ingredient]
fields[groups][][ingredients][][details]

Which poses me some problems and complex code I think.

designermonkey commented 8 years ago

@pmjones is there any discussion to be had on this? Do you think it's a valid idea?

jakejohns commented 8 years ago

@designermonkey, seem like @pmjones has been consumed by producer/bookdown/atlas for some time now. Based on the half life of some of my other open PR/Issues in other projects of fearless-leaders concern, it might be a min. Maybe @harikt has something insightful to say here? He often chimes in.

designermonkey commented 8 years ago

:)

harikt commented 8 years ago

Hi all,

I have nothing special / valuable information to share about this, ie why I am staying / stayed away. If I find something will mention here.

Thank you :-) .

pmjones commented 8 years ago

@jakejohns @designermonkey It is appealing to have a sub-filter and a repeated-application filter. I confess that intuitively I think it's going to be difficult and ugly, but I am willing to entertain PRs around them. My guess is that they will end up being more trouble than they are worth, but if you want to try it out and present your findings, I'll give them my attention.

pmjones commented 7 years ago

At this point, I think it would be best (if anyone is still interested) to pursue this as a PR against the 3.x branch.