Skip to content

Dynamic validation error messages #473

@fracz

Description

@fracz

I'm submitting a feature request

Currently, it is impossible to create a dynamic validation error message. For example, I want to validate IBAN account number. I configure the rule as follows:

ValidationRules
    .ensure('account').displayName("Account number")
    .satisfies(account => getActualChecksum(account) == calculateChecksum(account))
    .withMessage("Given IBAN number is invalid");

The validation message could be much more informative, for example Given IBAN number has cheksum 34 but it should be 56.

What about allowing to pass a callback that will generate a validation message based on given value? This should solve all of the cases where the validation message should be built based on the given value or something else dynamic.

ValidationRules
    .ensure('account').displayName("Account number")
    .satisfies(account => getActualChecksum(account) == calculateChecksum(account))
    .withMessage(account => "Given IBAN has checksum " + getActualChecksum(account) + " but it should be " +calculateChecksum(account));

OR it should be possible to define special parameters for message in addition to existing $displayName and $value, e.g.

ValidationRules
    .ensure('account').displayName("Account number")
    .satisfies(account => getActualChecksum(account) == calculateChecksum(account))
    .withMessage("Given IBAN has checksum ${actualChecksum} but it should be ${correctChecksum}")
    .withMessageParamsBuilder(account => {actualChecksum: getActualChecksum(account), correctChecksum: calculateChecksum(account)});

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions