xddq / schema2typebox

Creating TypeBox code from JSON schemas
MIT License
54 stars 12 forks source link

feature/unions for array or objects #48

Closed xddq closed 1 month ago

xddq commented 1 month ago

originally done by @smeijer here. just created new PR cause I somehow could not commit to the original one. I think the reason is that the pr creator account and the fork live in different accounts. But it is just an assumption.

Summary

This adds support for unions that have an array or object in them, like in the schema below.

Other unions like [string, null] were already supported. Unions with arrays [array, null] and objects [object, null] were not. Tests covering these new cases are included.

{
  type: "object",
  properties: {
    nullableObject: {
      type: ["object", "null"],
      properties: {
        name: { type: "string" },
      }
    },
    nullableArray: {
      type: ["array", "null"],
      items: {
        type: "string"
      },
    },
  }
});

I'm making this change, because we have schemas with nullable arrays and objects. Without this change, those schemas cannot be parsed.

Fixes #43