Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
json-schema-to-ts
Advanced tools
The json-schema-to-ts npm package is a utility that converts JSON Schema definitions into TypeScript types. This allows developers to ensure type safety and consistency between their JSON data and TypeScript code.
Convert JSON Schema to TypeScript Types
This feature allows you to convert a JSON Schema into a TypeScript type. The `FromSchema` utility type takes a JSON Schema object and infers the corresponding TypeScript type.
import { FromSchema } from 'json-schema-to-ts';
const userSchema = {
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
age: { type: 'number' }
},
required: ['id', 'name']
} as const;
type User = FromSchema<typeof userSchema>;
const user: User = {
id: '123',
name: 'John Doe',
age: 30
};
Type Validation
This feature ensures that the TypeScript types derived from JSON Schema are validated at compile time, reducing runtime errors and improving code reliability.
import { FromSchema } from 'json-schema-to-ts';
const productSchema = {
type: 'object',
properties: {
productId: { type: 'string' },
price: { type: 'number' }
},
required: ['productId', 'price']
} as const;
type Product = FromSchema<typeof productSchema>;
const product: Product = {
productId: 'abc123',
price: 19.99
};
The typescript-json-schema package generates JSON Schema from your TypeScript types. It works in the opposite direction compared to json-schema-to-ts, which converts JSON Schema to TypeScript types. This package is useful if you start with TypeScript types and need to generate JSON Schema for validation or documentation purposes.
Ajv is a JSON Schema validator that can also generate TypeScript types from JSON Schema using additional plugins. While its primary focus is on validation, it offers similar functionality to json-schema-to-ts when combined with the appropriate plugins.
Zod is a TypeScript-first schema declaration and validation library. It allows you to define schemas in TypeScript and infer types from them. While it doesn't convert JSON Schema to TypeScript types directly, it provides a similar type-safe experience for defining and validating data structures.
Infer types from your JSON schemas
FAQs
Infer typescript types from your JSON schemas!
We found that json-schema-to-ts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.