tlivings / enjoi

Converts a JSON schema to a Joi schema.
Apache License 2.0
282 stars 57 forks source link

handle cases where `type` keyword is undefined or `type` is an array #105

Open nlundquist opened 3 years ago

nlundquist commented 3 years ago

JSONSchemas without the type keyword are still valid, eg:

{ format: 'date-time' }

is a valid JSONSchema that matches against any non-string value, or any string value that is date-time formatted.

Additionally, type can be an array of types. In this way a single JSONSchema can do complex validations of multiple types of value without using combination keywords (allOf, etc.). As an example:

{
  type: ['string', 'integer']
  minLength: 5
  maximum: 10
}

The above would successfully match a string with length 5 or greater, or a integer less than 10.

Since these are valid JSONSchema cases they should ideally be supported by Enjoi, but it would require notable changes to how JSONSchemas are translated to Joi. Application of validation keywords is currently tied to the presence of an expected type value.

For cases where there is no explicit type Joi conditional validations would need to be used, something like:

Joi.any().when({ is: Joi.string(), then: Joi.date(), otherwise: Joi.any() })
nlundquist commented 3 years ago

this is basically the same issue as #57 and #72