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

Enable to pass "now" in date.min and date.max #449

Closed
wants to merge 1 commit into from
Closed

Enable to pass "now" in date.min and date.max #449

wants to merge 1 commit into from

Conversation

aymericbeaumet
Copy link
Contributor

It is not currently possible to validate a date relatively to the
current time. For example, the following schema will always validate
relatively to the time of the schema object allocation:

var schema = {
  date: Joi.date().min(new Date())
};

This patch addresses this issue by adding a new 'now' placeholder.
It dynamically computes the current time on-the-fly at each comparison.
Allowing to always compare relatively to the current time.

var schema = {
  // Ensure the date field cannot represent a past date
  date: Joi.date().min('now')
};

It is not currently possible to validate a date relatively to the
current time. For example, the following schema will always validate
relatively to the time of the `schema` object allocation:

```javascript
var schema = {
  date: Joi.date().min(new Date())
};
```

This patch addresses this issue by adding a new `'now'` placeholder.
It dynamically computes the current time on-the-fly at each comparison.
Allowing to always compare relatively to the current time.

```javascript
var schema = {
  // Ensure the date field cannot represent a past date
  date: Joi.date().min('now')
};
```
@rlidwka
Copy link

rlidwka commented Oct 3, 2014

hmm... how about adding "a hour ago" as well?

I mean, it should really be a function imho.

@aymericbeaumet
Copy link
Contributor Author

I agree it could be a function. I wanted to keep it simple. The thing is I remember an issue where Eran didn't wanted to accept custom functions (cannot find it).

Do you know any JS library to transform relative date (a year ago, in 2 minutes) in Date objects?

@hueniverse hueniverse added the feature New functionality or improvement label Oct 10, 2014
@Marsup Marsup self-assigned this Nov 5, 2014
@Marsup Marsup added this to the 4.8.0 milestone Nov 5, 2014
@Marsup
Copy link
Collaborator

Marsup commented Nov 5, 2014

I've merged this as is for the time being but I agree with @rlidwka, we need to come up with a better solution.
We could always include chrono which does this very well but how many "special cases" will Joi have to handle ?

Just to lay the state of things from what I gathered, Joi needs to be able to :

  • validate object
  • generate documentation
  • stay friendly with joi validation generators like enjoi which is pure json

I'm thinking about ways to achieve this but if you already have an idea feel free to open an issue @rlidwka :)

@Marsup Marsup closed this Nov 5, 2014
@rlidwka
Copy link

rlidwka commented Nov 5, 2014

I'm thinking about ways to achieve this but if you already have an idea feel free to open an issue @rlidwka :)

I already have an idea. There is no other way, really.

@Marsup
Copy link
Collaborator

Marsup commented Nov 5, 2014

That answers to only one of those 3 conditions.

@rlidwka
Copy link

rlidwka commented Nov 5, 2014

That answers to only one of those 3 conditions.

It answers to all three. Custom function can validate stuff, and it can be serialized. Documentation might require another custom function to generate it, but there is nothing unsolvable there.

@Marsup
Copy link
Collaborator

Marsup commented Nov 5, 2014

Never said it was, just saying it's more tricky than "just support functions and be done with it".

@Marsup Marsup mentioned this pull request Dec 27, 2014
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants