You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: