Commits

Michael Granger  committed 662737a

Consider empty string as no value for parameter validation. (fixes #8)

  • Participants
  • Parent commits 8ab80d5

Comments (0)

Files changed (2)

File lib/strelka/paramvalidator.rb

 	### the given +value+, and add the field to the appropriate field list based on the
 	### result.
 	def apply_constraint( constraint, value )
-		if !value.nil?
+		if !( value.nil? || value == '' )
 			result = constraint.apply( value, self.untaint_all? )
 
 			if !result.nil?

File spec/strelka/paramvalidator_spec.rb

 			expect( @validator[ :blorp ] ).to be_true
 		end
 
-		it "ignores identical duplicate constraints to be added twice" do
+		it "ignores identical duplicate constraints" do
 			@validator.add( :a_field, :string )
 			@validator.add( :a_field, :string )
 			expect( @validator.param_names ).to include( 'a_field' )
 			expect( @validator.missing ).to eq( ['id'] )
 		end
 
+		it "knows the names of fields that were required but empty" do
+			@validator.add( :id, :integer, :required )
+			@validator.validate( 'id' => '' )
+
+			expect( @validator ).to have_errors()
+			expect( @validator ).to_not be_okay()
+
+			expect( @validator.invalid ).to be_empty
+
+			expect( @validator.missing ).to have(1).members
+			expect( @validator.missing ).to eq( ['id'] )
+		end
+
 		it "knows the names of fields that did not meet their constraints" do
 			@validator.add( :number, :integer, :required )
 			@validator.validate( 'number' => 'rhinoceros' )
 			expect( @validator.invalid.keys ).to eq( ['number'] )
 		end
 
+		it "doesn't apply constraints for optional fields if the value is empty" do
+			@validator.add( :number, :integer, :optional )
+			@validator.validate( 'number' => '' )
+
+			expect( @validator ).to_not have_errors()
+			expect( @validator ).to be_okay()
+
+			expect( @validator.invalid ).to be_empty
+		end
+
 		it "can return a combined list of missing and invalid fields" do
 			@validator.add( :number, :integer )
 			@validator.add( :id, /^(\w{20})$/, :required )