openapistack / openapi-backend

Build, Validate, Route, Authenticate and Mock using OpenAPI
https://openapistack.co
MIT License
614 stars 83 forks source link

unknown format "date-time" ignored in schema #718

Closed robogeek closed 8 months ago

robogeek commented 9 months ago

The specification I'm working with uses ISO 8601 date-time strings, of course. The declaration for the corresponding schema object is:

    dateTime:
      type: string
      format: date-time
      description: datetime in ISO 8601 format
      example: 2023-06-15T09:30:00Z

When executing the test suite, a zillion error messages like this are printed:

unknown format "date-time" ignored in schema at path "#/properties/createdDateTime"

The messages go away when commenting the format: date-time specification.

anttiviljami commented 8 months ago

To properly validate formats like date-time, you should extend ajv with ajv-formats.

import addFormats from 'ajv-formats';

const api = new OpenAPIBackend({
  definition,
  customizeAjv: (ajv) => {
    addFormats(ajv, { mode: 'fast', formats: ['date-time'] });

    return ajv;
  },
});

See: https://openapistack.co/docs/openapi-backend/request-validation/#extended-formats

robogeek commented 8 months ago

As I said in the other issue - what you're suggesting is not what I am wanting. It shouldn't throw an error because a valid/legitimate format has been declared.

anttiviljami commented 8 months ago

I see. The warnings are generated by Ajv, and are indeed useful for openapi-backend users as an indicator of needing to add validators. Perhaps see if ajv has an option to turn these warnings off?

robogeek commented 8 months ago

If I enable some AJV code, does it force me into using AJV validation? I don't understand the impact, and am using Joi for validation instead.

anttiviljami commented 8 months ago

If you have no need for validation, you can just pass validate: false in the constructor so Ajv never gets initialised, effectively disabling these warnings.