Ability to generate json schema via custom generator
See original GitHub issueProblem
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:
- Created 3 years ago
- Reactions:19
- Comments:11 (7 by maintainers)
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!
I would like to give it a try 😃 Progress can be trailed here: https://github.com/valentinpalkovic/prisma-json-schema-generator