From bbbc0cb0cf60ece1661d2720465462a40591b368 Mon Sep 17 00:00:00 2001 From: Abdullah Alhusaini Date: Tue, 8 Aug 2023 20:10:02 +0300 Subject: [PATCH 1/2] json arrays are now validated as Array(JSON::Any) --- spec/amber/validations/params_spec.cr | 13 +++++++++++++ spec/support/helpers/validations_helper.cr | 5 +++++ src/amber/validators/params.cr | 14 ++++++++++---- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/spec/amber/validations/params_spec.cr b/spec/amber/validations/params_spec.cr index f77d32962..2e9056de4 100644 --- a/spec/amber/validations/params_spec.cr +++ b/spec/amber/validations/params_spec.cr @@ -3,6 +3,19 @@ require "../../spec_helper" module Amber::Validators describe Params do describe "#validation" do + context "JSON param" do + it "parses json array as [] of JSON::Any" do + params = json_params_builder({ x: [ 1, 2, 3 ] }.to_json) + validator = Validators::Params.new(params) + + validator.validation do + required(:x) + end + + validator.validate!["x"].should be_a Array(JSON::Any) + end + end + context "required params" do context "when missing" do it "is not valid and has 2 errors" do diff --git a/spec/support/helpers/validations_helper.cr b/spec/support/helpers/validations_helper.cr index e19011dae..b350e83c0 100644 --- a/spec/support/helpers/validations_helper.cr +++ b/spec/support/helpers/validations_helper.cr @@ -1,4 +1,9 @@ module ValidationsHelper + def json_params_builder(body = "") + request = HTTP::Request.new("POST", "/", HTTP::Headers{"Content-Type" => "application/json"}, body) + params = Amber::Router::Params.new(request) + end + def params_builder(body = "") request = HTTP::Request.new("GET", "") params = Amber::Router::Params.new(request) diff --git a/src/amber/validators/params.cr b/src/amber/validators/params.cr index 6e3d44877..9205abcee 100644 --- a/src/amber/validators/params.cr +++ b/src/amber/validators/params.cr @@ -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) | Nil getter present : Bool def initialize(field : String | Symbol, @msg : String?, @allow_blank : Bool = true) @@ -31,7 +31,12 @@ module Amber::Validators end private def call_predicate(params : Amber::Router::Params) - @value = params[@field] + begin + @value = JSON.parse(params[@field]).as_a + rescue + @value = params[@field] + end + @present = params.has_key?(@field) return true if params[@field].blank? && @allow_blank @@ -82,10 +87,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) | 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. From 4b948db030d6c85fb4977918b0088e70bd103f83 Mon Sep 17 00:00:00 2001 From: Abdullah Alhusaini Date: Tue, 8 Aug 2023 22:29:23 +0300 Subject: [PATCH 2/2] nested json objects are now represented as JSON::Any instead of String --- spec/amber/validations/params_spec.cr | 4 +++- src/amber/validators/params.cr | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/spec/amber/validations/params_spec.cr b/spec/amber/validations/params_spec.cr index 2e9056de4..a42dd9978 100644 --- a/spec/amber/validations/params_spec.cr +++ b/spec/amber/validations/params_spec.cr @@ -5,14 +5,16 @@ module Amber::Validators describe "#validation" do context "JSON param" do it "parses json array as [] of JSON::Any" do - params = json_params_builder({ x: [ 1, 2, 3 ] }.to_json) + params = json_params_builder({ x: [ 1, 2, 3 ], y: {z: [ 1, 2 ] } }.to_json) validator = Validators::Params.new(params) validator.validation do required(:x) + required("y") end validator.validate!["x"].should be_a Array(JSON::Any) + validator.validate!["y"].should be_a JSON::Any end end diff --git a/src/amber/validators/params.cr b/src/amber/validators/params.cr index 9205abcee..d0c3c40d3 100644 --- a/src/amber/validators/params.cr +++ b/src/amber/validators/params.cr @@ -6,7 +6,7 @@ module Amber::Validators class BaseRule getter predicate : (String -> Bool) getter field : String - getter value : String | Array(JSON::Any) | Nil + getter value : String | Array(JSON::Any) | JSON::Any | Nil getter present : Bool def initialize(field : String | Symbol, @msg : String?, @allow_blank : Bool = true) @@ -31,13 +31,22 @@ module Amber::Validators end private def call_predicate(params : Amber::Router::Params) + @present = params.has_key?(@field) + begin - @value = JSON.parse(params[@field]).as_a + 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 - @present = params.has_key?(@field) return true if params[@field].blank? && @allow_blank @@ -87,7 +96,7 @@ module Amber::Validators class Params getter raw_params : Amber::Router::Params getter rules = [] of BaseRule - getter params = {} of String => String | Array(JSON::Any) | Nil + getter params = {} of String => String | Array(JSON::Any) | JSON::Any | Nil getter errors = [] of Error def initialize(@raw_params)