graphql-laravel: Laravel's custom rules are not working on rules function of a mutation.

Versions:

  • graphql-laravel Version: 1.22
  • Laravel/Lumen Version: 5.8.*
  • PHP Version: 7.1.3

Question:

I created a custom rule for my validation and applying it on rules function of a mutation doesnt work.

code:

public function rules(array $args = []) {
        return [
              'name' => [
                    'unique:msp_branches,name,'.$args['id'], 
                    new EmptyStringRule   //custom rule
              ]
        ];
}

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15

Most upvoted comments

Thanks for the clarification.

What you’re describing is the expected behavior of the Laravel validation.

Custom validations using Validator::extend or rule objects implementing \Illuminate\Contracts\Validation\Rule are skipped for empty attributes.

To still have them applied, you need to use Validator::extendImplict or implement the \Illuminate\Contracts\Validation\ImplicitRule interface.

Please read up the chapter about Implicit Extensions:

By default, when an attribute being validated is not present or contains an empty string, normal validation rules, including custom extensions, are not run. … For a rule to run even when an attribute is empty, the rule must imply that the attribute is required. To create such an “implicit” extension, use the Validator::extendImplicit()

Unfortunately the docs don’t mention that this is also possible/available for rule objects => I’ve created https://github.com/laravel/docs/pull/5237 to clarify this.

I’ve found a source which describes this quite practically: https://laravel-news.com/custom-validation-rule-objects (but I understand you have to be even ware this concept exists to notice it).

Closing the issue as it’s unrelated to this library; thanks for reporting!