openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

Question - Using generated schemas with a tool like ajv

Open maulik13 opened this issue 10 months ago • 16 comments

Hi, I am really interested in the schema generation part since it includes the shape and contraints on the fields. My first thought was to plug in AJV together with the schema objects that openapi-ts generates. Which is proving to be a challenge since the schema includes JSON references (e.g. $ref: "#/components/schemas/CategoryTypeDto") from my OpenApi specs.

Here is the generated schema JavaScript object by openapi-ts which includes JSON references.

export const $EmailTenantRegistration = {
  required: ["tenant", "user"],
  type: "object",
  properties: {
    user: {
      $ref: "#/components/schemas/UserRegistrationInfo",
    },
    tenant: {
      $ref: "#/components/schemas/TenantRegisterationInfo",
    },
  },
  additionalProperties: false,
} as const;

And here is the snippet from my open API spec under "components" -> "schemas" ->

"EmailTenantRegistration": {
  "required": [
    "tenant",
    "user"
  ],
  "type": "object",
  "properties": {
    "user": {
      "$ref": "#/components/schemas/UserRegistrationInfo"
    },
    "tenant": {
      "$ref": "#/components/schemas/TenantRegisterationInfo"
    }
  },
  "additionalProperties": false
},

Should openapi-ts resolve these references when creating JavaScript objects? AJV cannot resolve these references from the schemas.gen.ts file.

maulik13 avatar Apr 21 '24 09:04 maulik13