lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Respect order of `@validator` compared to other field middleware directives

Open faiverson opened this issue 3 years ago • 4 comments

If we have a simple mutation like this with both directives

createUser(input: inputUser! @spread): User
    @create
    @validator
    @can(ability: "create")

@validator is not respecting the order. I'm expecting to execute @can first instead I get validation errors.

I think @validator should respect the order in the fields directives

faiverson avatar Sep 25 '20 18:09 faiverson

As of now, all validation runs in a discrete step that happens before all field middleware directives. That is because the validation rules from @rules, @validator and other validation directives are gathered and applied at once.

Keep in mind that @validator also works on input objects, in which case no field directive may be present, e.g.:

type Query {
  foo(input: Foo): Bar @can
}

input Foo @validator { ... }

spawnia avatar Sep 27 '20 20:09 spawnia

Thanks for the answer.

I understand the problem, but imho access authorization should be before any kind of validation. You might don't want to share any validation problem if an user doesn't have access. Permissions come first to me in any system.

Also I understand is a hard change, do you think is worth to considerer a refactoring to achieve this order logic? If not, any idea how can I change my own order?

faiverson avatar Sep 28 '20 15:09 faiverson

Hi,

In 4x release @validator directive doesn't exists. Only in master branch this directive is available, should I follow the master branch in composer or wait this feature to be merged into 4x branch ?

Thanks,

canatufkansu avatar Oct 07 '20 08:10 canatufkansu

@canatufkansu I do not plan to backport any features to v4. @validator requires substantial changes, so it can not be done in a backwards compatible way without a lot of extra work.

spawnia avatar Oct 09 '20 10:10 spawnia