question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to validate field only when it exists?  

See original GitHub issue

I would like to know how do I validate a field only when it exists.

My problem is that I have a phone field, but this field is dynamic and may or may not exist in the DOM, and I would like to validate it in my schema when it exists, but the problem is that when I put phone: Yup.string ().Required(), these fields are stuck in the validation even if they do not exist in the DOM.

Any suggestions to escape this validation?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

9reactions
kevinjanczak-devcommented, Nov 18, 2020
const schema = yup.object({
    phone: yup.string().when('$exist', {
        is: exist => exist,
        then: yup.string().required(),
        otherwise: yup.string()
    })
})

schema.validate({  }, {
    context: { exist: false }
}).catch(err => console.log(err))
2reactions
jrmarqueshdcommented, Sep 5, 2022

UPDATE:

In the version 0.32.11, the solution of @kevinjanczak-dev is not working.

And a found a solution to make validaiton only when field value exists, i’m using Cyclic Dependency, and this is my code:

const schema = yup.object().shape(
	{
		phone: yup.string().when('phone', {
			is: (exists) => !!exists,
			then: yup.string().test('validate_phone', 'phone is invalid', (phone) => phone.length === 14),
			otherwise: yup.string(),
		}),
	},
	[['phone', 'phone']],
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Yup: How to validate field only when it exists? - Stack Overflow
Change it to true when you modify the value in step 1. And then if the value is true for that key then...
Read more >
Validation rule "sometimes": apply rules only if field exists
Recently I've found out an interesting validation rule for form fields in Laravel, and I want to briefly tell you about it.
Read more >
Restrict data input by using validation rules - Microsoft Support
1. Field Validation Rule You can use a field validation rule to specify a criterion that all valid field values must meet. You...
Read more >
Only run exists validation if input is not empty array - Laracasts
just add sometimes rule on this like this . It checks against a field only if that field is present in the input...
Read more >
[Solved]-Yup: How to validate field only when it exists?-Reactjs
[Solved]-Yup: How to validate field only when it exists?-Reactjs ... You can set a additional boolean key where value is default false. Change...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found