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
Schema for unified automatic configuration validation #191
Conversation
Great! Only one thing. Did you think about something like Expect::arrayOf([
'dsn' => Expect::string()->required(), // Have to be presented, but CAN be null?
'user' => Expect::enum(Expect::string(), null), // Have not to be presented and CAN be null
'reflection' => Expect::string(), // Have not to be presented but CANNOT be null?
//...
]) Maybe this look better Expect::arrayOf([
'dsn' => Expect::string()->required(), // Have to be present and CANNOT be null
'user' => Expect::string()->nullable(), // Have not be presented and CAN be null
'reflection' => Expect::string(), // Have not to be presented but CANNOT be null
'password' => Expect::string()->required()->nullable(), // Have to be presented but CAN be null
//...
]) Or am I missing something? |
@dg why not making class for config, that way we're far more strict and full autocomplete in all ides, and with php7.4 we will have typehints on properties? Wouldn't that be far better then this dynamic validation? |
} | ||
|
||
|
||
public static function type(string $type): self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about:
function type(string ...$types): self
{
return new self(implode('|', $types));
}
To be able write:
Expect::type('string', Nette\DI\Definitions\Statement::class)
or own method for it.
@dakorpar I also thought about using the class for configuration and it's definitely a good idea. |
7e4108f
to
d9f7d5f
Compare
c84fbc6
to
3f1cadd
Compare
8d57c37
to
d2d68df
Compare
02ef205
to
9bd6554
Compare
1adcd1c
to
18fdc11
Compare
Cool! Can I somehow use the system to validate what users have in their |
@ondrejmirtes of course! |
@holantomas I added |
Hi, thanks for this I really like it! |
Are you sure? Afaik return types do not trigger autoloading. |
I am not sure, thanks. It was a habit from compiled languages. |
CompilerExtension can provide a schema which is automatically used to validate and normalize configuration:
These rules are used:
Expect::bool()->required()
[]
for array, list and struct) unless you change it viaExpect::bool()->default($default)
or simplyExpect::bool($default)
Expect::type('string|null')
orExpect::type('string|stdClass[]')
etc.This schema describes array with exactly two items:
Extra items can be allowed via
otherItems($type)
, ie:You can use constraints
min()
andmax()
to number values, but also size of strings or arrays:You can add additional assertions:
Before configuration is processed, it can be normalized:
Examples of usage: