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

feat: access of input value on template string for error messages #420

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

agSant01
Copy link

@agSant01 agSant01 commented Apr 1, 2021

Feat Error Template Message

Overview

The goal of this PR is to allow access of the input value on the error messages template strings via a :inputValue keyword.

Use Case

A use case where user is making upload of an excel file with 100+ rows, and the validation of those fields throw an validation error just saying to the user that an error occurred is not very helpful.

It would be better to return the actual values(s) that are invalid so the user can find the values and perform the fixes.

const validation = new Validator(
    data,
    {
        array: 'required|array',
        'array.*.postalCode': 'required|postal_code',
        // rest of validation schema...
    },
    {
        'postal_code.array.*.postalCode': "The postal code ':inputValue' is not valid.",
    }
);

// output -> "The postal code '0O987' is not valid."  | Second character is a letter 'O'

Proposed implementation

Modify the Messages._replacePlaceholders(rule, template, data) to add the rule.inputValue to the data object. By doing this it will not collide with the existing implementation of :value templater for the custom replacements.

Object as input value

The default string representation of an {} is [object Object]. In order to make it more readable the behavior of the replacement will be to replace it for [Object] only.

PS. It was also considered the option {...}. Open to feedback on this.

Updated README.md

Updated the README to reflect the newly added feature.

@agSant01 agSant01 changed the title Feat Error Template Message feat: access input value on template string for error messages Jun 9, 2021
@agSant01 agSant01 changed the title feat: access input value on template string for error messages feat: access of input value on template string for error messages Jun 9, 2021
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

Successfully merging this pull request may close these issues.

1 participant