question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to generate json schema via custom generator

See original GitHub issue

Problem

There are some servers like fastify that allow to define json schemas in order to provide better performance and input / output data validation. Right now I am using prisma with fastify and have to manually define such schemas. Most of these, however, would match types generated by prisma 1 to 1. I looked around and found few tools like json-schema-to-typescript and typebox that convert json schemas to types, but nothing that does the opposite.

Besides these servers there are some services like aws api gateway that also utalise such schemas. It would be awesome to have these generated by prisma alongside types (say under a flag in cli?), as I think prisma has all data to do this.

Suggested solution

Perhaps introduce a new cli flag like --json-schema to prisma generate command that would output these alongside types so they can be imported an used?

Alternatives

Right now these have to be written manually.

Additional context

Example below shows how a simple prisma model can be converted to json schema, there are more examples available at https://json-schema.org/understanding-json-schema that show various data type outputs for json schemas.

prisma schema

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  email     String   @unique
  name      String?
  role      Role     @default(USER)
  posts     Post[]
}

And I believe json schema for this would look as simple as

json schema output

const UserSchema = {
   id: 'number',
   createdAt: 'string',
   email: 'string',
   name: 'string',
   role: {
     type: 'string',
     enum: ['ADMIN', 'AUTHOR']
   },
   posts: {
      type: 'array',
      items: {
         type: 'object',
         properties: {
            id: 'number'
         }
      }
   }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:19
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

13reactions
valentinpalkoviccommented, Sep 23, 2020

And here it is! https://www.npmjs.com/package/prisma-json-schema-generator

It is my first npm package though. Just give me a hint, if something is not working from the beginning on and create an issue if you miss a functionality or something is not working like expected!

7reactions
valentinpalkoviccommented, Sep 21, 2020

I would like to give it a try 😃 Progress can be trailed here: https://github.com/valentinpalkovic/prisma-json-schema-generator

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementations - JSON Schema
Dashjoin JSON Schema editor - Graphical online JSON Schema editor for draft-06 (minus oneOf, anyOf, allOf, not). The generated schema can be tested...
Read more >
JSON Schema Validator, Generator & Editor Guide - Stoplight
With this JSON Schema file, we've established that we can expect, but not require, JSON files with the id key. The power of...
Read more >
Tool to generate JSON schema from JSON data - Stack Overflow
GenSON (PyPI | Github) is a JSON Schema generator that can generate a single schema from multiple objects. You can also merge schemas...
Read more >
Introduction – API Reference
The victools:jsonschema-generator aims at allowing the generation of JSON Schema (Draft 6, Draft 7, Draft 2019-09 or Draft 2020-12) to document Java code....
Read more >
genson - PyPI
GenSON is a powerful, user-friendly JSON Schema generator.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found