xddq / schema2typebox

Creating TypeBox code from JSON schemas
MIT License
66 stars 15 forks source link

ensure generation of $id as schema option in resulting typebox code #33

Closed xddq closed 10 months ago

xddq commented 1 year ago

Summary

Based on issue #32 . This ensures that the generated typebox code will always contain an $id property. Additional test cases for checking the schemaOptions were added

TODO:

xddq commented 1 year ago

Hey @tet-lenovo,

Thanks for your writeup here. It helped me implement this way faster. Also, can you check if this branch fullfills your requirements?

Also (admittedly, this is a little late :D ). I would want to know why this feature should be added. Honest question, what is the difference between using X and Y here?


import { Static, Type } from "@sinclair/typebox";

export type T = Static<typeof T>;
export const T = Type.Object({ a: Type.Number() }, { $id: "T" });

export const X = Type.Object({ a: Type.Ref(T) });
export const Y = Type.Object({ a: T });
tet-lenovo commented 12 months ago

@xddq to answer the embarrassing question first, other than my apparent lack of knowledge, either X or Y would work for my situation. Otherwise, the code does seem to work.

As a follow up, we are using this for REST request and response validation and the way we currently have this structured, we will need all of the defined schema to have a $id defined so this is a good change regardless.

xddq commented 12 months ago

@tat-lenovo

Oh, I see.. I should ask beforehand for upcoming issues : D

Could you explain why the $id field is needed in your setup? I still don't see the need. Will probably merge this through after your response and release in the next couple of days. I am somewhat busy atm.

xddq commented 11 months ago

@tet-lenovo Hey!

Any update on this?

tet-lenovo commented 11 months ago

Sorry for the delay with this. With the holiday, I haven't been around much the last couple of weeks. Our main use case for this is that we use the generated schemas with AJV for request/response validation. When you register a schema with AJV, you either provide an ID or it will use the $id defined with the schema. So we don't have to maintain another ID, we use the $id generated with the schema.

tet-lenovo commented 10 months ago

@xddq Any idea when you plan to merge this code?

xddq commented 10 months ago

@tet-lenovo hey! Sorry, I did forget about it. Will probably have a look at merging (and publishing a new release) on thursday.