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

ISSUE-1742 Replace kwalify with dry-schema for schema validation #1749

Merged
merged 1 commit into from Nov 7, 2023

Conversation

fbuys
Copy link
Contributor

@fbuys fbuys commented Oct 30, 2023

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.

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

See: #1734

We can take a difference approach for #1741 once this is accepted/merged.

This PR should close: #1742

Copy link
Contributor Author

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

Choose a reason for hiding this comment

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

Handle unparsable config files

@fbuys fbuys changed the title ISSUE-1734 Replace kwalify with dry-schema for schema validation ISSUE-1742 Replace kwalify with dry-schema for schema validation Oct 30, 2023
@fbuys fbuys force-pushed the fbuys/issue-1734-add-dry-schema branch from ee4f69d to 15478a9 Compare October 30, 2023 23:03
@troessner
Copy link
Owner

Wow, this looks great! Approved from my side! Wdyt @mvz ?

@mvz
Copy link
Collaborator

mvz commented Oct 31, 2023

I'll review this evening.

Copy link
Collaborator

@mvz mvz left a 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:

optional(:enabled).filled(:bool)
optional(:exclude).array(:string)
end
optional(:DataClump).filled(:hash) do
Copy link
Collaborator

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.

Copy link
Contributor Author

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) }
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

@fbuys fbuys left a 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.

optional(:enabled).filled(:bool)
optional(:exclude).array(:string)
end
optional(:DataClump).filled(:hash) do
Copy link
Contributor Author

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

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

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.

Copy link
Collaborator

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

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

@fbuys fbuys requested a review from mvz October 31, 2023 22:40
@fbuys fbuys force-pushed the fbuys/issue-1734-add-dry-schema branch from 85ad41b to d123127 Compare October 31, 2023 22:42
@@ -58,11 +58,11 @@ def load_from_file(path)

begin
configuration = YAML.load_file(path) || {}
SchemaValidator.new(configuration).validate
Copy link
Collaborator

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 👍

@mvz
Copy link
Collaborator

mvz commented Nov 3, 2023

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
@fbuys fbuys force-pushed the fbuys/issue-1734-add-dry-schema branch from d123127 to 91a9a9a Compare November 6, 2023 11:48
@fbuys
Copy link
Contributor Author

fbuys commented Nov 6, 2023

Just waiting for CI, then it's ready to squash and then merge 🎉

Thank you @mvz the PR is squashed and is ready to merge (if CI passes).

@mvz mvz merged commit 0cf1d6f into troessner:master Nov 7, 2023
6 checks passed
@mvz
Copy link
Collaborator

mvz commented Nov 7, 2023

Thanks @fbuys this is a great improvement!

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.

Consider replacing the yaml schema validator
3 participants