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

Option to define custom format validators globally #162

Closed
omkarmoghe opened this issue Dec 1, 2023 · 7 comments
Closed

Option to define custom format validators globally #162

omkarmoghe opened this issue Dec 1, 2023 · 7 comments

Comments

@omkarmoghe
Copy link
Contributor

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
Copy link
Owner

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:

json_schemer/README.md

Lines 183 to 190 in da9c8f9

# custom formats
formats: {
'int32' => proc do |instance, _format|
instance.is_a?(Integer) && instance.bit_length <= 32
end,
# disable specific format
'email' => false
},

I think it's a great idea, though. I imagine we would want to provide global options for everything (not just formats):

base_uri: DEFAULT_BASE_URI,
meta_schema: nil,
vocabulary: nil,
format: true,
formats: DEFAULT_FORMATS,
content_encodings: DEFAULT_CONTENT_ENCODINGS,
content_media_types: DEFAULT_CONTENT_MEDIA_TYPES,
keywords: DEFAULT_KEYWORDS,
before_property_validation: DEFAULT_BEFORE_PROPERTY_VALIDATION,
after_property_validation: DEFAULT_AFTER_PROPERTY_VALIDATION,
insert_property_defaults: false,
property_default_resolver: DEFAULT_PROPERTY_DEFAULT_RESOLVER,
ref_resolver: DEFAULT_REF_RESOLVER,
regexp_resolver: 'ruby',
output_format: 'classic',
resolve_enumerators: false,
access_mode: nil

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

omkarmoghe commented Dec 4, 2023

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
Copy link
Owner

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.

👍 that sounds good to me.

@davishmcclurg
Copy link
Owner

@omkarmoghe are you still interested in implementing this?

@omkarmoghe
Copy link
Contributor Author

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

davishmcclurg added a commit that referenced this issue Mar 2, 2024
Features:

- Global configuration
- Symbol key property defaults
- Better schema validation support

See individual commits for more details.

Closes:

- #157
- #162
- #167
- #173
davishmcclurg added a commit that referenced this issue Mar 2, 2024
Features:

- Global configuration
- Symbol key property defaults
- Better schema validation support

See CHANGELOG.md and individual commits for more details.

Closes:

- #157
- #162
- #167
- #173
@davishmcclurg davishmcclurg mentioned this issue Mar 2, 2024
@davishmcclurg
Copy link
Owner

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

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

No branches or pull requests

2 participants