-
Notifications
You must be signed in to change notification settings - Fork 2
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
Comments
There is actually a workaround to this bug which another undocumented feature where you can specify your entire own 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 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
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:
... you're mutating
finalMessage
andmessage
is a function, firstfinalMessage
is set to the return value ofmessage
and then it is set again to be the function itself.The text was updated successfully, but these errors were encountered: