-
-
Notifications
You must be signed in to change notification settings - Fork 30
Description
Let's say I validate the data structure of a blog post. This blog post has a body and an author. And as we support anonymous comments, the user is NOT mandatory. The author is a structure as well and if it IS PROVIDED it MUST have an e-mail.
I have a schema like this:
- comment: structure
- body: string (required)
- author: structure (not required)
- email: string (required)
- name: string (not required)
At the moment I am unable to validate the data with Nette/Schema like that because if I mark the email as required I still get a validation exception even if the author (not required) is not provided at all.
My validation schema code is like:
Expect::structure([
'body' => Expect::string()->required(),
'author' => Expect::structure([
'email' => Expect::string()->required(),
'name' => Expect::string(),
]),
]);
And for data like this:
- body: Hello World!
I get The mandatory option 'author > email' is missing.. I get the same result even if I explicitly set the structure as not required like this: 'author' => Expect::structure([ ... ])->setRequired(false).
This example is obviously all made up to make things clear but I've hit this issue three times in different places. One of them being writing MongoDB extension for DI and trying to validate the driverOptions (see the context, structure inside structure, but context not mandatory)
Is this a bug? Is it a feature? Or am I missing something?
We are obviously able to work around this by explicitly checking the values in all sorts of places but it is a shame we have to pollute our codebase, especially when we streamlined it pretty much by implementing Nette\Schema in the first place :-)