Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: parse Any and Unknown #50

Closed
FosterSamuel opened this issue Jun 6, 2024 · 2 comments · Fixed by #51
Closed

feature request: parse Any and Unknown #50

FosterSamuel opened this issue Jun 6, 2024 · 2 comments · Fixed by #51

Comments

@FosterSamuel
Copy link
Contributor

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: FosterSamuel@0af202f

Content of minimal json schema file which causes the problem

Click to expand/collapse
{
  "title": "Example",
  "type": "object",
  "properties": {
    "innerAny": {},
    "innerUnknown": {},
  },
  "required": ["innerAny", "innerUnknown"]
}
@xddq
Copy link
Owner

xddq commented Jun 6, 2024

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
Copy link
Contributor Author

Thanks @xddq! Opened #51 .

@xddq xddq closed this as completed in #51 Jun 6, 2024
xddq added a commit that referenced this issue Jun 6, 2024
## Summary

Parse `{}` in schemas as `Type.Unknown()`. Closes #50.

`Type.Any()` and `Type.Unknown()` produce `{}` in schemas. When `{}` was
parsed, the unsupported schema error was thrown. Now `{}` is parsed to
`Type.Unknown()` without an error.

Added test in `parser.spec.ts`, bumped version, and added changelog
item.

---------

Co-authored-by: Pierre Dahmani <hi@pierre-dev.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants