Joi: [question] How to allow multiple data types and be required?

Created on 15 Sep 2017  路  11Comments  路  Source: sideway/joi

I apologize if this isn't the best place for a question. From the README example:

const schema = Joi.object().keys({
    username: Joi.string().alphanum().min(3).max(30).required(),
    password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
    access_token: [Joi.string(), Joi.number()],
    birthyear: Joi.number().integer().min(1900).max(2013),
    email: Joi.string().email()
}).with('username', 'birthyear').without('password', 'access_token');

How do I continue to constrain access_token to either a string or number, but also require it?

support

Most helpful comment

@AdriVanHoudt, thanks! This works great:

const schema = Joi.object().keys({
    username: Joi.string().alphanum().min(3).max(30).required(),
    password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
    access_token: Joi.alternatives().try(Joi.string(), Joi.number()).required(),
    birthyear: Joi.number().integer().min(1900).max(2013),
    email: Joi.string().email()
}).with('username', 'birthyear').without('password', 'access_token');

All 11 comments

Joi.alternatives.try() (https://github.com/hapijs/joi/blob/master/API.md#alternativestryschemas) is probably what you want

@AdriVanHoudt, thanks! This works great:

const schema = Joi.object().keys({
    username: Joi.string().alphanum().min(3).max(30).required(),
    password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
    access_token: Joi.alternatives().try(Joi.string(), Joi.number()).required(),
    birthyear: Joi.number().integer().min(1900).max(2013),
    email: Joi.string().email()
}).with('username', 'birthyear').without('password', 'access_token');

@Nick-Riggs, no, your example is not working, I will create an issue.

Joi.alternatives().try(Joi.string(), Joi.number()).required(), is not working

its should be an array
Joi.alternatives().try([Joi.string(), Joi.number()]).required()

@sedhuait Correct, but you can't expect 2 years old issues to still be valid, the API had ample time to change since then.

@BilalDja The docs do a very bad job of pointing out HOW to achieve a conceptual goal. If you have an idea of what you want, the docs do not really help you find how to implement it.

Also, the docs do a pretty poor job of showing how to implement the things they document. I see you're trying to help with your statement but the docs are not really adequate. I have got much more of the answers I need about this library from online questions than from the docs.

Then contribute to writing better docs 馃し

Love seeing men in open source expecting women to clean up their messes. Take the criticism and don't make it someone elses responsibility.

1) I have no idea whether you're a man or a woman, and I really don't care
2) That's the whole point of open source, you're not happy about something, fix it, or at least don't act like an entitled customer for something you didn't pay a cent to use

@river-honer your tone and attitude are hostile, toxic, and unacceptable. You start by insulting the work of people who owe you nothing, and then continue with a remark that accuses of misogyny. I have zero tolerance for such behaviour. If this module doesn't do what you want, or the docs are not to your liking, don't use it. If you have constructive criticism offer it respectfully and politely - adding some gratitude usually goes a long way.

One more comment like that and you will be banned.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohamadresaaa picture mohamadresaaa  路  3Comments

Dreamystify picture Dreamystify  路  4Comments

longweiquan picture longweiquan  路  3Comments

n-sviridenko picture n-sviridenko  路  3Comments

Taxi4you picture Taxi4you  路  3Comments