nette / schema

📐 Validating data structures against a given Schema.
https://doc.nette.org/schema
Other
905 stars 26 forks source link

min/max/assert not applyed on default value #30

Open ncou opened 4 years ago

ncou commented 4 years ago

Hi,

It seems the controles like min/max or assert() are not appplyed on the default value. This could be problematic if the default value is dynamic.

In my use case i should controle the application key is a hexa 64 chars, and by default we use the environement variable APP_KEY.

I also tryed to move the min/max/assert function after the chained ->default(), but the result is the same :(

return Expect::structure([ 'key' => Expect::xdigit()->min(64)->max(64)->default(env('APP_KEY')), ]);

Is that a bug or an "expected" behaviour ?

Keep up the good work.

dg commented 3 years ago

Sometimes the default value doesn't really follow the rules, for example Expect::string() has default value null, but it doesn't allow null. So it's more like expected behaviour.

ncou commented 3 years ago

Yes you are right, but it seems more logic to enforce the rules if the default value is explicitely defined. And have an error in the illogical cases like Expect::string()->default(123)