Skip to content
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

if oneOf #1170

Closed
automaticdreams opened this issue Aug 25, 2022 · 1 comment · Fixed by #1000
Closed

if oneOf #1170

automaticdreams opened this issue Aug 25, 2022 · 1 comment · Fixed by #1000

Comments

@automaticdreams
Copy link

I trying to implement a check on three fields, and can't work out how to do this

if all three fields are empty, pass
if any one field has a value, validate the other two fields
ie
{startDate: '2022-03-27', endDate: '', description: ''} should have two validation errors, one for description and one for endDate
{startDate: '2022-03-27', endDate: '2022-03-27', description: ''} should have one validation error, for description
{startDate: '2022-03-27', endDate: '', description: 'some text'} should have one validation error, for endDate
{startDate: '', endDate: '2022-03-27', description: ''} should have two validation errors, one for description and one for startDate
{startDate: '', endDate: '', description: 'some text'} should have two validation errors, one for startDate and one for endDate
{startDate: '', endDate: '', description: ''} should NOT have any validation errors

I've tried the following
check('startDate').if(check('endDate').isDate() || check('description').notEmpty()).isDate().withMessage('Enter a valid start date')
check('endDate').if(check('startDate').isDate() || check('description').notEmpty()).isDate().withMessage('Enter a valid end date')
check('description').if(check('startDate').isDate() || check('endDate'). isDate()). notEmpty().withMessage('Enter a description')

but that only validates the first bit of the if (so for example, it doesn't check startDate if only description has a value)
I don't want to check them separately, as the project requires that there is only one error message presented for each field

I then tried using oneOf like so
check('startDate').if(oneOf([
check('endDate').isDate(),
check('description').notEmpty(),
])).isDate().withMessage('Enter a valid start date')

but that is showing a syntax error, which I presume is the combination of using if with oneOf - I can't find any documentation showing how to use oneOf inside an if

gustavohenke added a commit that referenced this issue Feb 18, 2023
@gustavohenke gustavohenke added this to the v7.0.0 milestone Feb 18, 2023
@gustavohenke gustavohenke linked a pull request Feb 18, 2023 that will close this issue
gustavohenke added a commit that referenced this issue Mar 11, 2023
gustavohenke added a commit that referenced this issue Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants