nuwave / nuwave/lighthouse

Validation attribute translations for nested arguments

Open
#2,227 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What problem does this feature proposal attempt to solve?

I'm struggling with translations, the problem is when returning the validation errors, if the args were nested it spits out something like input.name and it doesnt resolve __('validation.attributes.name') like it would with a normal laravel validator due to the input. prefix

The required translation from the validation.php file in lang directory:
'required' => 'ข้อมูล:attribute จำเป็นต้องกรอก',

Paired with 'attributes' => ['name' => 'TRANSLATION HERE'] in the same file

Lighthouse will spit out:
'input.name' => 'ข้อมูลinput.name จำเป็นต้องกรอก'

Because of the input. prefix, it doesn’t resolve and translate the attribute in the message

Is there a way to get around this without having to duplicate all of the attributes and prefix them with input. (what if it’s nested further, like a relation, you have to duplicate them all further with the same value but a different key

https://laravel.com/docs/9.x/validation#specifying-attribute-in-language-files

Which possible solutions should be considered?

If it’s not currently possible, my proposed solution would be to keep the key the same input.name in the returned error list, but pass through the name from the rules for the message name instead of input.name

This would be a breaking change, so maybe we could make it a toggled feature in the config or a directive or something? Open to suggestions

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 lang/validation.php file and Laravel’s documented attribute translation behavior, then trace how Lighthouse represents nested validation arguments. Done should preserve input.name as the returned error key while using name for message translation, with the chosen configuration or directive behavior and breaking-change implications documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, laravel, php
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.