Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@anatine/zod-openapi
Advanced tools
@anatine/zod-openapi is an npm package that bridges the gap between Zod, a TypeScript-first schema declaration and validation library, and OpenAPI, a standard for defining RESTful APIs. This package allows developers to generate OpenAPI documentation from Zod schemas, making it easier to maintain API documentation in sync with the actual code.
Generate OpenAPI Schema from Zod Schema
This feature allows you to generate an OpenAPI schema from a Zod schema. The code sample demonstrates how to define a Zod schema for a User object and then convert it into an OpenAPI schema.
const { z } = require('zod');
const { openApi } = require('@anatine/zod-openapi');
const UserSchema = z.object({
id: z.string(),
name: z.string(),
email: z.string().email()
});
const openApiSchema = openApi({
title: 'User API',
version: '1.0.0',
schemas: {
User: UserSchema
}
});
console.log(JSON.stringify(openApiSchema, null, 2));
Add Metadata to Zod Schemas
This feature allows you to add OpenAPI metadata directly to Zod schemas. The code sample shows how to extend Zod with OpenAPI capabilities and add descriptions to the fields in a User schema.
const { z } = require('zod');
const { extendZodWithOpenApi } = require('@anatine/zod-openapi');
extendZodWithOpenApi(z);
const UserSchema = z.object({
id: z.string().openapi({ description: 'User ID' }),
name: z.string().openapi({ description: 'User Name' }),
email: z.string().email().openapi({ description: 'User Email' })
});
console.log(UserSchema.openapi);
Generate OpenAPI Documentation
This feature allows you to generate a full OpenAPI documentation from Zod schemas. The code sample demonstrates how to define a Zod schema, convert it to an OpenAPI schema, and then generate the full OpenAPI documentation.
const { z } = require('zod');
const { openApi, generateOpenApiDocument } = require('@anatine/zod-openapi');
const UserSchema = z.object({
id: z.string(),
name: z.string(),
email: z.string().email()
});
const openApiSchema = openApi({
title: 'User API',
version: '1.0.0',
schemas: {
User: UserSchema
}
});
const openApiDocument = generateOpenApiDocument(openApiSchema);
console.log(JSON.stringify(openApiDocument, null, 2));
zod-to-openapi is another package that converts Zod schemas to OpenAPI documentation. It offers similar functionality to @anatine/zod-openapi but may have different API design and additional features. It is a good alternative if you are looking for different options in the same space.
ts-json-schema-generator generates JSON schema from TypeScript types. While it does not directly generate OpenAPI documentation, JSON schema can be used as a base for OpenAPI schemas. This package is useful if you are working with TypeScript and need to generate schemas for validation or documentation purposes.
openapi-typescript generates TypeScript types from OpenAPI schemas. It works in the opposite direction compared to @anatine/zod-openapi, but it can be useful in a workflow where you start with OpenAPI documentation and want to generate TypeScript types for use in your application.
Converts a Zod schema to an OpenAPI SchemaObject
as defined by openapi3-ts
Both openapi3-ts and zod are peer dependencies instead of dependant packages.
While zod
is necessary for operation, openapi3-ts
is for type-casting.
npm install openapi3-ts zod @anatine/zod-openapi
import { generateSchema } from '@anatine/zod-openapi';
const aZodSchema = z.object({
uid: z.string().nonempty(),
firstName: z.string().min(2),
lastName: z.string().optional(),
email: z.string().email(),
phoneNumber: z.string().min(10).optional(),
})
const myOpenApiSchema = generateSchema(aZodSchema);
// ...
This will generate an OpenAPI schema for myOpenApiSchema
{
"type": "object",
"properties": {
"uid": {
"type": "string",
"minLength": 1
},
"firstName": {
"type": "string",
"minLength": 2
},
"lastName": {
"type": "string"
},
"email": {
"type": "string",
"format": "email"
},
"phoneNumber": {
"type": "string",
"minLength": 10
}
},
"required": [
"uid",
"firstName",
"email"
]
}
import { extendApi, generateSchema } from '@anatine/zod-openapi';
const aZodExtendedSchema = extendApi(
z.object({
uid: extendApi(z.string().nonempty(), {
title: 'Unique ID',
description: 'A UUID generated by the server',
}),
firstName: z.string().min(2),
lastName: z.string().optional(),
email: z.string().email(),
phoneNumber: extendApi(z.string().min(10), {
description: 'US Phone numbers only',
example: '555-555-5555',
}),
}),
{
title: 'User',
description: 'A user schema',
}
);
const myOpenApiSchema = generateSchema(aZodExtendedSchema);
// ...
... or via extension of the Zod schema:
import { extendApi, generateSchema, extendZodWithOpenApi } from '@anatine/zod-openapi';
import {z} from 'zod';
extendZodWithOpenApi(z);
const aZodExtendedSchema =
z.object({
uid: z.string().nonempty().openapi({
title: 'Unique ID',
description: 'A UUID generated by the server',
}),
firstName: z.string().min(2),
lastName: z.string().optional(),
email: z.string().email(),
phoneNumber: z.string().min(10).openapi({
description: 'US Phone numbers only',
example: '555-555-5555',
}),
}).openapi(
{
title: 'User',
description: 'A user schema',
}
);
const myOpenApiSchema = generateSchema(aZodExtendedSchema);
// ...
This will generate an extended schema:
{
"type": "object",
"properties": {
"uid": {
"type": "string",
"minLength": 1,
"title": "Unique ID",
"description": "A UUID generated by the server"
},
"firstName": {
"type": "string",
"minLength": 2
},
"lastName": {
"type": "string"
},
"email": {
"type": "string",
"format": "email"
},
"phoneNumber": {
"type": "string",
"minLength": 10,
"description": "US Phone numbers only",
"example": "555-555-5555"
}
},
"required": [
"uid",
"firstName",
"email",
"phoneNumber"
],
"title": "User",
"description": "A user schema"
}
A great lib that provided some insights on dealing with various zod types.
Lib providing insights into using Zod with NestJS
This library is part of a nx monorepo @anatine/zod-plugins.
FAQs
Zod to OpenAPI converter
The npm package @anatine/zod-openapi receives a total of 125,255 weekly downloads. As such, @anatine/zod-openapi popularity was classified as popular.
We found that @anatine/zod-openapi demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.