Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.example SHOULD perform validation on the provided value #1478

Closed
blacksun1 opened this issue Apr 18, 2018 · 3 comments
Closed

.example SHOULD perform validation on the provided value #1478

blacksun1 opened this issue Apr 18, 2018 · 3 comments
Assignees
Labels
breaking changes Change that can breaking existing code feature New functionality or improvement
Milestone

Comments

@blacksun1
Copy link

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,

@huttli
Copy link

huttli commented Apr 26, 2018

Sounds like a perfect global validation option flag, say validateExample?

@blacksun1
Copy link
Author

Where would a "global validation option flag" go? As the example should be executed at the time that the schema is created rather than when a schema is used to validate an object.

@Marsup
Copy link
Collaborator

Marsup commented Jun 29, 2018

Agreed that the breaking change is somewhat debatable, it didn't break anyone's project though. Using it for unit test is rather dirty though 🤔

Supporting a validation of examples that mimics a true in-place validation is challenging. I didn't want to invest time and effort into supporting a feature that created problems and that I deem nice to have. Now if you really want this, feel free to try!

@Marsup Marsup added this to the 14.0.0 milestone Aug 9, 2018
@Marsup Marsup self-assigned this Aug 9, 2018
@Marsup Marsup added the breaking changes Change that can breaking existing code label Aug 9, 2018
@Marsup Marsup closed this as completed in 0a73518 Oct 14, 2018
@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking changes Change that can breaking existing code feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

5 participants