-
Notifications
You must be signed in to change notification settings - Fork 14
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
Validate Params #113
Open
shirish-pampoorickal
wants to merge
12
commits into
master
Choose a base branch
from
rc_validate_params
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Validate Params #113
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3336694
Implement Controller#brainstem_validate_params! which validates the g…
a020fab
Merge remote-tracking branch 'origin/rc_support_nested_params' into r…
shirish-pampoorickal 5c55e4a
Update ChangeLog with new features for sanitizing and validating inpu…
shirish-pampoorickal c2cdcb6
Merge remote-tracking branch 'origin/rc_support_nested_fields' into r…
shirish-pampoorickal 986c427
Add capability to validate and sanitize input params based on documen…
shirish-pampoorickal fe1e4f9
Allow validating & ignoring unknown params for multi nested params
shirish-pampoorickal d4670fb
Restrict brainstem_validate_params! to only validate rather than retu…
shirish-pampoorickal 334a1b4
Remove 'brainstem_ignore_unknown_params' implementation
shirish-pampoorickal 2d3adb9
Merge remote-tracking branch 'origin/master' into rc_validate_params
shirish-pampoorickal f62bc4d
Update comments and return value for brainstem_validate_params
shirish-pampoorickal d93009b
Merge branch 'master' into rc_validate_params
shirish-pampoorickal 31ed584
Update changelog to add new validation functionality `brainstem_valid…
shirish-pampoorickal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Brainstem | ||
class MalformedParams < StandardError | ||
attr_reader :malformed_params | ||
|
||
def initialize(message = "Malformed Params sighted", malformed_params = []) | ||
@malformed_params = malformed_params | ||
super(message) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
require 'brainstem/unknown_params' | ||
require 'brainstem/malformed_params' | ||
require 'brainstem/validation_error' | ||
|
||
module Brainstem | ||
class ParamsValidator | ||
attr_reader :malformed_params, :unknown_params | ||
|
||
def self.validate!(action_name, input_params, valid_params_config) | ||
new(action_name, input_params, valid_params_config).validate! | ||
end | ||
|
||
def initialize(action_name, input_params, valid_params_config) | ||
@valid_params_config = valid_params_config | ||
@input_params = sanitize_input_params!(input_params) | ||
@action_name = action_name.to_s | ||
|
||
@unknown_params = [] | ||
@malformed_params = [] | ||
end | ||
|
||
def validate! | ||
@input_params.each do |param_key, param_value| | ||
param_data = @valid_params_config[param_key] | ||
|
||
if param_data.blank? | ||
@unknown_params << param_key | ||
next | ||
end | ||
|
||
param_config = param_data[:_config] | ||
nested_valid_params = param_data.except(:_config) | ||
|
||
if param_config[:only].present? && !param_config[:only].map(&:to_s).include?(@action_name) | ||
@unknown_params << param_key | ||
elsif param_config[:recursive].to_s == 'true' | ||
validate_nested_params(param_key, param_config, param_value, @valid_params_config) | ||
elsif parent_param?(param_data) | ||
validate_nested_params(param_key, param_config, param_value, nested_valid_params) | ||
end | ||
end | ||
|
||
raise_when_invalid? ? unknown_params_error! : true | ||
end | ||
|
||
private | ||
|
||
def raise_when_invalid? | ||
@malformed_params.present? || @unknown_params.present? | ||
end | ||
|
||
def parent_param?(param_data) | ||
param_data.except(:_config).keys.present? | ||
end | ||
|
||
def validate_nested_params(param_key, param_config, value, valid_nested_params) | ||
return value if value.nil? | ||
|
||
param_type = param_config[:type] | ||
if param_type == 'hash' | ||
validate_nested_param(param_key, param_type, value, valid_nested_params) | ||
elsif param_type == 'array' && !value.is_a?(Array) | ||
@malformed_params << param_key | ||
else | ||
value.each { |value| validate_nested_param(param_key, param_type, value, valid_nested_params) } | ||
end | ||
end | ||
|
||
def validate_nested_param(parent_param_key, parent_param_type, value, valid_nested_params) | ||
begin | ||
self.class.validate!(@action_name, value, valid_nested_params) | ||
rescue Brainstem::ValidationError => e | ||
@unknown_params << { parent_param_key => e.unknown_params } | ||
@malformed_params << { parent_param_key => e.malformed_params } | ||
end | ||
end | ||
|
||
def sanitize_input_params!(input_params) | ||
malformed_params_error! unless input_params.is_a?(Hash) && input_params.present? | ||
|
||
input_params | ||
end | ||
|
||
def malformed_params_error! | ||
raise ::Brainstem::ValidationError.new("Input params are malformed") | ||
end | ||
|
||
def unknown_params_error! | ||
raise ::Brainstem::ValidationError.new("Invalid params encountered", @unknown_params, @malformed_params) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Brainstem | ||
class UnknownParams < StandardError | ||
attr_reader :unknown_params | ||
|
||
def initialize(message = "Unidentified Params sighted", unknown_params = []) | ||
@unknown_params = unknown_params | ||
super(message) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Brainstem | ||
class ValidationError < StandardError | ||
attr_reader :unknown_params, :malformed_params | ||
|
||
def initialize(message = "Invalid params sighted", unknown_params = [], malformed_params = []) | ||
@unknown_params = unknown_params | ||
@malformed_params = malformed_params | ||
|
||
super(message) | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we
to_h
params at some point before this? Otherwise this won't work in Rails 5, sinceActionController::Parameters
no longer descend fromHash
.EDIT: It looks we call
with_indifferent_access
on them above. I don't have an environment setup to test that on Rails 5 right now, but I'm not sure ifActionController::Parameters
supports that method in 5.