davishmcclurg / json_schemer

JSON Schema validator. Supports drafts 4, 6, 7, 2019-09, 2020-12, OpenAPI 3.0, and OpenAPI 3.1.
MIT License
408 stars 64 forks source link

Option to define custom format validators globally #162

Closed omkarmoghe closed 9 months ago

omkarmoghe commented 12 months ago

Was very interested in using the gem, was curious if there was a way to set or "register" formatters globally? Something similar to https://github.com/voxpupuli/json-schema#custom-format-validation. Would you be open to a PR to enable something like this?

davishmcclurg commented 12 months ago

Hi @omkarmoghe! Currently, there isn't a way to register formats globally. You can pass the formats option, but that only applies to a single schema: https://github.com/davishmcclurg/json_schemer/blob/da9c8f98255552a6b6b025bb43759cd15a2c00fd/README.md?plain=1#L183-L190

I think it's a great idea, though. I imagine we would want to provide global options for everything (not just formats): https://github.com/davishmcclurg/json_schemer/blob/da9c8f98255552a6b6b025bb43759cd15a2c00fd/lib/json_schemer/schema.rb#L53-L69

Would you be open to a PR to enable something like this?

Definitely! Let me know if you want to work on it and I'll do my best help.

omkarmoghe commented 12 months ago

I imagine we would want to provide global options for everything (not just formats)

Agreed! I generally like this approach to making gems configurable: https://dev.to/vinistock/make-a-ruby-gem-configurable-228d

Basically a Configuration singleton object that users can set default options via. Let me know if that approach looks good and I can take a stab at implementing it.

davishmcclurg commented 12 months ago

Basically a Configuration singleton object that users can set default options via. Let me know if that approach looks good and I can take a stab at implementing it.

:+1: that sounds good to me.

davishmcclurg commented 10 months ago

@omkarmoghe are you still interested in implementing this?

omkarmoghe commented 10 months ago

Hi @davishmcclurg, apologies for the delay, been a busy month/holiday season. I'm working on implementing this now! Should have a PR up this week 🤞🏽

omkarmoghe commented 10 months ago

Working in this branch: https://github.com/omkarmoghe/json_schemer/tree/default-config

davishmcclurg commented 9 months ago

Thanks again for the help, @omkarmoghe! I merged your changes and will release it shortly.