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

Replace ValidationError::schema with custom errors #280

Conversation

zhiburt
Copy link
Contributor

@zhiburt zhiburt commented Oct 2, 2021

Hi there, bump into here via hacktoberfest mark.

Honestly most likely it doesn't address JSONPointer correctly in errors.
And I was not sure which type of error better use for things like as64 error or regex syntax error but I did used ValidationError::format.

Note: there might be an issue with cargo fmt.

closes #235

Thank you for the library.

@Stranger6667
Copy link
Owner

Thank you @zhiburt !

I am happy to see your contribution :) I'll try to review this in the next few days :) Could you, please:

  • Run cargo fmt
  • Remove the unused function
  • Add a changelog entry

@Stranger6667
Copy link
Owner

Honestly most likely it doesn't address JSONPointer correctly in errors.

I think it is alright, in any case, it is an improvement over the status quo

And I was not sure which type of error better use for things like as64 error or regex syntax error but I did used

That's alright :)

@codecov
Copy link

codecov bot commented Oct 2, 2021

Codecov Report

Merging #280 (63c99d3) into master (3b57866) will decrease coverage by 2.64%.
The diff coverage is 9.05%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #280      +/-   ##
==========================================
- Coverage   83.15%   80.50%   -2.65%     
==========================================
  Files          56       56              
  Lines        5124     5336     +212     
==========================================
+ Hits         4261     4296      +35     
- Misses        863     1040     +177     
Impacted Files Coverage Δ
jsonschema/src/compilation/mod.rs 85.92% <0.00%> (-6.08%) ⬇️
jsonschema/src/error.rs 62.56% <ø> (+0.13%) ⬆️
jsonschema/src/keywords/additional_items.rs 77.92% <0.00%> (-7.80%) ⬇️
jsonschema/src/keywords/all_of.rs 80.39% <0.00%> (-6.85%) ⬇️
jsonschema/src/keywords/any_of.rs 84.78% <0.00%> (-8.08%) ⬇️
jsonschema/src/keywords/content.rs 75.00% <0.00%> (-6.46%) ⬇️
jsonschema/src/keywords/dependencies.rs 77.77% <0.00%> (-12.47%) ⬇️
jsonschema/src/keywords/enum_.rs 72.30% <0.00%> (-4.75%) ⬇️
jsonschema/src/keywords/exclusive_maximum.rs 81.69% <0.00%> (-4.88%) ⬇️
jsonschema/src/keywords/exclusive_minimum.rs 77.46% <0.00%> (-4.63%) ⬇️
... and 42 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3b57866...63c99d3. Read the comment docs.

Copy link
Owner

@Stranger6667 Stranger6667 left a comment

Choose a reason for hiding this comment

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

During the compilation phase, schema_path means a path within the JSON Schema meta schemas, and instance_path is a path within the input schema. Given that, the schema_path argument to ValidationError constructors can't be easily derived, so we can leave it empty - this is a corner case anyway (something that metaschema validation doesn't catch).

So, we need to put pointers constructed from context to instance_path argument and JSONPointer::default() to the schema_path one. As far as I see, it is the other way around now.

jsonschema/src/keywords/additional_properties.rs Outdated Show resolved Hide resolved
Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
@zhiburt
Copy link
Contributor Author

zhiburt commented Oct 4, 2021

During the compilation phase, schema_path means a path within the JSON Schema meta schemas

So its essentially pointers to this map?

https://github.com/Stranger6667/jsonschema-rs/blob/aaadd99b2cc376d9018759ecedbbe68c0700d2ab/jsonschema/src/compilation/options.rs#L28-L74

I am curious if we could reuse self.schema_path in structures like this as schema_path arg in error.

https://github.com/Stranger6667/jsonschema-rs/blob/fba763fd4fed01ae8073c6a0c9204cb8c7080933/jsonschema/src/keywords/pattern.rs#L19-L23

@Stranger6667
Copy link
Owner

So its essentially pointers to this map?

Yep, I'd say that they point to some locations within the values of this map.

I am curious if we could reuse self.schema_path in structures like this as schema_path arg in error.

In the majority of the cases, these schema_path values are already used by the meta-schema validators. If the meta-schema is not capable to catch the bug, then these manual error-handling scenarios should catch the problem.

Of course, we may try to keep the meta-schema context manually and trace all the paths during the compilation. However, it will be hard to do as we'll need to do it differently for different JSON Schema drafts, so I assume it will be easier to tweak the meta-schema instead - it happened before and was caused by an actual bug in meta schemas.

Hope I helps :)

@zhiburt
Copy link
Contributor Author

zhiburt commented Oct 4, 2021

I am curious if we could reuse self.schema_path in structures like this as schema_path arg in error.

In the majority of the cases, these schema_path values are already used by the meta-schema validators. If the meta-schema is not capable to catch the bug, then these manual error-handling scenarios should catch the problem.

I guess I did make a typo.
I meant to say maybe we could use this self.schema_path in errors.
Like this

ValidationError::format(self.schema_path, context.clone().into_pointer(), ...)

@Stranger6667
Copy link
Owner

I meant to say maybe we could use this self.schema_path in errors.

These structures are available only after compilation, the compilation process creates a tree of them. We already use meta-schema validators during the compilation process - errors from it will have exactly these schema_path values.

Copy link
Owner

@Stranger6667 Stranger6667 left a comment

Choose a reason for hiding this comment

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

Added a few comments - most of them are quite similar to each other and mostly are about choosing the proper ValidationError variant

jsonschema/src/keywords/additional_items.rs Outdated Show resolved Hide resolved
jsonschema/src/keywords/additional_properties.rs Outdated Show resolved Hide resolved
jsonschema/src/keywords/legacy/type_draft_4.rs Outdated Show resolved Hide resolved
jsonschema/src/keywords/legacy/type_draft_4.rs Outdated Show resolved Hide resolved
jsonschema/src/keywords/legacy/type_draft_4.rs Outdated Show resolved Hide resolved
jsonschema/src/keywords/max_length.rs Outdated Show resolved Hide resolved
jsonschema/src/keywords/max_properties.rs Outdated Show resolved Hide resolved
jsonschema/src/keywords/min_items.rs Outdated Show resolved Hide resolved
jsonschema/src/keywords/min_length.rs Outdated Show resolved Hide resolved
jsonschema/src/keywords/min_properties.rs Outdated Show resolved Hide resolved
Co-authored-by: dmitry.dygalo <dadygalo@gmail.com>
Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
@zhiburt
Copy link
Contributor Author

zhiburt commented Oct 5, 2021

You've spend quite a bit of time reviewing it.

@zhiburt zhiburt requested a review from Stranger6667 October 5, 2021 18:11
Copy link
Owner

@Stranger6667 Stranger6667 left a comment

Choose a reason for hiding this comment

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

Thanks! I think we're getting closer to merging :) A couple of minor things

jsonschema/src/keywords/min_properties.rs Outdated Show resolved Hide resolved
jsonschema/src/keywords/contains.rs Outdated Show resolved Hide resolved
Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
Co-Authored-by: dmitry.dygalo <dadygalo@gmail.com>
@Stranger6667 Stranger6667 merged commit 57c9bca into Stranger6667:master Oct 6, 2021
@Stranger6667
Copy link
Owner

Awesome! Thanks :)

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.

Replace ValidationError::schema calls with proper types
2 participants