Last active
August 10, 2020 12:04
-
-
Save pavsidhu/9934838fac0cd68ef5d46b848a41760e to your computer and use it in GitHub Desktop.
Validate.js with React Native wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import validation from 'validation.js' | |
export default function validate(fieldName, value) { | |
// Validate.js validates your values as an object | |
// e.g. var form = {email: '[email protected]'} | |
// Line 8-9 creates an object based on the field name and field value | |
var formValues = {} | |
formValues[fieldName] = value | |
// Line 13-14 creates an temporary form with the validation fields | |
// e.g. var formFields = { | |
// email: { | |
// presence: { | |
// message: 'Email is blank' | |
// } | |
// } | |
var formFields = {} | |
formFields[fieldName] = validation[field] | |
// The formValues and validated against the formFields | |
// the variable result hold the error messages of the field | |
const result = validatejs(formValues, formFields) | |
// If there is an error message, return it! | |
if (result) { | |
// Return only the field error message if there are multiple | |
return result[field][0] | |
} | |
return null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some strange reasons the
confirmPassword
field always returns a validation error, please how can I solve this blocker