joiful-ts / joiful

TypeScript Declarative Validation for Joi
239 stars 18 forks source link

Multiple types on field #216

Closed ruankotovich closed 3 years ago

ruankotovich commented 3 years ago

In the raw Joi lib it is possible to map a field to multiple types using the following approach:

const alt = Joi.alternatives().try(Joi.number(), Joi.string());

In there a way to do this using joiful?

laurence-myers commented 3 years ago

I think you can use Joiful's custom() method. Something like this might work?

@jf.custom((Joi) => Joi.alternatives().try(Joi.number(), Joi.string()))
myProperty: number | string;

I notice we don't have tests or documentation about the custom() method, that's something we should add.

ruankotovich commented 3 years ago

I think you can use Joiful's custom() method. Something like this might work?

@jf.custom((Joi) => Joi.alternatives().try(Joi.number(), Joi.string()))
myProperty: number | string;

I notice we don't have tests or documentation about the custom() method, that's something we should add.

It worked like a charm, thanks a lot!

Regarding the tests, I will try to do something about the custom() following the guidelines

laurence-myers commented 3 years ago

I'll close this issue since @jf.custom() worked.