-
Notifications
You must be signed in to change notification settings - Fork 208
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
Handle nested JSON and JSON arrays in params properly #1338
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
module ValidationsHelper | ||
def json_params_builder(body = "") | ||
request = HTTP::Request.new("POST", "/", HTTP::Headers{"Content-Type" => "application/json"}, body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is hard coded as a POST request. Does that mean that it can only be used with POST requests, or can it be used with PUT and PATCH requests too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well this is just a test, but we should be testing |
||
params = Amber::Router::Params.new(request) | ||
end | ||
|
||
def params_builder(body = "") | ||
request = HTTP::Request.new("GET", "") | ||
params = Amber::Router::Params.new(request) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ module Amber::Validators | |
class BaseRule | ||
getter predicate : (String -> Bool) | ||
getter field : String | ||
getter value : String? | ||
getter value : String | Array(JSON::Any) | JSON::Any | Nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my thought as well, so I don't think we need the extra |
||
getter present : Bool | ||
|
||
def initialize(field : String | Symbol, @msg : String?, @allow_blank : Bool = true) | ||
|
@@ -31,9 +31,23 @@ module Amber::Validators | |
end | ||
|
||
private def call_predicate(params : Amber::Router::Params) | ||
@value = params[@field] | ||
@present = params.has_key?(@field) | ||
|
||
begin | ||
a-alhusaini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value = JSON.parse(params[@field]) | ||
|
||
if value.is_a? Array(JSON::Any) | ||
@value = value | ||
elsif value.as_a? | ||
@value = value.as_a | ||
else | ||
@value = value | ||
end | ||
rescue | ||
@value = params[@field] | ||
end | ||
|
||
|
||
return true if params[@field].blank? && @allow_blank | ||
|
||
@predicate.call params[@field] unless @predicate.nil? | ||
|
@@ -82,10 +96,11 @@ module Amber::Validators | |
class Params | ||
getter raw_params : Amber::Router::Params | ||
getter rules = [] of BaseRule | ||
getter params = {} of String => String? | ||
getter params = {} of String => String | Array(JSON::Any) | JSON::Any | Nil | ||
getter errors = [] of Error | ||
|
||
def initialize(@raw_params); end | ||
def initialize(@raw_params) | ||
end | ||
|
||
# This will allow params to respond to HTTP::Params methods. | ||
# For example: [], []?, add, delete, each, fetch, etc. | ||
|
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.
Maybe returning a
JSON::PullParser
would be better here, so people could useJSON::Serializable
to decode these parameters. 🤔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.
When was that introduced in the language? I don't recall seeing this at the time I wrote this PR.
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.
@a-alhusaini it's been part of the std library for a few years
https://crystal-lang.org/api/1.10.1/JSON/PullParser.html