Skip to content

Example for Array Schema Not Possible #828

@jasonmcaffee

Description

@jasonmcaffee

Context

It is not possible to create an example for an array schema, as the value passed into the example function is concatenated with existing examples.

  • node version: 4.0.0
  • joi version: 7.2.3
  • environment (node, browser): no
  • used with (hapi, standalone, ...): standalone
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

Trying to provide examples for a joi.array, so that schema.describe().examples will provide an array of arrays.
Describe your issue here, include schemas and inputs you are validating if needed.

const schema = Joi.array()
   .example(['a', 'b'])
   .example(['c', 'd'])

let examples = schema.describe().examples;
assert(examples.length == 2); //fails

Which result you had ?

examples.length is 1

What did you expect ?

examples.length is 2

Offending line of code:

https://github.com/hapijs/joi/blob/master/lib/any.js#L447

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug or defect

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions