-
Notifications
You must be signed in to change notification settings - Fork 280
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
ISSUE-1742 Replace kwalify with dry-schema for schema validation #1749
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file replaces the old schema.yml
raise Errors::ConfigFileError, error_message(errors) | ||
raise Errors::ConfigFileError, error_message(result.errors) | ||
rescue NoMethodError | ||
raise Errors::ConfigFileError, "Invalid configuration file at #{Reek::DEFAULT_CONFIGURATION_FILE_NAME}." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle unparsable config files
ee4f69d
to
15478a9
Compare
Wow, this looks great! Approved from my side! Wdyt @mvz ? |
I'll review this evening. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good and is a great improvement over our current validation.
Just one nit and a question, see below:
lib/reek/configuration/schema.rb
Outdated
optional(:enabled).filled(:bool) | ||
optional(:exclude).array(:string) | ||
end | ||
optional(:DataClump).filled(:hash) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DataClump key is defined twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed, thank you!
end | ||
|
||
def validate | ||
errors = CLI::Silencer.without_warnings { @validator.validate @configuration } | ||
return if !errors || errors.empty? | ||
result = CLI::Silencer.without_warnings { @validator.call(@configuration) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the silencing still needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, it is not needed. Removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review @mvz !
I made an additional commit to make review easier.
If it is good to keep I will squash it.
Note how I use lib/reek/configuration/configuration_file_finder.rb to catch invalid schema error messages.
lib/reek/configuration/schema.rb
Outdated
optional(:enabled).filled(:bool) | ||
optional(:exclude).array(:string) | ||
end | ||
optional(:DataClump).filled(:hash) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed, thank you!
end | ||
|
||
def validate | ||
errors = CLI::Silencer.without_warnings { @validator.validate @configuration } | ||
return if !errors || errors.empty? | ||
result = CLI::Silencer.without_warnings { @validator.call(@configuration) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, it is not needed. Removed.
@@ -58,11 +58,11 @@ def load_from_file(path) | |||
|
|||
begin | |||
configuration = YAML.load_file(path) || {} | |||
SchemaValidator.new(configuration).validate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving it here so we can catch configuration file errors raised by the validator.
The benefit is that we have access to the config file path here.
If this is not ideal, I can revert to the way we had but won't have access to the config file path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense 👍
raise Errors::ConfigFileError, error_message(errors) | ||
raise Errors::ConfigFileError, error_message(result.errors) | ||
rescue NoMethodError | ||
raise Errors::ConfigFileError, 'unrecognized configuration data' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raise shorter error messages, because they get caught and used in lib/reek/configuration/configuration_file_finder.rb
85ad41b
to
d123127
Compare
@@ -58,11 +58,11 @@ def load_from_file(path) | |||
|
|||
begin | |||
configuration = YAML.load_file(path) || {} | |||
SchemaValidator.new(configuration).validate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense 👍
Just waiting for CI, then it's ready to squash and then merge 🎉 |
The current validator (Kwalify) seems outdated and lacks good documentation. A recent issue showed that we could improve the schema validation to also check and warn against missing configurations (See issue: troessner#1734) dry-schema provide good documentation and it looks like it also provides the features we require. Structural validation where key presence can be verified separately from values. This removes ambiguity related to "presence" validation where you don't know if value is indeed nil or if a key is missing in the input hash. Changes include: - Update changelog - Add a validation section to How-To-Write-New-Detectors - Add schema specs - Update feature specs - Catch schema validation error in lib/reek/configuration/configuration_file_finder.rb, so we have access to the config file path for the displayed error message. See: troessner#1742 See: troessner#1734
d123127
to
91a9a9a
Compare
Thank you @mvz the PR is squashed and is ready to merge (if CI passes). |
Thanks @fbuys this is a great improvement! |
This PR looks big but most of the changes is rewriting the old schema file with the dry-schema syntax.
The current validator (Kwalify) seems outdated and lacks good documentation.
A recent issue showed that we could improve the schema validation to also check and warn against missing configurations (See issue: #1734)
dry-schema provides good documentation and it looks like it also provides the features we require.
Changes include:
See: #1734
We can take a difference approach for #1741 once this is accepted/merged.
This PR should close: #1742