yiisoft / validator

Yii validator library
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
112 stars 39 forks source link

Ability add base result before validation #598

Closed vjik closed 1 year ago

vjik commented 1 year ago

My suggestion: add ability add base result to Context and use here.

Use case: input model with prevalidation raw data.

samdark commented 1 year ago

What is prevalidation data?

vjik commented 1 year ago

Raw values and populated values can be different. My service validate raw data into populate process and then run classic validation.

For example:

final class SimpleModel implements ValidatedModelInterface
{
    use ValidatedModelTrait;

    public function __construct(
        #[PreValidate(new Required())]
        #[Length(min: 3, skipOnError: true)]
        public string $a = '.',
        public string $b = '.',
        public string $c = '.',
    ) {
    }
}

$service->create(SimpleModel::class, data: []);

In raw data a not exists and Required add error. Then length should not add error because option skipOnError is true.

BoShurik commented 1 year ago

Why don't you use separate models: for user input (with raw data) and model with populated data (converted from user input)?

vjik commented 1 year ago

Why don't you use separate models: for user input (with raw data) and model with populated data (converted from user input)?

Not convient. One input model is simple.

samdark commented 1 year ago

It is a lazy approach that is convenient at first but:

  1. It complicates validator.
  2. The use-case is not common enough.
  3. With application growth these two layer inputs are likely to diverge more and such approach will create more problems than it is trying to solve.

Overall, I don't think it is a good idea.

vjik commented 1 year ago

Decided don't make it, because use-case is changed: if pre validation is fail then not run validation.