{"name":"@peculiar/json-schema","version":"2.0.0","keywords":["json","stringify","serialize","parse","convert","decorator"],"author":{"name":"PeculiarVentures, Inc"},"license":"MIT","_id":"@peculiar/json-schema@2.0.0","maintainers":[{"name":"rmhrisk","email":"ryan_hurst@hotmail.com"},{"name":"microshine","email":"microshine@mail.ru"},{"name":"yury.strozhevsky","email":"yury@strozhevsky.com"},{"name":"peculiarventures","email":"info@peculiarventures.com"},{"name":"apilguk","email":"apilguk@gmail.com"},{"name":"donskov","email":"donskovdmitriyv@gmail.com"},{"name":"worldthirteen","email":"mihailzachepilo@gmail.com"}],"contributors":[{"name":"Ryan Hurst","email":"rmh@unmitigatedrisk.com"},{"name":"Miroshin Stepan","email":"microshine@mail.ru"}],"homepage":"https://github.com/PeculiarVentures/json-schema#readme","bugs":{"url":"https://github.com/PeculiarVentures/json-schema/issues"},"dist":{"shasum":"f31005a011b05d71388491982194597a6a68ee40","tarball":"http://registry-npm.ywwl.com/@peculiar/json-schema/download/@peculiar/json-schema-2.0.0.tgz","fileCount":7,"integrity":"sha512-Cw/e7hP0kHRGLOgtIMftm6ie1YgXO30uqZFK+bU6hTMi4QKC20S9trntL/E+/8z/d3fAUNpWRf5v+gW3JtKzqA==","signatures":[{"sig":"MEQCIHn5wZawvZkSQj3cjpBfKLhHcUj3UBh0Jp/d1HiVtCKrAiBOIE0mlXGebQHP5q+VG1SZCHOhGi0CLJi2IHMcqebHxA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"sig":"MEQCICxTuw+dW6J9sj6mccBhnSR4HK6KoE25jYdk12IEgXcNAiA/4f+lBT94Pvg8rWPCF9AcnvlJNMHXm4bkT/zeJPeRAA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@peculiar%2fjson-schema@2.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":46293,"size":8132,"noattachment":false,"key":"/@peculiar/json-schema/-/@peculiar/json-schema-2.0.0.tgz"},"main":"./build/index.js","types":"./build/index.d.ts","module":"./build/index.mjs","engines":{"node":">=16.0.0"},"exports":{".":{"import":"./build/index.mjs","require":"./build/index.js"},"./package.json":"./package.json"},"gitHead":"d1bbbcfe0a0791fcc816c33ed899947849e4ee8a","scripts":{"lint":"oxlint","test":"vitest run","build":"tsdown","format":"oxfmt","prepack":"npm run build","coverage":"vitest run --coverage","lint:fix":"oxlint --fix","typecheck":"tsc --noEmit","format:check":"oxfmt --check"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"a27c6648-2cb5-47c5-8055-533ac06ef0dd"}},"repository":{"url":"git+https://github.com/PeculiarVentures/json-schema.git","type":"git"},"_npmVersion":"11.19.0","description":"Serialize, parse and validate JSON with TypeScript decorators","directories":{},"sideEffects":false,"_nodeVersion":"24.21.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"oxfmt":"^0.72.0","oxlint":"^1.87.0","tsdown":"^0.23.0","vitest":"^4.1.11","typescript":"^6.0.3","@types/node":"^22.20.5","@vitest/coverage-v8":"^4.1.11","@arethetypeswrong/core":"^0.18.5","conventional-changelog":"^8.1.3","conventional-changelog-angular":"^9.4.0"},"_npmOperationalInternal":{"tmp":"tmp/json-schema_2.0.0_1791552780342_0.8741229713409322","host":"s3://npm-registry-packages-npm-production"},"readme":"<h1 align=\"center\">\n  @peculiar/json-schema\n</h1>\n\n<div align=\"center\">\n\n![NPM License](https://img.shields.io/npm/l/@peculiar/json-schema)\n![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/PeculiarVentures/json-schema/publish.yml?branch=master&label=publish)\n[![npm version](https://img.shields.io/npm/v/@peculiar/json-schema.svg)](https://www.npmjs.com/package/@peculiar/json-schema)\n![Coveralls](https://img.shields.io/coverallsCoverage/github/PeculiarVentures/json-schema)\n[![npm downloads](https://img.shields.io/npm/dm/@peculiar/json-schema.svg)](https://www.npmjs.com/package/@peculiar/json-schema)\n\n</div>\n\nSerialize and parse JSON with TypeScript classes. Describe the schema once with the `@JsonProp` decorator, and the library maps property names, converts values and validates input in both directions.\n\n## Introduction\n\nJSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, but in practice it is a [minefield](https://seriot.ch/projects/parsing_json.html) when machines need to parse it.\n\nSchemas help, but they can be complicated to use. `@peculiar/json-schema` addresses this with decorators: you annotate a class, and the library handles serialization, parsing and validation of JSON for you.\n\nAll input data is untrusted, so it should be validated before use. Parsing into a schema gives you a typed object or a descriptive error.\n\n## Installation\n\n```sh\nnpm install @peculiar/json-schema\n```\n\nThe package is ESM-only with TypeScript declarations, and requires Node.js `^20.19.0 || >=22.12.0`. CommonJS code can still `require()` it on those versions.\n\n`@JsonProp` is a TypeScript legacy (experimental) decorator. Enable it in your `tsconfig.json`:\n\n```json\n{\n  \"compilerOptions\": {\n    \"experimentalDecorators\": true\n  }\n}\n```\n\n## Breaking Changes In v3\n\nv3 ships ESM only. The CommonJS build is removed: v2 published `build/index.js` (CommonJS), `build/index.mjs` (ESM) and `build/index.d.mts`; v3 publishes ES modules only, with `build/index.js` and `build/index.d.ts` as the entry point behind an `exports` map. The other files under `build/` mirror `src/` and are internal.\n\n- **Node.js `^20.19.0 || >=22.12.0` is required** (previously `>=16.0.0`). These versions support `require()` of ES modules, so CommonJS code can keep using `require(\"@peculiar/json-schema\")` without changes. Node.js 22.0–22.11 is not supported: it can't `require()` ESM.\n- **TypeScript projects that compile to CommonJS** need `\"module\": \"node20\"` or `\"nodenext\"` (TypeScript 5.9+). With `\"module\": \"node16\"` the compiler reports TS1479.\n- **ESM and bundler setups** (`\"module\": \"preserve\"`, `\"moduleResolution\": \"bundler\"`) work without changes.\n- **Legacy `\"moduleResolution\": \"node\"` (node10)** resolves the package through `types`; `@peculiar/json-schema` has no subpath exports, so nothing else changes.\n- **Jest in CommonJS mode** can't load ESM packages. Use Jest's ESM mode, add `@peculiar/json-schema` to the `transformIgnorePatterns` exceptions, or switch to Vitest.\n- **Deep imports stay blocked.** The `exports` map allows only `@peculiar/json-schema` and `@peculiar/json-schema/package.json`, as in v2.\n- **Internal types are no longer exported.** The bundled v2 declarations accidentally exposed `IJsonSchema`, `IJsonSchemaItem`, `IJsonNamedSchema` and `JsonTransform` as types. v3 exports the same 18 names as v1 and `src/index.ts`; type imports of those four no longer compile.\n- **`require()` and `import` share one schema storage.** v2 shipped separate CommonJS and ESM copies, so an application that loaded both (for example ESM code plus a CommonJS dependency) got two schema storages, and classes decorated through one copy were unknown to `JsonParser`/`JsonSerializer` from the other. v3 has a single module, so both load the same storage.\n\nApart from those four internal types, the public API is unchanged; only packaging and runtime requirements changed. `@JsonProp` still needs `\"experimentalDecorators\": true`.\n\n## Examples\n\n### Creating a schema\n\n```ts\nimport { JsonParser, JsonSerializer, JsonProp, JsonPropTypes, type IJsonConverter } from \"@peculiar/json-schema\";\n\n// Custom data converter\nconst JsonBase64UrlConverter: IJsonConverter<Uint8Array, string> = {\n  fromJSON: (value: string) => base64UrlToBuffer(value),\n  toJSON: (value: Uint8Array) => bufferToBase64Url(value),\n};\n\nclass EcPublicKey {\n  @JsonProp({ name: \"kty\" })\n  keyType = \"EC\";\n\n  @JsonProp({ name: \"crv\" })\n  namedCurve = \"\";\n\n  @JsonProp({ converter: JsonBase64UrlConverter })\n  x = new Uint8Array(0);\n\n  @JsonProp({ converter: JsonBase64UrlConverter })\n  y = new Uint8Array(0);\n\n  @JsonProp({ name: \"ext\", type: JsonPropTypes.Boolean, optional: true })\n  extractable = false;\n\n  @JsonProp({ name: \"key_ops\", type: JsonPropTypes.String, repeated: true, optional: true })\n  usages: string[] = [];\n}\n\nconst json = `{\n  \"kty\": \"EC\",\n  \"crv\": \"P-256\",\n  \"x\": \"zCQ5BPHPCLZYgdpo1n-x_90P2Ij52d53YVwTh3ZdiMo\",\n  \"y\": \"pDfQTUx0-OiZc5ZuKMcA7v2Q7ZPKsQwzB58bft0JTko\",\n  \"ext\": true\n}`;\n\nconst ecPubKey = JsonParser.parse(json, { targetSchema: EcPublicKey });\nconsole.log(ecPubKey);\n\necPubKey.usages.push(\"verify\");\n\nconst jsonText = JsonSerializer.serialize(ecPubKey, undefined, undefined, 2);\nconsole.log(jsonText);\n\n// Output\n//\n// EcPublicKey {keyType: \"EC\", namedCurve: \"P-256\", x: Uint8Array(32), y: Uint8Array(32), extractable: true, …}\n//\n// {\n//   \"kty\": \"EC\",\n//   \"crv\": \"P-256\",\n//   \"x\": \"zCQ5BPHPCLZYgdpo1n-x_90P2Ij52d53YVwTh3ZdiMo\",\n//   \"y\": \"pDfQTUx0-OiZc5ZuKMcA7v2Q7ZPKsQwzB58bft0JTko\",\n//   \"ext\": true,\n//   \"key_ops\": [\n//     \"verify\"\n//   ]\n// }\n```\n\n`base64UrlToBuffer` and `bufferToBase64Url` stand for any base64url helpers, such as `Buffer.from(value, \"base64url\")` in Node.js.\n\n### Extending a schema\n\n```ts\nimport { JsonParser, JsonSerializer, JsonProp } from \"@peculiar/json-schema\";\n\nclass BaseObject {\n  @JsonProp({ name: \"i\" })\n  public id = 0;\n}\n\nclass Word extends BaseObject {\n  @JsonProp({ name: \"t\" })\n  public text = \"\";\n}\n\nclass Person extends BaseObject {\n  @JsonProp({ name: \"n\" })\n  public name = \"\";\n\n  @JsonProp({ name: \"w\", repeated: true, type: Word })\n  public words: Word[] = [];\n}\n\nconst json = `{\n  \"i\": 1,\n  \"n\": \"Bob\",\n  \"w\": [\n    { \"i\": 2, \"t\": \"hello\" },\n    { \"i\": 3, \"t\": \"world\" }\n  ]\n}`;\n\nconst person = JsonParser.parse(json, { targetSchema: Person });\nconsole.log(person);\n\nconst word = new Word();\nword.id = 4;\nword.text = \"!!!\";\nperson.words.push(word);\n\nconst jsonText = JsonSerializer.serialize(person, undefined, undefined, 2);\nconsole.log(jsonText);\n\n// Output\n//\n// Person {id: 1, name: \"Bob\", words: [Word {id: 2, text: \"hello\"}, Word {id: 3, text: \"world\"}]}\n//\n// {\n//   \"i\": 1,\n//   \"n\": \"Bob\",\n//   \"w\": [\n//     {\n//       \"i\": 2,\n//       \"t\": \"hello\"\n//     },\n//     {\n//       \"i\": 3,\n//       \"t\": \"world\"\n//     },\n//     {\n//       \"i\": 4,\n//       \"t\": \"!!!\"\n//     }\n//   ]\n// }\n```\n\n## API\n\nSee the type declarations in [`src/index.ts`](src/index.ts) (published as `build/index.d.ts`).\n\n## Contributing\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, commit conventions and the release flow.\n","publish_time":1791552780441,"_cnpm_publish_time":1791552780441,"dist-tags":{"latest":"3.0.0"}}