1.2.15 • Published 1 year ago

@ibabkin/openapi-to-zod v1.2.15

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year 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 year ago

1.2.14

1 year ago

1.2.12

2 years ago

1.2.13

2 years ago

1.2.10

2 years ago

1.2.11

2 years ago

1.2.9

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago