Override Typebox object properties in schema composiiton/merge #268
-
Hello @sinclairzx81 , Sorry, another question. it's possible to "mutate" type box object properties to reuse them to compose schemas?. A common use of case when we are defining schemas is to "merge" types overriding a few properties. We are using https://github.com/boschni/json-merger. So we run I was wondering if it's possible to "override/mutate" properties of a typebox object when reusing it to compose schemas. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
@lucashfreitas Hi! TypeBox types/schemas are considered immutable from a library standpoint. TypeBox doesn't prevent users from mutating schemas...but doing so may invalidate the TS type associated with that schema (so it would be generally discouraged) Composition in TypeBox is however supported; but limited to composition types provided by the library such as // typescript intersect (similar to merge)
interface A { a: number }
interface B { b: number }
type C = A & B
// typebox intersect
const A = Type.Object({ a: Type.Number() })
const B = Type.Object({ b: Type.Number() })
const C = Type.Intersect([A, B]) Here, the It might be possible to use TypeBox to construct correct source schemas that are later passed to Does this help? Or were you just looking to use |
Beta Was this translation helpful? Give feedback.
-
@sinclairzx81 thank you very much! A last and out-of-scope question: By any chance, can this library do the reverse - generating typescript types from JSON schema? I think that's not supported and not indented to, right? I am using some external $refs for a definition for schemas and would like for it to be fully type compatible. I tried several libraries to generate TS types out of JSON schemas but all of them have issues/aren't working correctly, so due to that I have decided to migrate the schema definition to typebox and get it out of the box. :) |
Beta Was this translation helpful? Give feedback.
-
Have a look at https://www.npmjs.com/package/json-schema-to-typescript which may be able to convert your existing JSON Schema to TypeScript. Once you have converted to TypeScript, you might be able to use this to convert TypeScript into TypeBox. To run this, you will need to clone the TypeBox repository and run the generator from the const Code = CodeGen.TypeBox(`
interface A {
x: number,
y: number,
z: number
}
`)
console.log(Code) // print TypeBox translated code. Note: Automatic code translation is a work in progress (and hasn't had a lot of testing) but it may be able to help save you some time. The key will be getting your existing schemas into typescript code first tho. Hope that helps! |
Beta Was this translation helpful? Give feedback.
@lucashfreitas Hi!
TypeBox types/schemas are considered immutable from a library standpoint. TypeBox doesn't prevent users from mutating schemas...but doing so may invalidate the TS type associated with that schema (so it would be generally discouraged)
Composition in TypeBox is however supported; but limited to composition types provided by the library such as
Type.Intersect([A, B])
,Type.KeyOf(A)
,Type.Partial(A)
, etc. I'm not very familiar withjson-merger
but taking a look at it's readme, it doesn't seem like it would be immediately compatible. To merge objects in TypeBox, you would use similar type construction patterns used to compose types in TypeScript. For example.// typescript …