Skip to content

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

Merged
mvz merged 1 commit into
troessner:masterfrom
fbuys:fbuys/issue-1734-add-dry-schema
Nov 7, 2023
Merged

ISSUE-1742 Replace kwalify with dry-schema for schema validation#1749
mvz merged 1 commit into
troessner:masterfrom
fbuys:fbuys/issue-1734-add-dry-schema

Conversation

@fbuys

@fbuys fbuys commented Oct 30, 2023

Copy link
Copy Markdown
Contributor

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

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

@mvz

mvz commented Oct 31, 2023

Copy link
Copy Markdown
Collaborator

I'll review this evening.

@mvz mvz left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
Copy Markdown
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!

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
Copy Markdown
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
Copy Markdown
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.

@fbuys fbuys left a comment

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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!

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
Copy Markdown
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.


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

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

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

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

mvz commented Nov 3, 2023

Copy link
Copy Markdown
Collaborator

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

fbuys commented Nov 6, 2023

Copy link
Copy Markdown
Contributor Author

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
@mvz

mvz commented Nov 7, 2023

Copy link
Copy Markdown
Collaborator

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