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

Add ValidatorSchemaOptions and SerializerSchemaOptions #103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bcomnes
Copy link

@bcomnes bcomnes commented Sep 16, 2024

Original Messsage

I would like to propose that the Plugin definition of this type provider support accepting FromSchemaOptions somewhere in the generics argument, so that I can pass in schema references on routes that have external references.

Is that something you are open to? Feedback welcome. I will add test shortly. I just wanted to open up for some feedback in terms of implementation.

Also, do you want me to target latest or next?

This patch introduces two new options for the JsonSchemaToTsProvider:

  • ValidatorSchemaOptions
  • SerializerSchemaOptions

These are both FromSchemaOptions from the json-schema-to-ts package.

Most importantly, they let you customize the references allowing for named schema references, and secondly deserialize which lets you customize how things are serialized and deserialized from a type perspective. See the updated examples of when this is useful.

They can be passed to the type provider and the plugin providers.

Secondly, since you don't always need to set these, and there are 2 more options, all meta type arguments have been shifted to named arguments instead of unnamed positionals.

I'm happy to undo this second part, but in my experience named arguments for a complex options object like this tends to be more clear and less verbose and more explicit when you want to skip arguments you don't want to set.

I also updated the readme and tests.

BREAKING CHANGE: This is a significant breaking change that requires consumers to update any code that uses type options anywhere.

Checklist

@bcomnes bcomnes changed the title Implement basic options passing idea for discussion Accept FromSchemaOptions in plugin definitions Sep 16, 2024
@bcomnes bcomnes changed the title Accept FromSchemaOptions in plugin definitions Accept FromSchemaOptions in plugin definitions for references Sep 16, 2024
@bcomnes bcomnes force-pushed the references-in-helpers branch from 7b38ce9 to 3dc0e8a Compare September 16, 2024 22:54
@bcomnes
Copy link
Author

bcomnes commented Sep 30, 2024

I'll update this for 4.0. Is this (or similar) something people would be willing to land?

@bcomnes bcomnes force-pushed the references-in-helpers branch from 3dc0e8a to 8efd6bc Compare October 5, 2024 20:10
@mcollina mcollina requested review from climba03003 and Uzlopak and removed request for climba03003 October 17, 2024 16:32
@mcollina
Copy link
Member

I'm ok to land this in v4. Can you update this?

Sorry for the long wait, it ended up at the bottom of my long GH notification queue.

@bcomnes
Copy link
Author

bcomnes commented Oct 17, 2024

Yeah. Been playing with it inside of a fork. Will update

@mikaelkaron
Copy link

This would be a nice addition, currently working it around by using .withTypeProvider, but it would be nice sugar to have it in the plugin definition instead.

@bcomnes
Copy link
Author

bcomnes commented Dec 17, 2024

Sorry to be slow on this PR, if anyone wants to beat me to it, feel free.

The main hangup I'm having here is figuring out an API that is acceptable.

I think given the new changes in fastify type providers, the generic should accept two options objects, one for the serializer and one for the validator. In practice, I've found that I need to pass serialize options to one side and not the other.

@bcomnes bcomnes force-pushed the references-in-helpers branch from 8efd6bc to 91e9b5f Compare December 18, 2024 22:58
@bcomnes bcomnes changed the base branch from v3.x to main December 18, 2024 23:01
@bcomnes
Copy link
Author

bcomnes commented Dec 18, 2024

Ok, I refactored a bunch of discoveries I made while using this. I think this is ready to go but open to feedback and objections regarding the breaking change.

cc @mcollina this is ready to go, @Fdawgs as the most active maintainer and also @kalvenschraut as the original contributor of these options.

I'm quite happy with how this turned out, especially when used with the async plugin provider. Very clean!

Published my fork at @bcomnes/fastify-type-provider-json-schema-to-ts if anyone wants to play with it in the meantime.

BREAKING CHANGE: Use named type arguments and also pass ValidatorSchemaOptions and SerializerSchemaOptions to their respective type provider.
@bcomnes bcomnes force-pushed the references-in-helpers branch from 91e9b5f to 72a5cfa Compare December 18, 2024 23:35
@bcomnes bcomnes changed the title Accept FromSchemaOptions in plugin definitions for references Add ValidatorSchemaOptions and SerializerSchemaOptions Dec 18, 2024
@mikaelkaron
Copy link

I'll try the fork in my project and drop feedback here if anything breaks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants