Skip to content

Support for combine schema: allOf, oneOf, anyOf #2051

@aleung

Description

@aleung

Describe the problem you are trying to fix (provide as much context as possible)

OpenAPI spec provides several keywords which you can use to combine schemas. You can use these keywords to create a complex schema, or validate a value against multiple criteria.

  • oneOf – validates the value against exactly one of the subschemas
  • allOf – validates the value against all the subschemas
  • anyOf – validates the value against any (one or more) of the subschemas

When I implement an REST API from the OpenAPI v3 definition in node.js, Joi doesn't provide corresponding API to validate the combined schema. Our team is trying to make server code generated from API definition, which uses Joi to perform input parameters validation.

Which API (or modification of the current API) do you suggest to solve that problem ?

We suggest the API to be used like this:

Joi.object().keys({
  name: Joi.string(),
  kind: Joi.oneOf([
    Joi.object().keys({
      x: Joi.number(),
      y: Joi.boolean()
    }),
    Joi.object().keys({
      a: Joi.string(),
      b: Joi.number()
    }),
  ])
});

Are you ready to work on a pull request if your suggestion is accepted ?

Our team mate @ufonion has make Joi extensions to support allOf and oneOf. Would like to see if it's possible to implement into Joi API.

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions