{"id":18862492,"url":"https://github.com/giovannicardamone/class-schema","last_synced_at":"2025-10-08T18:55:06.770Z","repository":{"id":38090665,"uuid":"391601033","full_name":"GiovanniCardamone/class-schema","owner":"GiovanniCardamone","description":"Javascript / Typescript class to JsonSchema","archived":false,"fork":false,"pushed_at":"2023-01-21T16:04:38.000Z","size":361,"stargazers_count":8,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-08-10T23:44:09.475Z","etag":null,"topics":["browser","json-schema","nodejs","typescript"],"latest_commit_sha":null,"homepage":"https://giovannicardam.one/class-schema","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/GiovanniCardamone.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2021-08-01T10:50:36.000Z","updated_at":"2023-07-12T08:06:53.000Z","dependencies_parsed_at":"2023-02-12T11:15:59.300Z","dependency_job_id":null,"html_url":"https://github.com/GiovanniCardamone/class-schema","commit_stats":null,"previous_names":[],"tags_count":63,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GiovanniCardamone%2Fclass-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GiovanniCardamone%2Fclass-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GiovanniCardamone%2Fclass-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GiovanniCardamone%2Fclass-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GiovanniCardamone","download_url":"https://codeload.github.com/GiovanniCardamone/class-schema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223633967,"owners_count":17176870,"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":["browser","json-schema","nodejs","typescript"],"created_at":"2024-11-08T04:34:39.521Z","updated_at":"2025-10-08T18:55:06.672Z","avatar_url":"https://github.com/GiovanniCardamone.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# class-schema\n\n![Logo](media/images/banner.png)\n\n\u003cdiv align=\"center\"\u003e\n\n![JavaScript](https://img.shields.io/badge/ES6-Supported-yellow.svg?style=for-the-badge\u0026logo=JavaScript) ![TypeScript](https://img.shields.io/badge/TypeScript-Supported-blue.svg?style=for-the-badge\u0026logo=Typescript)\n\n[![CI](https://github.com/GiovanniCardamone/class-schema/actions/workflows/npm-ci.yml/badge.svg)](https://github.com/GiovanniCardamone/class-schema/actions/workflows/npm-ci.yml)\n[![Coverage Status](https://coveralls.io/repos/github/GiovanniCardamone/class-schema/badge.svg?branch=main)](https://coveralls.io/github/GiovanniCardamone/class-schema?branch=main)\n[![Known Vulnerabilities](https://snyk.io/test/github/GiovanniCardamone/class-schema/badge.svg)](https://snyk.io/test/github/GiovanniCardamone/class-schema)\n[![NPM version](https://img.shields.io/npm/v/class-schema.svg?style=plastic)](https://www.npmjs.com/package/class-schema)\n[![NPM downloads](https://img.shields.io/npm/dm/class-schema.svg?style=plastic)](https://www.npmjs.com/package/class-schema)\n\n\u003c/div\u003e\n\nclass-schema is a library intended to extract from javascript class, the corrispondent [JSON Schema](https://json-schema.org/), usually, the schema is written by hand or throught some tool that create the schema.\nbut, with this library you can extract the schema directly from the class you defined, so you have a single source of truth of the schema [SSOT](https://en.wikipedia.org/wiki/Single_source_of_truth) that is your class.\n\n## :package: Installation\n\n```bash\nnpm install class-schema\n```\n\nto use `class-schema` you also need the package `reflect-metadata`\n\n```bash\nnpm install reflect-metadata\n```\n\n## :rocket: Usage\n\n### Javascript\n\nIn order to use decorators in javascript, a transpiler that can\n\n```bash\nnpm i -D babel-cli\n```\n\n### TypeScript\n\nYou need to enable `experimentalDecorators` and `emitDecoratorMetadata` in your `tsconfig.json`\n\n```json5\n// file: tsconfig.json\n{\n  compilerOptions: {\n    experimentalDecorators: true,\n    emitDecoratorMetadata: true,\n  },\n}\n```\n\nin your index you have to import `reflect-metadata`\n\n```typescript\nimport 'reflect-metadata'\n```\n\nand you are ready to go!\n\n## :chart_with_upwards_trend: Examples\n\n```typescript\nimport 'reflect-metadata'\nimport { use, schema, prop, ref, enums } from 'class-schema'\n\nconst vowels = ['a', 'e', 'i', 'o', 'u', 'y']\ntype Vowels = typeof vowels[number]\n\n@schema()\nclass MyObject {\n  @enums(vowels)\n  myEnum: Vowels\n}\n\n@schema()\nclass MySchema {\n  @prop()\n  myProp: number\n\n  @array()\n  @prop(Number)\n  myPropArray: number[]\n\n  @ref(MyObject)\n  myObject: MyObject\n}\n```\n\n\u003e to get javascript object that represent jsonschema of class `use(MySchema)`\n\n```json5\n// output of `JSON.stringify(use(MySchema))\n{\n  type: 'object',\n  properties: {\n    myProp: {\n      type: 'number',\n    },\n    myPropArray: {\n      type: 'array',\n      items: {\n        type: 'number',\n      },\n    },\n    myObject: {\n      type: 'object',\n      properties: {\n        myEnum: {\n          type: 'array',\n          items: {\n            type: 'string',\n            enum: ['a', 'e', 'i', 'o', 'u', 'y'],\n          },\n        },\n      },\n      required: ['myEnum'],\n    },\n  },\n  required: ['myProp', 'myPropArray', 'myObject'],\n}\n```\n\n## :toolbox: Summary\n\n### :arrow_forward: use\n\n## :books: Documentation\n\n[Full Documentation](https://giovannicardam.one/class-schema)\n\n## :label: License\n\n[MIT](https://github.com/GiovanniCardamone/class-schema/blob/main/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiovannicardamone%2Fclass-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgiovannicardamone%2Fclass-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiovannicardamone%2Fclass-schema/lists"}