{"id":15284235,"url":"https://github.com/vlashchanka/json-lint-d-ts","last_synced_at":"2025-04-12T23:21:19.383Z","repository":{"id":57284968,"uuid":"290607199","full_name":"vlashchanka/json-lint-d-ts","owner":"vlashchanka","description":"Lint JSON files with Typescript","archived":false,"fork":false,"pushed_at":"2020-10-24T08:12:25.000Z","size":263,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T17:21:21.653Z","etag":null,"topics":["json","linting","typescript","typescript-generator"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vlashchanka.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["vlashchanka"]}},"created_at":"2020-08-26T21:20:22.000Z","updated_at":"2022-07-23T20:31:16.000Z","dependencies_parsed_at":"2022-09-17T03:01:21.245Z","dependency_job_id":null,"html_url":"https://github.com/vlashchanka/json-lint-d-ts","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/vlashchanka%2Fjson-lint-d-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlashchanka%2Fjson-lint-d-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlashchanka%2Fjson-lint-d-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlashchanka%2Fjson-lint-d-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vlashchanka","download_url":"https://codeload.github.com/vlashchanka/json-lint-d-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248644179,"owners_count":21138563,"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","linting","typescript","typescript-generator"],"created_at":"2024-09-30T14:51:54.892Z","updated_at":"2025-04-12T23:21:19.364Z","avatar_url":"https://github.com/vlashchanka.png","language":"TypeScript","funding_links":["https://github.com/sponsors/vlashchanka"],"categories":[],"sub_categories":[],"readme":"json-lint-d-ts\n------\n[![npm](https://img.shields.io/npm/v/json-lint-d-ts.svg?maxAge=2592000)](https://www.npmjs.com/package/json-lint-d-ts)\n![Node.js CI](https://github.com/vlashchanka/json-lint-d-ts/workflows/Node.js%20CI/badge.svg)\n[![Dependency Status][1]][2]\n[![Dev Dependency Status][3]][4]\n\n\n![image](assets/icon.svg)\n------\n\nWrite type safe json files in your project with Typescript.\n\n## Installation\n`yarn -D json-lint-d-ts`\n\n## Usage\n\n### Validation\n\nImport `validate` function and pass all your JSON files paths with their type declarations.\n\n```typescript\nimport { validate } from \"json-lint-d-ts\";\n\nconst result = validate([\n    [\"./hello.json\", \"./hello.d.ts\"],\n]);\n\n/*\n[\n  {\n    jsonPath: './hello.json',\n    jsonErrors: [\n      `hello.json (13,3): Type '\"World\"' is not assignable to type '\"world\"'.`\n    ]\n  }\n]\n*/\nconsole.log(result);\n```\n\nTypescript declaration example:\n\n⚠️ it is important to have `type Root` in your `d.ts` file ⚠️\n\n```typescript\ninterface HelloLowerCase {\n    hello: \"world\";\n}\ninterface HelloUpperCase {\n    HELLO: \"WORLD\";\n}\n\ntype HelloType = HelloLowerCase | HelloUpperCase;\n\ntype Root = HelloType\n\n```\n\nThe `result` of the validation:\n\n```shell script\n[\n  {\n    jsonPath: './hello.json',\n    jsonErrors: [\n      `hello.json (13,3): Type '\"World\"' is not assignable to type '\"world\"'.`\n    ]\n  }\n]\n```\n\nThe usage example could be found in `demo/hello-world` folder.\n\n\n### Generation\n\nIt is possible to generate typescript files automatically for existing jsons:\n\n```typescript\nconst name = \"LintRule\";\ngenerate(\n    {\n        id: \"rule-semi\",\n        name: \"semi\",\n        description: \"Rule to describe usage of semicolons\",\n        level: \"warning\",\n        isLevel: false,\n    },\n    {\n        name: name,\n        shouldOutput: true,\n    }\n);\n```\n\nIt will output the file: `LintRule.d.ts` with the following contents:\n\n```typescript\nexport interface LintRule {\n    id:          string;\n    name:        string;\n    description: string;\n    level:       string;\n    isLevel:     boolean;\n}\n\ninterface Root extends LintRule {}\n```\n\n\nAdvanced usage:\n\nIn case you want to generate schemas from endpoint, `json-lint-d-ts` supports\nurl samples:\n\n\n```typescript\nconst name = \"UsersEndpoint\";\nconst result = await generateAsync([\"https://yourwebsite.com/api/users?page=1\"],\n    {\n        name: name,\n        shouldOutput: true,\n    }\n);\n```\n\nIn case API returns such data back:\n\n```json\n{\n    \"page\": 1,\n    \"per_page\": 6,\n    \"total\": 12,\n    \"total_pages\": 2,\n    \"data\": [{\n        \"id\": 1,\n        \"email\": \"george.bluth@yourwebsite.com\",\n        \"first_name\": \"George\",\n        \"last_name\": \"Bluth\",\n        \"avatar\": \"https://s3.amazonaws.com/avatar.jpg\"\n    }, {\n        \"id\": 2,\n        \"email\": \"janet.weaver@yourwebsite.com\",\n        \"first_name\": \"Janet\",\n        \"last_name\": \"Weaver\",\n        \"avatar\": \"https://s3.amazonaws.com/avatar.jpg\"\n    }, {\n        \"id\": 3,\n        \"email\": \"emma.wong@yourwebsite.com\",\n        \"first_name\": \"Emma\",\n        \"last_name\": \"Wong\",\n        \"avatar\": \"https://s3.amazonaws.com/avatar.jpg\"\n    }, {\n        \"id\": 4,\n        \"email\": \"eve.holt@yourwebsite.com\",\n        \"first_name\": \"Eve\",\n        \"last_name\": \"Holt\",\n        \"avatar\": \"https://s3.amazonaws.com/avatar.jpg\"\n    }, {\n        \"id\": 5,\n        \"email\": \"charles.morris@yourwebsite.com\",\n        \"first_name\": \"Charles\",\n        \"last_name\": \"Morris\",\n        \"avatar\": \"https://s3.amazonaws.com/avatar.jpg\"\n    }, {\n        \"id\": 6,\n        \"email\": \"tracey.ramos@yourwebsite.com\",\n        \"first_name\": \"Tracey\",\n        \"last_name\": \"Ramos\",\n        \"avatar\": \"https://s3.amazonaws.com/avatar.jpg\"\n    }],\n    \"ad\": {\n        \"company\": \"Weekly News\",\n        \"url\": \"http://news.org/\",\n        \"text\": \"A weekly newsletter focusing on development\"\n    }\n    }\n```\n the following TS file would be generated:\n\n```typescript\n// UsersEndpoint.d.ts\n\nexport interface UsersEndpoint {\n    page:        number;\n    per_page:    number;\n    total:       number;\n    total_pages: number;\n    data:        Datum[];\n    ad:          Ad;\n}\n\nexport interface Ad {\n    company: string;\n    url:     string;\n    text:    string;\n}\n\nexport interface Datum {\n    id:         number;\n    email:      string;\n    first_name: string;\n    last_name:  string;\n    avatar:     string;\n}\n\ninterface Root extends UsersEndpoint {}\n```\n\n\n\n## Troubleshooting\n\nYou could pass boolean value `isDiagnosticsFileCreated` in the object as a second\nargument to the `validate` function\n\n```typescript\nimport { validate } from \"json-lint-d-ts\";\n\nconst result = validate([\n    [\"./hello.json\", \"./hello.d.ts\"],\n], {\n    isDiagnosticsFileCreated: true,\n});\n```\n\nThis will generate a joined file near your json document `hello.json.ts`\nwith your types and json content where you could manually compare the difference.\n\n\n## How does all this work?\n\n### JSON validation:\n\nUnder the hood *json-lint-d-ts* uses Typescript Compiler by extending compiler host.\nJSON objects are loaded from filesystem and compared against passed `.d.ts` files.\nThe result is extracted from diagnostics. \n\n### Typescript Schema generation:\n\nFor Typescript files generation from raw JSON [quicktype](https://github.com/quicktype/quicktype) is used\n\n## Contributing\nPlease do!\n\n## Alternatives\n\n- [jsonschema](https://www.npmjs.com/package/jsonschema): JSON Schema is a vocabulary that allows you to annotate and validate JSON documents http://json-schema.org/.\n- [joi](https://www.npmjs.com/package/joi): schema description language and data validator for JavaScript.\n\n## Enjoy\n\n🚀\n\n[1]: https://david-dm.org/vlashchanka/json-lint-d-ts.svg\n[2]: https://david-dm.org/vlashchanka/json-lint-d-ts\n[3]: https://david-dm.org/vlashchanka/json-lint-d-ts/dev-status.svg\n[4]: https://david-dm.org/vlashchanka/json-lint-d-ts?type=dev\n  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvlashchanka%2Fjson-lint-d-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvlashchanka%2Fjson-lint-d-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvlashchanka%2Fjson-lint-d-ts/lists"}