Skip to content

.example SHOULD perform validation on the provided value #1478

@blacksun1

Description

@blacksun1

Context

  • node version: *
  • joi version: 13.1.2
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone

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

Describe your issue here, include schemas and inputs you are validating if needed.

const schema = Joi.string().example(123);

Which result you had ?

Constant schema is set to a valid schema object.

What did you expect ?

Error to be thrown

Description

I've been using the .example method everywhere on pretty much every schema that I've ever created as it gives us the flexibility of producing documentation but also use it as a cheap and easy way of unit testing.

Now, as per #1189, it was decided that this would just be taken out. It wasn't even thought useful to document the change (the documentation still says that it should throw) let alone marked as a breaking change.

Can this be put back the way it was and maybe some more thought could be put into the issue such as allowing the example to take a second argument of an option object that could take:

  • A ref property. What is the point of having an example without ref data anyway when it requires a ref?
  • A validate property that would validate the example or not and make it true by default.

By the way, I absolutely love the Joi project and I've recommending it to developers ever since I found it. This particular piece of functionality has been one of the main selling points I've used when pushing it onto people.

Thanks in advance,

Metadata

Metadata

Assignees

Labels

breaking changesChange that can breaking existing codefeatureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions