Skip to content

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

@dbrockman

Description

@dbrockman

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"
    }
  ]
}

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions