@@ -2190,3 +2190,39 @@ def test_required_complex_key_value_validation():
21902190 assert "expected str" in error_msg
21912191
21922192
2193+ def test_complex_required_keys_with_specific_value_validation ():
2194+ """Test complex required keys combined with specific value validation for brightness range."""
2195+ schema = Schema ({
2196+ Required (Any ('color' , 'temperature' , 'brightness' )): str ,
2197+ 'brightness' : All (Coerce (int ), Range (min = 0 , max = 100 )), # Additional validation for brightness specifically
2198+ 'device_id' : str
2199+ })
2200+
2201+ # Valid - color provided, no brightness validation needed
2202+ result = schema ({'color' : 'red' , 'device_id' : 'light1' })
2203+ assert result == {'color' : 'red' , 'device_id' : 'light1' }
2204+
2205+ # Valid - temperature provided, no brightness validation needed
2206+ result = schema ({'temperature' : '3000K' , 'device_id' : 'light1' })
2207+ assert result == {'temperature' : '3000K' , 'device_id' : 'light1' }
2208+
2209+ # Valid - brightness provided and within range
2210+ result = schema ({'brightness' : '50' , 'device_id' : 'light1' })
2211+ assert result == {'brightness' : 50 , 'device_id' : 'light1' }
2212+
2213+ # Invalid - brightness provided but out of range (255 > 100)
2214+ # Should NOT get "required field missing" error, but should get range error
2215+ with pytest .raises (MultipleInvalid ) as exc_info :
2216+ schema ({'brightness' : '255' , 'device_id' : 'light1' })
2217+
2218+ # Verify it's a range error, not a missing required field error
2219+ error_msg = str (exc_info .value )
2220+ assert "required" not in error_msg .lower () # No "required field missing" error
2221+ assert "value must be at most 100" in error_msg # Range validation error
2222+
2223+ # Invalid - no lighting attributes provided
2224+ with pytest .raises (MultipleInvalid ) as exc_info :
2225+ schema ({'device_id' : 'light1' })
2226+ assert "at least one of ['color', 'temperature', 'brightness'] is required" in str (exc_info .value )
2227+
2228+
0 commit comments