nuwave / nuwave/lighthouse

Correctly handle leaf inputs that were cast to PHP native enum instances

Open
#2,209 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs reproduction
Dominant language
PHP
Stars
3.5k
Forks
468
Avg merge
3h 9m
Merged PRs (30d)
2

Description

Describe the bug

Enum passed to validator resulting some dependant rules not working, for example: required_if

enum ItemType: string
{
    case SKU = "sku";
    case VOUCHER = "voucher";
}
enum ItemType {
  SKU
  VOUCHER
}
input OrderItem {
  type: ItemType!
  uuid: String @rules(apply: ["required_if:type,sku"])
  code: String @rules(apply: ["required_if:type,voucher"])
}

at the end, the ItemType enum will passed to the Validator::validateRequiredIf function and it will compare it with a string, which is always false

see Laravel's ValidatesAttributes.php line 1589

Expected behavior/Solution

Probably convert all enum into its value/name before passing it to validator, but not sure any efficient way to do it or not.

Steps to reproduce

See the explanation above

Lighthouse Version
v5.61.0

-- Update --

Some workaround solution
// Make validator class to replace the value before validation

class MyValidator extends \Illuminate\Validation\Validator
{
    public function parseDependentRuleParameters($parameters): array
    {
        [$values, $other] = parent::parseDependentRuleParameters($parameters);
        if ($other instanceof UnitEnum) {
            $other = $other->name;
        }
        return [$values, $other];
    }

    public function getDisplayableValue($attribute, $value): string
    {
        if ($value instanceof UnitEnum) $value = $value->name;
        return parent::getDisplayableValue($attribute, $value);
    }
}
// replace the original validator somewhere in service provider

$this->app->afterResolving('validator', function (ValidatorFactory $factory){
    $factory->resolver(fn(...$params)=>new MyValidator(...$params));
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the GraphQL schema and enum example in the issue, then inspect Laravel's ValidatesAttributes.php around validateRequiredIf and the referenced dependent-parameter handling. Compare the reported behavior with the MyValidator workaround. Done means required_if and similar dependent rules correctly handle native PHP enum inputs, with coverage for the reproduced schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, laravel, php
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.