Skip to content

Instantly share code, notes, and snippets.

@danielb2
Last active December 26, 2015 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielb2/64d8974e44d39882e891 to your computer and use it in GitHub Desktop.
Save danielb2/64d8974e44d39882e891 to your computer and use it in GitHub Desktop.

alternatives

Generates a type that will match one of the provided alternative schemas via the try() method. If no schemas are added, the type will not match any value except for undefined. Schemas can be expressed as multiple arguments or given as an array.

Supports the same methods of the any() type.

var alt = Joi.alternatives().try(Joi.number(), Joi.string());
// Same as var alt = Joi.alternatives.try([Joi.number(), Joi.string()])

Alternatives can be expressed using a shorter notation.

var alt = Joi.alternatives(Joi.number(), Joi.string())
// Same as var alt = Joi.alternatives.try([Joi.number(), Joi.string()])
@danielb2
Copy link
Author

alternatives

Generates a type that will match one of the provided alternative schemas via the try()
method. If no schemas are added, the type will not match any value except for undefined.

Supports the same methods of the any() type.

alternatives.try(schema, schema2, ...| [schema, schema2, ...])

Try accepts schemas using single or multiple arguments, or as an array.

var alt = Joi.alternatives().try(Joi.number(), Joi.string());
alt.validate('a', function (err, value) { });

Using .try() is optional and allows for a shorter version:

var alt = Joi.alternatives(Joi.number(), Joi.string())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment