{"id":13485126,"url":"https://github.com/nwtgck/ts-json-validator","last_synced_at":"2025-05-01T05:37:48.361Z","repository":{"id":48452733,"uuid":"186382925","full_name":"nwtgck/ts-json-validator","owner":"nwtgck","description":"JSON Validator for TypeScript - Safer JSON.parse() validating by TypeScript types","archived":false,"fork":false,"pushed_at":"2021-07-25T16:44:24.000Z","size":601,"stargazers_count":26,"open_issues_count":9,"forks_count":1,"subscribers_count":3,"default_branch":"develop","last_synced_at":"2025-05-01T05:37:42.569Z","etag":null,"topics":["json","scheme","structure","type","type-safe","typescript","validator"],"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/nwtgck.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":"2019-05-13T08:57:41.000Z","updated_at":"2023-06-20T08:07:02.000Z","dependencies_parsed_at":"2022-09-08T03:11:43.887Z","dependency_job_id":null,"html_url":"https://github.com/nwtgck/ts-json-validator","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nwtgck%2Fts-json-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nwtgck%2Fts-json-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nwtgck%2Fts-json-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nwtgck%2Fts-json-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nwtgck","download_url":"https://codeload.github.com/nwtgck/ts-json-validator/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251830466,"owners_count":21650803,"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":["json","scheme","structure","type","type-safe","typescript","validator"],"created_at":"2024-07-31T17:01:46.903Z","updated_at":"2025-05-01T05:37:48.325Z","avatar_url":"https://github.com/nwtgck.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# ts-json-validator\n[![CircleCI](https://circleci.com/gh/nwtgck/ts-json-validator.svg?style=shield\u0026circle-token=f91f04558f6a90694804aa0ba6347c3de3dd25d7)](https://circleci.com/gh/nwtgck/ts-json-validator)\n\nSafer `JSON.parse()` validating by TypeScript types\n\nWrite a format of JSON once, Derive the type automatically at compile-time.\n\n## Installation\n\n```bash\nnpm install -S git+https://github.com/nwtgck/ts-json-validator#v0.1.2\n```\n\n## Basic Usage\n\n```ts\nimport {nul, bool, num, str, literal, opt, arr, tuple, obj, union, TsType, validatingParse} from 'ts-json-validator';\n\n// Create a format\nconst personFormat = obj({\n  name: str,\n  age: num\n});\n\n// Generate Person type\n// IMPORTANT: Type is derived at compile-time. Just a write format once!\ntype Person = TsType\u003ctypeof personFormat\u003e;\n\n\n// Safer parse than JSON.parse()\nconst p1: Person | undefined = validatingParse(personFormat, '{\"name\": \"jack\", \"age\": 8}');\n```\n\n\n```ts\nconst myObj: {[key: string]: any} = {name: \"jack\", age: 8};\nconst p2: Person | undefined = validate(personFormat, myObj);\n// =\u003e not undefined\n```\n\n```ts\nconst myObj2: any = {name: \"jack\", age: \"this is a text\"};\nisValid(personFormat.runtimeType, myObj2);\n// =\u003e false (because type of age should be number)\n```\n\n## Core feature\n\nThe main feature is that **`type Person` is automatically derived**. So you just define the format/structure object of JSON such as `personFormat`. Then you can get a `type` for free.\n\n## Usage of Array, nested objects, Literal Types, Union Types and Tuples\n\n\n```ts\nimport {nul, bool, num, str, literal, opt, arr, tuple, obj, union, TsType, validatingParse} from 'ts-json-validator';\n\n// Create a format\nconst myObj1Format = obj({\n  // String array - string[]\n  myStrs: arr(str),\n  // Nested object - (myFlag: boolean)\n  myObj: obj({\n    myFlag: bool\n  }),\n  // Union Type - string | number | bool\n  strOrNumOrBool: union(str, num, bool),\n  // Object array - {prop1: number, prop2: string}[]\n  myObjs: arr(obj({\n    prop1: num,\n    prop2: str\n  })),\n  // Optional property（myOptional?: number）\n  myOptional: opt(num),\n  // Literal Type - (myLiteral: 'MY_LITERAL')\n  myLiteral: literal('MY_LITERAL' as const),\n  // Literal Union Type - ('red' | 'blue')\n  myLitUnion: union(literal('red' as const), literal('blue' as const)),\n  // Nullable - (string | null)\n  myNullable: union(str, nul),\n  // Tuple - [string, boolean, number]\n  myTuple: tuple(str, bool, num),\n});\n\n\n// Generate MyObj1 type\n// IMPORTANT: Type is derived at compile-time. Just write a format once!\ntype MyObj1 = TsType\u003ctypeof myObj1Format\u003e;\n```\n\nIn Union Type, `T1 | T2 | ... | T64` is supported. In tuple, `[T1, T2, ..., T64]` is supported.\n\n## Type-safety\n\nYou can find errors at compile-time, not runtime!\n\n### Wrong type in the array\n`myStrs` should be an string array, but `128` is included.  \n![](doc_assets/wrong-type-in-array.png)\n\n### Missing property\n`myStrs` is required, but missing.  \n![](doc_assets/missing-property.png)\n\n### Wrong Literal Type\n`myLiteral` should be `'MY_LITERAL'` type, but found `'YOUR LITERAL'`  \n![](doc_assets/wrong-literal-type.png)\n\n### Unknown property\nProperty `somethingElse` is not defined in `myObj1Format`.  \n![](doc_assets/unknown-property.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnwtgck%2Fts-json-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnwtgck%2Fts-json-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnwtgck%2Fts-json-validator/lists"}