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

Pass in objects as an error message #321

Open
toms opened this issue Apr 17, 2023 · 1 comment
Open

Pass in objects as an error message #321

toms opened this issue Apr 17, 2023 · 1 comment

Comments

@toms
Copy link

toms commented Apr 17, 2023

I think it'd be cool to pass in an object as an error message that can be parsed and processed later in whatever validation function you have setup.

For example, in my use case I'd like to notify a client with a message on invalid input, but only for specific fields. Otherwise, I'd like to send a default error message. Now this can be done by manually filling each message for any potential errors, while also including the default message

someNumField: {
    type: 'number',
    integer: true,
    min: 1,
    max: 10000,
    messages: {
        number: 'Please contact support',
        integer: 'Please contact support',
        numberMin: 'Your number amount must be at least 1',
        numberMax: 'Your number must be below 10,000',
        required: 'Please contact support',
    },
},

and always just send the .message property, but I think this is unnecessarily repetitive, especially for fields with a lot of validators.

Ideally, I'd like to only specify messages for fields that I'd like to customize:

someNumField: {
    type: 'number',
    integer: true,
    min: 1,
    max: 1000000,
    messages: {
        numberMin: { isCustom: true, 'Your number amount must be at least 1' },
        numberMax: { isCustom: true, 'Your number must be below 10,000' },
    },
},

and then assign a default output message in my validator utility function:

const errorMessage = error?.message?.isCustom || 'Please contact support';

However, all objects get parsed to strings ([Object object]).
Attempting to JSON.stringify an object just leads to errors due to unescaped quotations:

undefined:8
errors.push({ type: "number", message: "{"message":"test message"}", field: field, actual: origValue, label: label });
                                          ^^^^^^^

SyntaxError: Unexpected identifier
    at new Function (<anonymous>)

A different possible solution for this scenario would be to implement a default global error message field, but I think adding in object support for messages adds some additional flexibility for other potential use cases.

@icebob
Copy link
Owner

icebob commented Apr 23, 2023

I fear I don't understand your issue. You don't need to define all messages in all fields, just what you want to override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants