nette / schema

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

Required with parameter to set to true or false. #15

Closed josefsabl closed 4 years ago

josefsabl commented 4 years ago

It would be useful if required() method had fingerprint like this:

public function required(bool $required = true): self

Imagine you want to generate the schema programmatically either to require all options or to allow for only partial result. Imagine some create operation versus update operation where either I have to provide all fields or only ones I want to edit.

public static function getSchema(bool $partialAllowed) {
        return Expect::structure(
            [
                'name' => Expect::string()->required(!$partialAllowed),
            ]
        );
}

I am implementing restfull api, particularly post, put and patch methods and this would hugely declutter my code.

I will send PR shortly.

dg commented 4 years ago

Closed by #16