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

Date "now" refers to date when schema was compiled #480

Closed
dbrockman opened this issue Nov 8, 2014 · 1 comment
Closed

Date "now" refers to date when schema was compiled #480

dbrockman opened this issue Nov 8, 2014 · 1 comment
Assignees
Labels
bug Bug or defect
Milestone

Comments

@dbrockman
Copy link

Observed in 4.8.0

The "now" keyword passed to date.min and date.max creates a new Date when the schema is compiled.

It would make more sense if the date was created when validating the schema.

In this example the validation fails because test_date is created one second after the schema.

var Joi = require('joi');
var schema = Joi.date().max('now');
var test_date;

setTimeout(function() {
  test_date = new Date();
}, 1000);

setTimeout(function() {
  var result = Joi.validate(test_date, schema);
  // result.error -> "validate: value must be less than or equal to ..."
  console.log('Validation error:', result.error);
}, 2000);

A solution could be to keep "now" in the _tests part of the schema and convert to date when validating:

{
  "isJoi": true,
  "_type": "date",
  "_tests": [
    {
      "name": "max",
      "arg": "now"
    }
  ]
}
@Marsup Marsup added the bug Bug or defect label Nov 9, 2014
@Marsup Marsup self-assigned this Nov 9, 2014
@Marsup Marsup added this to the 4.8.1 milestone Nov 9, 2014
@Marsup Marsup closed this as completed in e8b8f4f Nov 9, 2014
@Marsup
Copy link
Collaborator

Marsup commented Nov 9, 2014

Sorry for not noticing. It's shipped as 4.8.1.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants