Michael Granger avatar Michael Granger committed b59828d

Fix validator for literal "false" (via JSON)

Comments (0)

Files changed (2)

lib/strelka/paramvalidator.rb

 		params = stringify_keys( params )
 		@fields = deep_copy( params )
 
+		self.log.debug "Starting validation with fields: %p" % [ @fields ]
+
 		# Use the constraints list to extract all the parameters that have corresponding
 		# constraints
 		self.constraints.each do |field, constraint|
 	### the given +value+, and add the field to the appropriate field list based on the
 	### result.
 	def apply_constraint( constraint, value )
-		if value
+		if !value.nil?
 			result = constraint.apply( value, self.untaint_all? )
 
 			if !result.nil?
 	def valid
 		self.validate unless self.validated?
 
+		self.log.debug "Building valid fields hash from raw data: %p" % [ @valid ]
 		unless @parsed_params
 			@parsed_params = {}
 			for key, value in @valid
+				self.log.debug "  adding %s: %p" % [ key, value ]
 				value = [ value ] if key.to_s.end_with?( '[]' )
 				if key.to_s.include?( '[' )
 					build_deep_hash( value, @parsed_params, get_levels(key.to_s) )

spec/strelka/paramvalidator_spec.rb

 				@validator[:enabled].should be_true()
 			end
 
-			it "accepts the value 'false'" do
+			it "accepts the string 'false'" do
 				@validator.validate( 'enabled' => 'false' )
 
 				@validator.should be_okay()
 				@validator[:enabled].should be_false()
 			end
 
+			it "accepts the literal false value" do
+				@validator.validate( 'enabled' => false )
+
+				@validator.should be_okay()
+				@validator.should_not have_errors()
+
+				@validator[:enabled].should be_false()
+			end
+
 			it "accepts the value 'f'" do
 				@validator.validate( 'enabled' => 'f' )
 
 				@validator[:enabled].should be_nil()
 			end
 
+			it "includes literal false values in the hash of valid data" do
+				@validator.validate( 'enabled' => false )
+
+				@validator.valid.should include( enabled: false )
+			end
+
 		end
 
 		describe ":integer constraints" do
 				@validator[:count].should == -407
 			end
 
+			it "accepts literal integers" do
+				@validator.add( :count, :integer )
+				@validator.validate( 'count' => 118 )
+
+				@validator.should be_okay()
+				@validator.should_not have_errors()
+
+				@validator[:count].should == 118
+			end
+
 			it "rejects non-integers" do
 				@validator.add( :count, :integer )
 				@validator.validate( 'count' => '11.1' )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.