I'm trying to validate a email field on a sign up form to check if it already exist. To do this I'm checking the running a GET request on the server that returns true and false.
This is my validation Schema for the field:
validationSchema={
yup.object().shape({
registrationEmail: yup.string().email('Invalid email').test('Unique Email','Email already in use', async (value) => {axios.get('http://localhost:5000/users/register', value).catch(err => console.log(err)) })
})
}
The problem seems to be that the values field is blank. How do I past the value from the Formik Field to my function?
EDIT: Managed to send value using the following
registrationEmail: yup.string().email('Invalid email').test('Unique Email','Email already in use', function(value){return new Promise((resolve, reject) => {axios.get('http://localhost:5000/users/register', value)})})
I'm still having troubles returning a response this is what the route looks like
router.get('/register', (req, res) => {
User.findOne({email: req.body.email}).then(user => {
if(user){
return true
}else{
return false
}
})
})
I'm trying to validate a email field on a sign up form to check if it already exist. To do this I'm checking the running a GET request on the server that returns true and false.
This is my validation Schema for the field:
validationSchema={
yup.object().shape({
registrationEmail: yup.string().email('Invalid email').test('Unique Email','Email already in use', async (value) => {axios.get('http://localhost:5000/users/register', value).catch(err => console.log(err)) })
})
}
The problem seems to be that the values field is blank. How do I past the value from the Formik Field to my function?
EDIT: Managed to send value using the following
registrationEmail: yup.string().email('Invalid email').test('Unique Email','Email already in use', function(value){return new Promise((resolve, reject) => {axios.get('http://localhost:5000/users/register', value)})})
I'm still having troubles returning a response this is what the route looks like
router.get('/register', (req, res) => {
User.findOne({email: req.body.email}).then(user => {
if(user){
return true
}else{
return false
}
})
})
Share
Improve this question
edited Mar 25, 2020 at 12:57
itsOnly1_Jah
asked Mar 24, 2020 at 20:58
itsOnly1_JahitsOnly1_Jah
2411 gold badge3 silver badges11 bronze badges
2 Answers
Reset to default 7I solved my problem for the validation I did the following:
registrationEmail: yup.string().email('Invalid email')
.test('Unique Email','Email already in use',
function(value){return new Promise((resolve, reject) => {
axios.post('http://localhost:5000/users/register/validEmail', {'email': value})
.then(res => {if(res.data.msg === 'Username already been taken'){resolve(false)} resolve(true)})
})}
)
For the route:
router.post('/register/validEmail', (req, res) => {
console.log('Request: ', req.body)
User.findOne({email: req.body.email}).then(user => {
if(user){
console.log({ msg:"Username already been taken" })
return res.json({ msg:"Username already been taken" })
}
console.log({ msg: "Username available." })
return res.json({ msg: "Username available." })
}).catch((err)=>{
console.error(err);
res.status(400).json('Error: ' + err)
})
})
Turns out I had to use a POST request or the body of the request would be empty.
I noticed that you had used async/await in your question, but your answer used promises. I prefer async/await and initially thought that it wasn't supported by Yup for their test function because if your solution, but Yup does support async/await! Here's an example of my code for anyone that wants an example. There are also a few examples in the docs: https://github.com/jquense/yup#mixedtestname-string-message-string--function-test-function-schema
username: Yup.string()
.min(2, 'Too short, minimum 2 characters')
.max(50, 'Too long, maximum 50 characters')
.required('Required')
.test(
'username-backend-validation', // Name
'Username taken', // Msg
async (username) => {
// Res from backend will be flag at res.data.success, true for
// username good, false otherwise
const { data: { success } } = await axios.post(
"http://localhost:3001/api/users/register/validate-username",
{ username: username }
);
return success
}
)