-
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from ok32/validation
Add optional config validation (using dry-validation schema)
- Loading branch information
Showing
8 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Config | ||
module Validation | ||
def validate! | ||
v_res = Config.schema.(self.to_hash) | ||
|
||
unless v_res.success? | ||
raise ValidationError.new("Config validation failed:\n\n#{format_errors(v_res)}") | ||
end | ||
end | ||
|
||
def format_errors(v_res) | ||
flatten_hash(v_res.messages).map do |field, msgs| | ||
"#{' ' * 2}#{field}: #{msgs.join('; ')}" | ||
end.join('\n') | ||
end | ||
|
||
def flatten_hash(h, acc={}, pref=[]) | ||
h.inject(acc) do |a, (k,v)| | ||
if v.is_a?(Hash) | ||
flatten_hash(v, acc, pref + [k]) | ||
else | ||
acc[(pref + [k]).join('.')] = v | ||
acc | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
youtube: | ||
api_key: ADSEF453 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters