kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
892 stars 235 forks source link

[openapi-types] How extend SchemaObject #775

Open TheLox95 opened 2 years ago

TheLox95 commented 2 years ago

I would like to add an extra property to the OpenAPIV3.SchemaObject interface like

const mySchema: OpenAPIV3.SchemaObject = {
  properties: {
    name: { type: 'string', transform: ['trim'] },
  },
};

Where transform is a custom keyword defined in the ajv-keywords library. How can we do this?

rkoval commented 2 years ago

hey there, just passing through. seems like i had a similar issue whereby ajv couldn't compile/validate OpenAPI schemas when using ajv-keywords with transform. if this was the same issue as yours, i was able to work around it by importing the underlying .json for the OpenAPI JSON schema directly, modifying that schema, and then manually passing it into an ajv instance instead of the validator in openapi-schema-validator. here's an example:

import Ajv from 'ajv';
import openApiJsonSchema from 'openapi-schema-validator/dist/resources/openapi-3.0.json';

openApiJsonSchema.definitions.schema.properties.transform = {
  type: 'array',
  items: {type: 'string'},
};

const ajv = new Ajv({
  removeAdditional: true,
  useDefaults: true,
  allErrors: true,
  coerceTypes: true,
  strict: true,
  strictRequired: false,
});

const validate = ajv.compile(openApiJsonSchema);
// ... do validation ...

hope that helps! ✌️