simonplend / express-json-validator-middleware

Express middleware for validating requests against JSON schema

Home Page:https://npm.im/express-json-validator-middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Date-time validation?

katerlouis opened this issue · comments

I'm new to JSON schema validation, but according to this website there is a date-time type. But I get an error when I use this in my schema declaration.

module.exports = {
  type: 'object',

  properties: {
    startedAt: {
      type: 'date-time',
    },

  // ...
};

error:
Error: schema is invalid: data.properties['startedAt'].type should be equal to one of the allowed values, data.properties['startedAt'].type should be array, data.properties['startedAt'].type should match some schema in anyOf

Is there a documentation saying which types are supported?

date-time is a format, and the format keyword can only be used with properties which have a type of string. I think your example should be:

module.exports = {
  type: 'object',

  properties: {
    startedAt: {
      type: 'string',
      format: 'date-time',
    },

  // ...
};

Note that this library currently depends on v6 of Ajv which has support for formats (including date-time) built in, so the schema above should "just work". I'm hoping to upgrade this library to use v8 of Ajv in the near future, which requires you to install the ajv-formats plugin in order to use formats in your schemas.

I hope that helps!