gcornut / valibot-json-schema

Converter from Valibot schemas to JSON schema
https://www.npmjs.com/package/@gcornut/valibot-json-schema
MIT License
44 stars 4 forks source link

How can I use instance #17

Closed seba9989 closed 6 months ago

seba9989 commented 6 months ago

Hello,

I would like to know how to use instance (for example: instance(File)) with valibot-json-schema.

And if there is no support for this type I would like to know if it is planned.

Thanks!

gcornut commented 6 months ago

There is no standard way to serialize a file into json. Are you using some kind of web framework that uses json to transfer files ? I don’t think this lib should try to convert any non standard json types but I could look into adding an option to let you customize how unsupported valibot schema should be converted to json schema

seba9989 commented 6 months ago

Specifically, I use a library for sveltekit which this allows for convenient handling of forms. (https://superforms.rocks/get-started/valibot). Adapter module for valibot will exploit your project.

gcornut commented 6 months ago

v0.0.26 now provides a customSchemaConversions option

You can now do toJSONSchema({ schema: instance(File), customSchemaConversions: { instance: () => ... } }) and decide how you want to convert unsupported instance schema into JSON schema.

To skip the validation you could just return an empty JSON schema ({})

gcornut commented 6 months ago

Looks like AJV (a JSON schema validator) offers extensions to handle JS instanceof: https://ajv.js.org/packages/ajv-keywords.html#instanceof

customSchemaConversions could definitely be used to output an ajv compatible schema for the valibot instance schema