xddq / schema2typebox

Creating TypeBox code from JSON schemas
MIT License
62 stars 14 forks source link

feature request: parse Any and Unknown #50

Closed FosterSamuel closed 3 months ago

FosterSamuel commented 3 months ago

Library version: 1.7.4

JSON schema version: draft-07

I am willing to contribute to fix the issue 💚

The current behavior

When parsing schemas with Any or Unknown, an error is thrown. Example:

const example = Type.Object({
  innerAny: Type.Any(),
  innerUnknown: Type.Unknown(),
});

const input = JSON.stringify(example); 
// {"type":"object","properties":{"innerAny":{},"innerUnknown":{}},"required":["innerAny","innerUnknown"]}

const throwsErrorOnGeneration = await schema2typebox({ input });
// Unsupported schema. Did not match any type of the parsers. Schema was: {}
//  at collect (src/schema-to-typebox.ts:95:9)
//  at collect (src/schema-to-typebox.ts:174:9)
//  at Array.map (<anonymous>)
//  at map (src/schema-to-typebox.ts:172:6)
//  at parseObject (src/schema-to-typebox.ts:73:12)
//  at collect (src/schema-to-typebox.ts:52:23)
//  at schema2typebox (src/programmatic-usage.ts:23:32)
//  at Object.<anonymous> (test/parser.spec.ts:152:25)

The expected behavior

Parse innerAny and innerUnknown as Type.Unknown.

Maybe this was intentionally avoided? I couldn't find anything in my search of the repo discussing this.

Why does it happen? What/How to fix the issue?

Empty {} is not parsed. Branch with fix: https://github.com/FosterSamuel/schema2typebox/commit/0af202fcabf6c2b96fdee3adb63914268a84e41b

Content of minimal json schema file which causes the problem

Click to expand/collapse ```json { "title": "Example", "type": "object", "properties": { "innerAny": {}, "innerUnknown": {}, }, "required": ["innerAny", "innerUnknown"] } ```
xddq commented 3 months ago

hey @FosterSamuel

thanks for raising this issue and fixing it. I quickly looked over the changes, they are looking good. Feel free to create a PR. It will probably be merged and released in the next couple of days

FosterSamuel commented 3 months ago

Thanks @xddq! Opened #51 .