Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Ability to add custom error message for Validator #849

Closed
iTaybb opened this issue Jan 3, 2023 · 3 comments
Closed

[RFC] Ability to add custom error message for Validator #849

iTaybb opened this issue Jan 3, 2023 · 3 comments
Labels
Docs Not a Bug Not a Problem, expected behavior RFC

Comments

@iTaybb
Copy link

iTaybb commented Jan 3, 2023

Is your feature request related to a problem? Please describe.
Currently, when using a Validator with a custom condition, the error message is not indicative enough.

For example:
Validator("ipify_api_list", condition=lambda x: len(x) > 5)
would result in:
dynaconf.validator.ValidationError: ipify_api_list invalid for <lambda>(['api.ipify.com']) in env main

Describe the solution you'd like
Add a new, "error_msg" option for Validator that will be shown as the error message

Additional context
I'll be glad to make a PR for this myself.

@iTaybb iTaybb added Not a Bug Not a Problem, expected behavior RFC labels Jan 3, 2023
@rochacbruno
Copy link
Member

The Validator class takes an messages attribute

    custom_msg = "You cannot set {name} to {value} in env {env}"
    settings.validators.register(
        Validator("VERSION", ne=1, messages={"operations": custom_msg}),
    )

I think it is missing in the docs

default_messages = MappingProxyType(
{
"must_exist_true": "{name} is required in env {env}",
"must_exist_false": "{name} cannot exists in env {env}",
"condition": "{name} invalid for {function}({value}) in env {env}",
"operations": (
"{name} must {operation} {op_value} "
"but it is {value} in env {env}"
),
"combined": "combined validators failed {errors}",
}
)

@iTaybb
Copy link
Author

iTaybb commented Jan 4, 2023

This is true (however the key of the messages should be condition and not operations.
So this issue is about documenting it better :)

@rochacbruno
Copy link
Member

added to the docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Not a Bug Not a Problem, expected behavior RFC
Projects
None yet
Development

No branches or pull requests

2 participants