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 before_validation,after_validation options to hook into the validation process #77

Merged
merged 12 commits into from
Oct 9, 2020
Merged

Conversation

ahx
Copy link
Contributor

@ahx ahx commented Sep 3, 2020

This adds before_validation, after_validation options to modify data before or after validation. The insert_property_defaults now is one of these before hooks.
One use case of this is to parse data based on your JSON Schema before or after the validation is run.

The after_validation option should basically solve #49.

In it's current state this only works when validating objects. 😬 What do you think?

@ahx ahx changed the title Add before_validation option to hook into the validation process Add before_validation,after_validation options to hook into the validation process Sep 3, 2020
@ahx ahx marked this pull request as ready for review September 3, 2020 13:00
@ahx
Copy link
Contributor Author

ahx commented Sep 3, 2020

Should we name these before_object_validation / after_object_validation to make clear that this is only useful when validating a Hash? 🤔

Copy link
Owner

@davishmcclurg davishmcclurg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool! It's great to see it used to implement insert_property_defaults.

Should we name these before_object_validation / after_object_validation to make clear that this is only useful when validating a Hash? 🤔

What do you think about before_property_validation/after_property_validation? That is consistent with insert_property_defaults.

README.md Outdated Show resolved Hide resolved
lib/json_schemer/schema/base.rb Outdated Show resolved Hide resolved
lib/json_schemer/schema/base.rb Outdated Show resolved Hide resolved
lib/json_schemer/schema/base.rb Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@ahx
Copy link
Contributor Author

ahx commented Sep 16, 2020

I was trying to implement handling readOnly via a before-hook, but I think we should pass the parent schema to these Procs as well to be useful for a use-case like that. I'll push another commit for that tomorrow.

This is useful if you want to act on things like "required" or so in the parent schema or a property
@ahx
Copy link
Contributor Author

ahx commented Sep 17, 2020

I have changed the hook signature so that the parent schema is passed as well.

proc { |data, property, property_schema, schema|  }

README.md Outdated Show resolved Hide resolved
lib/json_schemer/schema/base.rb Show resolved Hide resolved
lib/json_schemer/schema/base.rb Outdated Show resolved Hide resolved
test/json_schemer_test.rb Outdated Show resolved Hide resolved
@ahx ahx requested a review from davishmcclurg October 5, 2020 20:16
@davishmcclurg davishmcclurg merged commit 45b2d0c into davishmcclurg:master Oct 9, 2020
@davishmcclurg
Copy link
Owner

Thanks @ahx!

@davishmcclurg
Copy link
Owner

Released in 0.2.16

@ahx ahx deleted the before_validation_hooks branch October 15, 2020 06:51
@ahx
Copy link
Contributor Author

ahx commented Oct 15, 2020

@davishmcclurg That it so cool.
I think I can push this to cover most of my use cases for #55 or #49 in my projects. 🚀
Thanks for working on this with me. ❤️

This was referenced Oct 15, 2020
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 this pull request may close these issues.

None yet

2 participants