Skip to content

Instantly share code, notes, and snippets.

@pavsidhu
Last active August 10, 2020 12:04
Show Gist options
  • Save pavsidhu/9934838fac0cd68ef5d46b848a41760e to your computer and use it in GitHub Desktop.
Save pavsidhu/9934838fac0cd68ef5d46b848a41760e to your computer and use it in GitHub Desktop.
Validate.js with React Native wrapper
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@example.com'}
// 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
}
@fauzzi
Copy link

fauzzi commented Jul 1, 2017

Hi, this code have error on field and unknow validatejs function.

@harpalsinh-jadeja
Copy link

#Actually, it is not an error, but it is only structure of the code. using that you have to make your own wrapper
I try to make that as under...

import validation from 'validate.js'

export default function validate(fieldName, value) {
    var constraints = {
	email: {
	    presence: true,
	    format: {
	        pattern: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
	        message: 'Invalid email id',
	    }
	},
	password: {
	    presence: true,
	    length: {
	        minimum: 4,
	        message: 'Invalid Password',
	    }
	},
	confirmPassword: {
	    presence: true,
	    equality: 'password'
	},
	phoneNo: {
	    presence: true,
	    format: {
	        pattern: "^[0-9]{10}$",
	        message: 'Invalid phone number',
	    },
	},
    };

    var formValues = {}
    formValues[fieldName] = value

    var formFields = {}
    formFields[fieldName] = constraints[fieldName]


    const result = validation(formValues, formFields)

    if (result) {
	return result[fieldName][0]
    }
    return null
}

@dotCLL
Copy link

dotCLL commented Apr 3, 2018

There is actually a slight typo with the original post as well.

Change import validation from 'validation.js' to import validation from 'validate.js'

@johntips
Copy link

👍

@Big-Silver
Copy link

Big-Silver commented May 21, 2018

I got the following error: Cannot set property 'tests' of undefined
Here is my code.

import validation from 'validation.js'

export default function validate(fieldName, value) {
var constraints = {
	email: {
		presence: {
			message: "Email is required"
		},
		email: {
			message: "Doesn't look like a valid email"
		}
	}
};

var formValues = {};
formValues[fieldName] = value;

var formFields = {};
formFields[fieldName] = constraints[fieldName];

const result = validation(formValues, formFields);

if (result) {
	return result[fieldName][0];
}

return null;
}

@ramprasathrk
Copy link

ramprasathrk commented Jul 15, 2018

For those who face the issue as @Big-Silver mentioned:
There are two similar npm packages available: validation.js and validate.js, this similarity by name becomes a confusion.
This approach works only with validate.js, so make sure to install the correct one: npm install --save validate.js

@vkingmaker
Copy link

#Actually, it is not an error, but it is only structure of the code. using that you have to make your own wrapper
I try to make that as under...

import validation from 'validate.js'

export default function validate(fieldName, value) {
    var constraints = {
	email: {
	    presence: true,
	    format: {
	        pattern: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
	        message: 'Invalid email id',
	    }
	},
	password: {
	    presence: true,
	    length: {
	        minimum: 4,
	        message: 'Invalid Password',
	    }
	},
	confirmPassword: {
	    presence: true,
	    equality: 'password'
	},
	phoneNo: {
	    presence: true,
	    format: {
	        pattern: "^[0-9]{10}$",
	        message: 'Invalid phone number',
	    },
	},
    };

    var formValues = {}
    formValues[fieldName] = value

    var formFields = {}
    formFields[fieldName] = constraints[fieldName]


    const result = validation(formValues, formFields)

    if (result) {
	return result[fieldName][0]
    }
    return null
}

For some strange reasons the confirmPassword field always returns a validation error, please how can I solve this blocker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment