0.0.3 • Published 2 years ago

openapi-superstruct v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

OpenAPI Superstruct

npm

Node.js library that generates Superstruct definitions based on an OpenAPI spec.

Note: this project is under active development.

First, an example

Given this sample spec, openapi-superstruct will produce this file:

/* eslint-disable */
/* autogenerated by openapi-superstruct */

import * as s from 'superstruct';

const struct_Pet = s.object({
  id: s.integer(),
  name: s.string(),
  tag: s.optional(s.string())
});
const struct_Pets = s.array(s.lazy(() => struct_Pet));
const struct_Error = s.object({
  code: s.integer(),
  message: s.string()
});

export const structs = {
  Pet: struct_Pet,
  Pets: struct_Pets,
  Error: struct_Error
};

export type Pet = s.Infer<typeof structs['Pet']>;
export type Pets = s.Infer<typeof structs['Pets']>;
export type Error = s.Infer<typeof structs['Error']>;

Why?

Other projects, like the excellent openapi-typescript-codegen, already largely bridge the gap between OpenAPI and Typescript. This project wants to take one step further by introducing the ability to perform runtime type validation validation on all the API models defined by the OpenAPI spec. Performing runtime type validation could be useful to verify that the server conforms to the OpenAPI spec's definitions, whether you own the server or not. Superstruct makes it easy for us to have runtime type validation side-by-side with proper Typescript types.

Acknowledgements

Special thanks to @ferdikoomen for his project openapi-typescript-codegen, which was both a conceptual and practical inspiration for this project.