1.2.15 • Published 1 month ago

@ibabkin/openapi-to-zod v1.2.15

Weekly downloads
-
License
ISC
Repository
github
Last release
1 month ago

NPM version:latest npm downloads Coverage Status License

Description

Generates validators for operation payloads from OpenAPI 3.0 specification using Zod.

Usage

  • supported version of OpenAPI: 3.0.0
  • supports yaml-import
openapi-to-zod --input ./swagger.yaml --output ./validators.ts
import { renderDocument } from '@ibabkin/openapi-to-zod';
fs.writeFileSync('.generated/output.ts', renderDocument(api as OpenAPIV3.Document));

Input example:

openapi: 3.0.0
info:
  title: Simple Inventory API
  description: This is a simple API
  contact:
    email: you@your-company.com
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.0
servers:
  - url: https://virtserver.swaggerhub.com/DOSAKANTIVENKATESH/smp/1.0.0
    description: SwaggerHub API Auto Mocking
tags:
  - name: admins
    description: Secured Admin-only calls
  - name: developers
    description: Operations available to regular developers
paths:
  !!import/merge
    - paths.yaml

components:
  schemas:
    !!import/merge
      - components.yaml

Output example:

import { z } from "zod";

const zNumber = z.string().regex(/^\d+$/).transform(Number);

const zDate = z.preprocess((arg) => {
  if (typeof arg === "string" || typeof arg === "number") {
    return new Date(arg);
  }

  return arg;
}, z.date());

export const PAYLOADS = {
  searchInventory: z.object({
    query: z.object({
      searchString: z.string().optional(),
      skip: zNumber,
      limit: zNumber
    })
  }),
  addInventory: z.object({
    body: z.object({
      id: z.string(),
      name: z.string(),
      releaseDate: zDate
    })
  })
};
1.2.15

1 month ago

1.2.14

1 month ago

1.2.12

9 months ago

1.2.13

8 months ago

1.2.10

10 months ago

1.2.11

10 months ago

1.2.9

11 months ago

1.2.8

11 months ago

1.2.7

11 months ago

1.2.6

11 months ago

1.2.5

11 months ago

1.2.4

11 months ago

1.2.3

11 months ago

1.2.2

11 months ago

1.2.1

11 months ago

1.2.0

11 months ago

1.1.8

11 months ago

1.1.7

11 months ago

1.1.6

11 months ago

1.1.5

11 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago