auraphp / Aura.Filter

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

Add skipBlankRule method for sanitize too #157

Open Gruven opened 2 years ago

Gruven commented 2 years ago

In addition to #137 I think there should be an option to skip blank rule for sanitize too

harikt commented 2 years ago

Why do we want to skip a field if it is blank. There will be no harm if you want to run the sanitize on blank value.

Gruven commented 2 years ago

@harikt not skip blank field, but skip blank rule. Try this code for example

$subject = ['field' => null];
$filterFactory = new FilterFactory();
$filter = $filterFactory->newSubjectFilter();

// as now works
$filter->sanitize('field')->to('callback', static fn($subject, $field) => true);

// as it should work
// $filter->sanitize('field')->skipBlankRule()->to('callback', static fn($subject, $field) => true);

$filter->apply($subject);
echo $filter->getFailures()->getMessagesForFieldAsString('field');

Now it says field should have sanitized to callback(*Closure*). But if we use skipBlankRule, there will be no error.