openapi-backend
openapi-backend copied to clipboard
Incorrect formatters being used for Ajv in Validation
I am getting errors on run from ajv that is being loaded in openapi-backend
.
unknown format "date-time" ignored in schema at path "#/properties/timestamp"
unknown format "date-time" ignored in schema at path "#/properties/timestamp"
The fix is this snippet that should go into the validation file.
const AJV = require('ajv').default;
const addFormats = require('ajv-formats').default;
const ajv = new AJV();
addFormats(ajv);
If you just want to add date-time support you can use:
const api = new OpenAPIBackend({
definition: '...........',
customizeAjv: (ajv, ajvOpts, validationContext) => {
let dtFormat = {
type: 'string',
validate: /^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i,
};
ajv.addFormat("date-time", dtFormat);
return ajv;
}
});
The regex above was copied from the "ajv-formats" module, here: https://github.com/ajv-validator/ajv-formats/blob/8b424f1e11d23f556cc12f1b9fd16a37286cf326/src/formats.ts
Hi @ak683517,
That snippet returned an error with
index.ts:45:32 - error TS2345: Argument of type '{ type: string; validate: RegExp; }' is not assignable to parameter of type 'Format'.
Property 'async' is missing in type '{ type: string; validate: RegExp; }' but required in type 'AsyncFormatDefinition<number>'.
45 ajv.addFormat("date-time", dtFormat);
~~~~~~~~
node_modules/ajv/dist/types/index.d.ts:165:5
165 async: true;
~~~~~
'async' is declared here.
Found 1 error.
I'd cast it as any
:
ajv.addFormat("date-time", dtFormat as any);
Any updates when this is going to be fixed?
Would also like to get an update -> so we should add the formatter ourselves if needed?
The following worked for me:
import { fullFormats } from 'ajv-formats/dist/formats'
const api = new OpenAPIBackend({
definition: '...........',
ajvOpts: {
formats: fullFormats,
},
})
@anttiviljami Any updates when this is going to be fixed?