josh
josh

Reputation: 87

How to allow empty or a string validated by a regex in JOI?

I am to pass a vat value. The validation should allow saving it in the db if it is empty or passes the given regex of a certain country.

.when('country_code', {
                is: 'AT',
                then: Joi.alternatives([
                    Joi.string().allow(''),
                    Joi.string().regex(/(AT)?U[0-9]{8}/)
                ])
            })

This what I have achieved till now but it isn't working.

Upvotes: 1

Views: 875

Answers (1)

Herb
Herb

Reputation: 325

This is what you are looking for: Joi.string().regex(/(AT)?U[0-9]{8}$/).allow('')

Upvotes: 2

Related Questions