ansman/validate.js

Placeholder for original property name in message?

Opened this issue · 0 comments

jlem commented

The default validation messages attempt to turn the property name into something human readable, but this is not good if you're trying to provide specific information about a config property or something else that should tell the developer which property failed the validation.

I was wondering if there was a placeholder to get the original property name.

For example, if have this definition right now:

        const rules = {
            apiBaseUrl: {
                url: { 
                     message: `^- apiBaseUrl: Expected a URL, given %{value} instead.
                },
            },
            clientBaseUrl: {
                url: { 
                     message: `^- clientBaseUrl: Expected a URL, given %{value} instead.
                },
            }
        };

I should be able to generalize this message and assign it to the URL validator generically with a message like this:

"^%{property}: Expected a URL, given %{value} instead."

Where %{property} is the exact name of the property being validated, rather than a human readable version.

Is this possible in the current version, if not then I'd like to make this a feature request.