Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'"" should not be empty' when using Each() #924

Closed
bertramakers opened this issue Feb 28, 2018 · 3 comments
Closed

'"" should not be empty' when using Each() #924

bertramakers opened this issue Feb 28, 2018 · 3 comments

Comments

@bertramakers
Copy link

Hi! I was trying out this library to validate some JSON, and I'm really impressed how feature-rich it is.

That being said, I'm getting some unclear error messages when I assert that an array has no empty values.

For example, I'm trying to validate the following code (snippet):

{
  "title": {
    "en": "Example title",
    "nl": "Voorbeeld titel",
    "fr": ""
  }
}

I have some custom validators, one for the values:

use Respect\Validation\Rules\NotEmpty;
use Respect\Validation\Validator;

class TitleValidator extends Validator
{
    public function __construct()
    {
        $rules = [
            new NotEmpty(),
        ];

        parent::__construct($rules);
    }
}

One for the language codes (keys):

use Respect\Validation\Rules\Regex;
use Respect\Validation\Validator;

class LanguageValidator extends Validator
{
    public function __construct()
    {
        $rules = [
            new Regex('/^[a-z]{2}$/'),
        ];

        parent::__construct($rules);
    }
}

And finally one to combine them:

use Respect\Validation\Rules\Each;
use Respect\Validation\Rules\Length;
use Respect\Validation\Validator;

class TranslatedTitleValidator extends Validator
{
    public function __construct()
    {    
        $rules = [
            new Each(
                new TitleValidator(),
                new LanguageValidator()
            ),
            new Length(1, null, true),
        ];

        parent::__construct($rules);
    }
}

Then I use them to validate an array like this:

$v = Validator::key('title', new TranslatedTitleValidator(), true);
$v->assert(
    [
        'title' => [
            'en' => 'Example title',
            'nl' => 'Voorbeeld titel',
            'fr' => '',
        ]
    ]
);

I'd expect a message along the lines of:

title.fr should not be empty

However instead I get:

"" should not be empty

Which is confusing for our API clients (because in reality we have more properties than just title).
Is there any way that I configure the TitleValidator in the Each to use the parent validator name + the key?

(Note that I'm using the actual classes and not the aliases because we want to re-use some of the rules across our codebase. For example, we have multiple document types with the title property.)

@bertramakers
Copy link
Author

To clarify, the translations are not limited to en, nl and fr but can be any language code. So I cannot do something like this because it would be too limited:

$rules = [
    new Key('title.en', new TitleValidator(), true),
    new Key('title.nl', new TitleValidator(), true),
    new Key('title.fr', new TitleValidator(), true),
];

@nickl-
Copy link
Member

nickl- commented Jul 11, 2018

All the validation errors will report on the supplied value that failed validation. This is because the validator wouldn't know if you are referencing a variable, an array or an object field. The validator only knows that the given value has failed and this is what is reported in the most expressive way possible.

The only way I can think of for you to produce a contextual error as you want would be to nest the validation rules so that you know which title validator failed for which specific language and then implement a custom public function reportError($input, array $extraParams = []) to include the information you will then have at your disposal into the message produced.

In other words TranslatedTitleValidator will validate each TitleValidator which in turns validates each LanguageValidator then at any given point you will know which language failed and produce the errors accordingly.

Hope this helps...

@henriquemoody
Copy link
Member

With so many open issues, it's become hard to really know what's really important and what I have fixed already.

I'm closing issues that are too old, and I'm not really looking at them all. If this is still relevant, please comment here and I will reopen this issue.

Thanks for reporting! 🐼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants