zod-package-json
Version:
Zod schema for the package.json file format
112 lines (78 loc) • 3.32 kB
Markdown
[](https://github.com/velut/zod-package-json/actions?query=workflow%3ACI)
[](https://codecov.io/gh/velut/zod-package-json)
[](https://www.jsdocs.io/package/zod-package-json)

[](https://www.npmjs.com/package/zod-package-json)
[](https://github.com/velut/zod-package-json/blob/main/LICENSE)
[](https://zod.dev/) schema for the [`package.json`](https://docs.npmjs.com/cli/v11/configuring-npm/package-json) file format.
This package exports a [Zod](https://zod.dev/packages/zod) and a [Zod mini](https://zod.dev/packages/mini) schema (and the inferred TypeScript type) named `PackageJson`
that can parse most well-formed `package.json` files.
The schema includes all currently supported properties listed in the
[](https://docs.npmjs.com/cli/v11/configuring-npm/package-json)
as well as additional well-known properties specific to TypeScript and Node.js.
The schema also preserves unknown properties, which can be accessed
by indexing the parsed data with the desired string keys.
The schema only validates the known properties against their expected type
but does not do any additional normalization such as merging similar properties.
If necessary, that can be done by extending the schema with Zod methods such as
`.transform()` or `.refine()`.
- [**Explore the API on jsDocs.io**](https://www.jsdocs.io/package/zod-package-json)
- View package contents on [**unpkg**](https://unpkg.com/zod-package-json/)
- View repository on [**GitHub**](https://github.com/velut/zod-package-json)
- Read the changelog on [**GitHub**](https://github.com/velut/zod-package-json/blob/main/CHANGELOG.md)
> [!NOTE]
> Make sure you have Zod v4 installed in your application to use this package.
Using `npm`:
```
npm add zod zod-package-json
```
Using `yarn`:
```
yarn add zod zod-package-json
```
Using `pnpm`:
```
pnpm add zod zod-package-json
```
Using `bun`:
```
bun add zod zod-package-json
```
To use the classic Zod schema:
```typescript
import { PackageJson } from "zod-package-json";
// Parse data from a `package.json` file.
const packageJson = PackageJson.parse({
name: "foo",
version: "1.0.0",
unknownProp: "who knows",
});
// Access a known property.
packageJson.name; // "foo"
// Access an unknown property.
packageJson["unknownProp"]; // "who knows"
```
To use the [Zod mini](https://zod.dev/packages/mini) schema:
```typescript
import { PackageJson } from "zod-package-json/mini";
// Parse data from a `package.json` file.
const packageJson = PackageJson.parse({
name: "foo",
version: "1.0.0",
unknownProp: "who knows",
});
// Access a known property.
packageJson.name; // "foo"
// Access an unknown property.
packageJson["unknownProp"]; // "who knows"
```
```
MIT
```
Copyright (c) 2025 Edoardo Scibona
See [LICENSE](./LICENSE) file.