{"id":18601565,"url":"https://github.com/peculiarventures/json-schema","last_synced_at":"2026-03-09T07:31:46.347Z","repository":{"id":33069213,"uuid":"150883791","full_name":"PeculiarVentures/json-schema","owner":"PeculiarVentures","description":"This package uses ES2015 decorators to simplify JSON schema creation and use","archived":false,"fork":false,"pushed_at":"2022-12-30T19:02:15.000Z","size":235,"stargazers_count":2,"open_issues_count":6,"forks_count":4,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-25T11:20:26.195Z","etag":null,"topics":["decorators","json","parsing","schema"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PeculiarVentures.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-29T16:42:37.000Z","updated_at":"2020-07-22T09:00:54.000Z","dependencies_parsed_at":"2023-01-14T23:30:42.177Z","dependency_job_id":null,"html_url":"https://github.com/PeculiarVentures/json-schema","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeculiarVentures%2Fjson-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeculiarVentures%2Fjson-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeculiarVentures%2Fjson-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeculiarVentures%2Fjson-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PeculiarVentures","download_url":"https://codeload.github.com/PeculiarVentures/json-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248281395,"owners_count":21077423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decorators","json","parsing","schema"],"created_at":"2024-11-07T02:08:44.317Z","updated_at":"2026-03-09T07:31:46.287Z","avatar_url":"https://github.com/PeculiarVentures.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON-SCHEMA\n\n[![License](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://raw.githubusercontent.com/PeculiarVentures/json-schema/master/LICENSE.md)\n[![CircleCI](https://circleci.com/gh/PeculiarVentures/json-schema.svg?style=svg)](https://circleci.com/gh/PeculiarVentures/json-schema)\n[![Coverage Status](https://coveralls.io/repos/github/PeculiarVentures/json-schema/badge.svg?branch=master\u0026t=ddJivl)](https://coveralls.io/github/PeculiarVentures/json-schema?branch=master)\n[![npm version](https://badge.fury.io/js/%40peculiar%2Fjson-schema.svg)](https://badge.fury.io/js/%40peculiar%2Fjson-schema)\n\n[![NPM](https://nodei.co/npm/@peculiar/json-schema.png)](https://nodei.co/npm/@peculiar/json-schema/)\n\nThis package uses ES2015 [decorators](https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841) to simplify JSON [schema creation and use](https://json-schema.org/understanding-json-schema/index.html). \n\n\n## Introduction\n\nJSON (JavaScript Object Notation) is a lightweight data-interchange format that was designed to be easy for humans to read and write but in practice, it is [minefield](http://seriot.ch/parsing_json.html) when it machines need to parse it.\n\nWhile the use of schemas can help with this problem their use can be complicated. When using `json-schema` this is addressed by using decorators to make both serialization and parsing of XML possible via a simple class that handles the schemas for you.  \n\nThis is important because validating input data before its use is important to do because all input data is evil. Using a schema helps you handle this data [safely](https://www.whitehatsec.com/blog/handling-untrusted-json-safely/). \n\n\n## Installation\n\nInstallation is handled via  `npm`:\n\n```\n$ npm install @peculiar/json-schema\n```\n\n## Examples\n### Node.js\n\nCreating a schema:\n```js\nimport { JsonParser, JsonSerializer, JsonProp, JsonPropTypes, IJsonConverter } from \"@peculiar/json-schema\";\n\n// custom data converter\nconst JsonBase64UrlConverter: IJsonConverter\u003cUint8Array, string\u003e = {\n  fromJSON: (value: string) =\u003e base64UrlToBuffer(value),\n  toJSON: (value: Uint8Array) =\u003e 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\nExtending a Schema:\n```js\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 = 0;\n  @JsonProp({ name: \"w\", repeated: true, type: Word })\n  public words = [];\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 = \"!!!\";\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//   \"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\nUse [index.d.ts](index.d.ts) file\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeculiarventures%2Fjson-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeculiarventures%2Fjson-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeculiarventures%2Fjson-schema/lists"}