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

Undocumented message function has a bug #20

Open
ianchanning opened this issue Jul 19, 2022 · 1 comment
Open

Undocumented message function has a bug #20

ianchanning opened this issue Jul 19, 2022 · 1 comment

Comments

@ianchanning
Copy link

ianchanning commented Jul 19, 2022

I wanted to be able to use values in the error message - and although not documented in the README it is supported.

However there is a bug because in createFormValidation.js#L43:

            let finalMessage = defaultMessage
            if (typeof message === 'function') {
                finalMessage = message(value, values, options)
            }
            if (typeof message !== 'undefined') {
                finalMessage = message
            }

... you're mutating finalMessage and message is a function, first finalMessage is set to the return value of message and then it is set again to be the function itself.

@ianchanning
Copy link
Author

ianchanning commented Jul 19, 2022

There is actually a workaround to this bug which another undocumented feature where you can specify your entire own createFieldValidation function.

You could maybe document this too 🙂

/**
 * Generate the field validation function
 *
 * @param {function} validate boolean function that checks the form value
 * @param {function|string} message Either function that gives an error message based on the value or else error message
 * @param {string} defaultMessage if message is undefined
 * @returns {function} (string, array, object) => [{ path, message }] The field validation function itself
 */
const createFieldValidation =
    (validate, errorMessage, defaultMessage = 'Invalid') =>
    (path, values, options) => {
        const value = values[path]
        const formatMessage = () => {
            if (typeof errorMessage === 'function') {
                return errorMessage(value, values, options)
            }
            return typeof errorMessage === 'undefined'
                ? defaultMessage
                : errorMessage
        }
        if (typeof value === 'undefined' || validate(value, values, options)) {
            return []
        }
        return [{ path, message: formatMessage() }]
    }

You can then specify this as the validation function in the formRule object, instead of validate and message.

const validation = createFormValidation([
    {
        path: 'code',
        validate: isRequired,
        message: <Trans>Please enter a code</Trans>,
    },
    {
        path: 'code',
        validation: createFieldValidation(
            preformat(getLength(invalidNumbers), isEqualTo(0)),
            // message format
            (code) =>
                `${t`Invalid numbers`}: '${invalidNumbers(code)
                    .split('')
                    .join(',')}'`
        ),
    },
])

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

1 participant