valibot icon indicating copy to clipboard operation
valibot copied to clipboard

the lang prop from the global configuration overrides a message from the method v.message

Open yurrko opened this issue 6 months ago • 2 comments

import * as v from 'valibot';
import '@valibot/i18n/de';

v.setGlobalConfig({ lang: 'de' });

const Schema = v.object({
  email: v.message(v.pipe(v.string(), v.email()), 'Method v.message'),
  password: v.pipe(v.string(), v.minLength(8)),
});

const result = v.safeParse(Schema, {
  email: 'janeexample.com',
  password: '12345678',
});

console.log(result.issues?.[0].message);

expect: Method v.message actual: Ungültige E-Mail: "janeexample.com" erhalten

yurrko avatar May 11 '25 08:05 yurrko

Hey 👋 thanks for reaching out! This is expected for historical reasons, but I can see why it is unexpected in your example. Initially, Valibot did not have a config and message method. Back then, the only way to define a custom message for the entire context of the schema execution was via the third argument when calling parse or safeParse. Later, we added the config and message message to override this configuration for a subset of a schema. Since our initial implementation assumes that defining a custom error message via getSpecificMessage is more specific than via the execution context, your custom error message will be ignored (here is the code). Unfortunately, the i18n package overrides the custom error message you define via message. We should consider changing this for v2 or find a better solution to make message a higher priority.

fabian-hiller avatar May 11 '25 23:05 fabian-hiller

Hi, @yurrko. I'm Dosu, and I'm helping the Valibot team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported that setting the global language to German ('de') causes Valibot to override your custom error messages defined with v.message, showing default German messages instead.
  • I explained this is due to the current design where custom messages via v.message are considered less specific than global or i18n messages, leading to the override.
  • I acknowledged this behavior as unexpected and suggested revisiting message source priority in a future v2 update.
  • You responded positively to the explanation, but the issue remains unresolved.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of Valibot by commenting here to keep the discussion open.
  • Otherwise, this issue will be automatically closed in 30 days.

Thanks for your understanding and contribution!

dosubot[bot] avatar Aug 10 '25 16:08 dosubot[bot]