open-api icon indicating copy to clipboard operation
open-api copied to clipboard

[openapi-types] How extend SchemaObject

Open TheLox95 opened this issue 4 years ago • 1 comments

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?

TheLox95 avatar Nov 15 '21 16:11 TheLox95

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! ✌️

rkoval avatar Apr 13 '22 00:04 rkoval