{"id":13474951,"url":"https://github.com/valentinpalkovic/prisma-json-schema-generator","last_synced_at":"2025-05-15T01:06:09.014Z","repository":{"id":37072181,"uuid":"297043696","full_name":"valentinpalkovic/prisma-json-schema-generator","owner":"valentinpalkovic","description":"A generator for Prisma 2 to generate a valid JSON Schema (v7)","archived":false,"fork":false,"pushed_at":"2025-05-05T22:13:00.000Z","size":9292,"stargazers_count":312,"open_issues_count":19,"forks_count":36,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-08T11:46:45.007Z","etag":null,"topics":["json-schema","prisma","prisma-generator","prisma-schema","prisma2"],"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/valentinpalkovic.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-09-20T09:36:10.000Z","updated_at":"2025-05-07T02:27:50.000Z","dependencies_parsed_at":"2023-09-26T02:35:43.800Z","dependency_job_id":"f8dfbbbd-0ec9-4844-9520-b073f754e0b5","html_url":"https://github.com/valentinpalkovic/prisma-json-schema-generator","commit_stats":{"total_commits":1462,"total_committers":11,"mean_commits":132.9090909090909,"dds":"0.12106703146374831","last_synced_commit":"a1195fe64bb816f3755784b09977cfa3566fd6fe"},"previous_names":[],"tags_count":58,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valentinpalkovic%2Fprisma-json-schema-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valentinpalkovic%2Fprisma-json-schema-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valentinpalkovic%2Fprisma-json-schema-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valentinpalkovic%2Fprisma-json-schema-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/valentinpalkovic","download_url":"https://codeload.github.com/valentinpalkovic/prisma-json-schema-generator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254254040,"owners_count":22039792,"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-schema","prisma","prisma-generator","prisma-schema","prisma2"],"created_at":"2024-07-31T16:01:16.196Z","updated_at":"2025-05-15T01:06:08.989Z","avatar_url":"https://github.com/valentinpalkovic.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"[![Actions Status](https://github.com/valentinpalkovic/prisma-json-schema-generator/workflows/build/badge.svg)](https://github.com/valentinpalkovic/prisma-json-schema-generator/actions)\n[![Code QL](https://github.com/valentinpalkovic/prisma-json-schema-generator/workflows/CodeQL/badge.svg)](https://github.com/valentinpalkovic/prisma-json-schema-generator/workflows/CodeQL/badge.svg)\n[![npm](https://img.shields.io/npm/v/prisma-json-schema-generator)](https://www.npmjs.com/package/prisma-json-schema-generator)\n[![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://github.com/valentinpalkovic/prisma-json-schema-generator/blob/main/LICENSE)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![Open Source? Yes!](https://badgen.net/badge/Open%20Source%20%3F/Yes%21/blue?icon=github)](https://github.com/Naereen/badges/)\n\n# Prisma JSON Schema Generator\n\nA generator, which takes a Prisma 2 `schema.prisma` and generates a JSON Schema in version 7 of the specification (https://json-schema.org/).\n\n## Getting Started\n\n**1. Install**\n\nnpm:\n\n```shell\nnpm install prisma-json-schema-generator --save-dev\n```\n\nyarn:\n\n```shell\nyarn add -D prisma-json-schema-generator\n```\n\n**2. Add the generator to the schema**\n\n```prisma\ngenerator jsonSchema {\n  provider = \"prisma-json-schema-generator\"\n}\n```\n\nWith a custom output path (default=./json-schema)\n\n```prisma\ngenerator jsonSchema {\n  provider = \"prisma-json-schema-generator\"\n  output = \"custom-output-path\"\n}\n```\n\nAdditional options\n\n```prisma\ngenerator jsonSchema {\n  provider = \"prisma-json-schema-generator\"\n  keepRelationScalarFields = \"true\"\n  schemaId = \"some-schema-id\"\n  includeRequiredFields = \"true\"\n  persistOriginalType = \"true\"\n  forceAnyOf = \"true\"\n}\n```\n\nThe generator currently supports a few options\n\n| Key                      | Default Value | Description                                                                                                                                                                                            |\n| ------------------------ | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| keepRelationScalarFields | \"false\"       | By default, the JSON Schema that's generated will output only objects for related model records. If set to \"true\", this will cause the generator to also output foreign key fields for related records |\n| keepRelationFields       | \"true\"        | Determines whether to include fields from related models in the generated schema. Setting it to `\"false\"` allows excluding related model fields from the schema.                                       |\n| schemaId                 | undefined     | Add an id to the generated schema. All references will include the schema id                                                                                                                           |\n| includeRequiredFields    | \"false\"       | If this flag is `\"true\"` all required scalar prisma fields that do not have a default value, will be added to the `required` properties field for that schema definition.                              |\n| persistOriginalType      | \"false\"       | If this flag is `\"true\"` the original type will be outputed under the property key \"originalType\"                                                                                                      |\n| forceAnyOf               | \"false\"       | If this flag is `\"true\"` the union types will be forced to use `anyOf`. Check [contradictory types](https://ajv.js.org/strict-mode.html#contradictory-types) for details                               |\n\n**3. Run generation**\n\nprisma:\n\n```shell\nprisma generate\n```\n\nnexus with prisma plugin:\n\n```shell\nnexus build\n```\n\n## Supported Node Versions\n\n|         Node Version | Support            |\n| -------------------: | :----------------- |\n| (Maintenance LTS) 18 | :heavy_check_mark: |\n|             (LTS) 20 | :heavy_check_mark: |\n|         (Current) 21 | :heavy_check_mark: |\n\n## Examples\n\n### PostgreSQL\n\nThis generator converts a prisma schema like this:\n\n```prisma\ndatasource db {\n\tprovider = \"postgresql\"\n\turl      = env(\"DATABASE_URL\")\n}\n\nmodel User {\n    id                  Int      @id @default(autoincrement())\n    // Double Slash Comment: It will NOT show up in JSON schema\n    createdAt           DateTime @default(now())\n    /// Triple Slash Comment: It will show up in JSON schema [EMAIL]\n    email               String   @unique\n    weight              Float?\n    is18                Boolean?\n    name                String?\n    number              BigInt   @default(34534535435353)\n    favouriteDecimal    Decimal\n    bytes               Bytes /// Triple Slash Inline Comment: It will show up in JSON schema [BYTES]\n    successorId         Int?     @unique\n    successor           User?    @relation(\"BlogOwnerHistory\", fields: [successorId], references: [id])\n    predecessor         User?    @relation(\"BlogOwnerHistory\")\n    role                Role     @default(USER)\n    posts               Post[]\n    keywords            String[]\n    biography           Json\n}\n\nmodel Post {\n    id     Int   @id @default(autoincrement())\n    user   User? @relation(fields: [userId], references: [id])\n    userId Int?\n}\n\nenum Role {\n    USER\n    ADMIN\n}\n```\n\ninto:\n\n```javascript\n{\n    $schema: 'http://json-schema.org/draft-07/schema#',\n    definitions: {\n        Post: {\n            properties: {\n                id: { type: 'integer' },\n                user: {\n                    anyOf: [\n                        { $ref: '#/definitions/User' },\n                        { type: 'null' },\n                    ],\n                },\n            },\n            type: 'object',\n        },\n        User: {\n            properties: {\n                biography: {\n                    type: [\n                        'number',\n                        'string',\n                        'boolean',\n                        'object',\n                        'array',\n                        'null'\n                    ],\n                },\n                createdAt: { format: 'date-time', type: 'string' },\n                email: {\n                    description: 'Triple Slash Comment: Will show up in JSON schema [EMAIL]',\n                    type: 'string'\n                },\n                id: { type: 'integer' },\n                is18: { type: ['boolean', 'null'] },\n                keywords: { items: { type: 'string' }, type: 'array' },\n                name: { type: ['string', 'null'] },\n                number: { type: 'integer', default: '34534535435353' },\n                bytes: {\n                    description: 'Triple Slash Inline Comment: Will show up in JSON schema [BYTES]',\n                    type: 'string'\n                },\n                favouriteDecimal: { type: 'number' },\n                posts: {\n                    items: { $ref: '#/definitions/Post' },\n                    type: 'array',\n                },\n                predecessor: {\n                    anyOf: [\n                        { $ref: '#/definitions/User' },\n                        { type: 'null' },\n                    ],\n                },\n                role: { enum: ['USER', 'ADMIN'], type: 'string', default: 'USER' },\n                successor: {\n                    anyOf: [\n                        { $ref: '#/definitions/User' },\n                        { type: 'null' },\n                    ],\n                },\n                weight: { type: ['integer', 'null'] },\n            },\n            type: 'object',\n        },\n    },\n    properties: {\n        post: { $ref: '#/definitions/Post' },\n        user: { $ref: '#/definitions/User' },\n    },\n    type: 'object',\n}\n```\n\nSo the following input will correctly be validated:\n\n```javascript\n{\n    post: {\n        id: 0,\n        user: {\n            id: 100,\n        },\n    },\n    user: {\n        id: 10,\n        createdAt: '1997-07-16T19:20:30.45+01:00',\n        email: 'jan@scharnow.city',\n        biography: {\n            bornIn: 'Scharnow',\n        },\n        is18: true,\n        keywords: ['prisma2', 'json-schema', 'generator'],\n        name: null,\n        posts: [\n            {\n                id: 4,\n            },\n            {\n                id: 20,\n            },\n        ],\n        predecessor: {\n            id: 10,\n            email: 'horst@wassermann.de',\n        },\n        successor: null,\n        role: 'USER',\n        weight: 10.14,\n    },\n}\n```\n\n### MongoDB\n\nThe generator also takes care of composite types in MongoDB:\n\n```prisma\ndatasource db {\n    provider = \"mongodb\"\n    url      = env(\"DATABASE_URL\")\n}\n\nmodel User {\n    id      String @id @default(auto()) @map(\"_id\") @db.ObjectId\n    photos  Photo[]\n}\n\ntype Photo {\n    height Int      @default(200)\n    width  Int      @default(100)\n    url    String\n}\n```\n\nOutput:\n\n```javascript\n{\n    $schema: 'http://json-schema.org/draft-07/schema#',\n    definitions: {\n        User: {\n            properties: {\n                id: { type: 'string' },\n                photos: {\n                    items: { $ref: '#/definitions/Photo' },\n                    type: 'array',\n                },\n            },\n            type: 'object',\n        },\n        Photo: {\n            properties: {\n                height: {\n                    type: 'integer',\n                    default: 200,\n                },\n                width: {\n                    type: 'integer',\n                    default: 100,\n                },\n                url: {\n                    type: 'string',\n                },\n            },\n            type: 'object',\n        },\n    },\n    properties: {\n        user: { $ref: '#/definitions/User' },\n    },\n    type: 'object',\n}\n```\n\n### No relation fields\n\nFor some use cases, it might be useful to not include relation fields in the generated schema. This can be achieved by setting the `keepRelationFields` option to `\"false\"` and the `keepRelationScalarFields` option to `\"true\"`. For example if you want to use the generated schema to validate POST request object for instance, you might want to use this option.\n\n```prisma\ndatasource db {\n    provider = \"postgresql\"\n    url      = env(\"DATABASE_URL\")\n}\n\ngenerator jsonSchema {\n  provider                 = \"prisma-json-schema-generator\"\n  keepRelationScalarFields = \"true\" // default is \"false\"\n  keepRelationFields       = \"false\" // default is \"true\"\n}\n\nmodel User {\n    id                  Int      @id @default(autoincrement())\n    createdAt           DateTime @default(now())\n    email               String   @unique\n    weight              Float?\n    is18                Boolean?\n    name                String?\n    number              BigInt   @default(34534535435353)\n    favouriteDecimal    Decimal\n    bytes               Bytes\n    successorId         Int?     @unique\n    successor           User?    @relation(\"BlogOwnerHistory\", fields: [successorId], references: [id])\n    predecessor         User?    @relation(\"BlogOwnerHistory\")\n    role                Role     @default(USER)\n    posts               Post[]\n    keywords            String[]\n    biography           Json\n}\n\nmodel Post {\n    id     Int   @id @default(autoincrement())\n    user   User? @relation(fields: [userId], references: [id])\n    userId Int?\n}\n\nenum Role {\n    USER\n    ADMIN\n}\n```\n\nOutput:\n\n```javascript\n{\n    $schema: 'http://json-schema.org/draft-07/schema#',\n    definitions: {\n        Post: {\n            properties: {\n                id: { type: 'integer' },\n                userId: { type: ['integer', 'null'] },\n            },\n            type: 'object',\n        },\n        User: {\n            properties: {\n                biography: {\n                    type: [\n                        'number',\n                        'string',\n                        'boolean',\n                        'object',\n                        'array',\n                        'null'\n                    ],\n                },\n                createdAt: { format: 'date-time', type: 'string' },\n                email: {\n                    description: 'Triple Slash Comment: Will show up in JSON schema [EMAIL]',\n                    type: 'string'\n                },\n                id: { type: 'integer' },\n                is18: { type: ['boolean', 'null'] },\n                keywords: { items: { type: 'string' }, type: 'array' },\n                name: { type: ['string', 'null'] },\n                number: { type: 'integer', default: '34534535435353' },\n                bytes: {\n                    description: 'Triple Slash Inline Comment: Will show up in JSON schema [BYTES]',\n                    type: 'string'\n                },\n                favouriteDecimal: { type: 'number' },\n                role: { enum: ['USER', 'ADMIN'], type: 'string', default: 'USER' },\n                successorId: { type: ['integer', 'null'] },\n                weight: { type: ['integer', 'null'] },\n            },\n            type: 'object',\n        },\n    },\n    properties: {\n        post: { $ref: '#/definitions/Post' },\n        user: { $ref: '#/definitions/User' },\n    },\n    type: 'object',\n}\n```\n\n## License: MIT\n\nCopyright (c) 2020 Valentin Palkovič\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n```\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalentinpalkovic%2Fprisma-json-schema-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvalentinpalkovic%2Fprisma-json-schema-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalentinpalkovic%2Fprisma-json-schema-generator/lists"}