Coverage for gws-app/gws/plugin/model_validator/number_range/__init__.py: 0%

28 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 22:59 +0200

1"""Validator for number ranges.""" 

2 

3from typing import Optional 

4 

5import gws 

6import gws.base.model.validator 

7 

8gws.ext.new.modelValidator('numberRange') 

9 

10 

11class Config(gws.base.model.validator.Config): 

12 """Validator for number ranges.""" 

13 

14 min: Optional[gws.ext.config.modelValue] 

15 """Minimum value for the range.""" 

16 max: Optional[gws.ext.config.modelValue] 

17 """Maximum value for the range.""" 

18 

19 

20class Object(gws.base.model.validator.Object): 

21 minVal: Optional[gws.ModelValue] 

22 maxVal: Optional[gws.ModelValue] 

23 

24 def configure(self): 

25 self.minVal = self.create_child_if_configured(gws.ext.object.modelValue, self.cfg('min')) 

26 self.maxVal = self.create_child_if_configured(gws.ext.object.modelValue, self.cfg('max')) 

27 

28 def validate(self, field, feature, mc): 

29 val = feature.attributes.get(field.name) 

30 if not isinstance(val, (int, float)): 

31 return False 

32 

33 if self.minVal: 

34 v = self.minVal.compute(field, feature, mc) 

35 if val < v: 

36 return False 

37 

38 if self.maxVal: 

39 v = self.maxVal.compute(field, feature, mc) 

40 if val > v: 

41 return False 

42 

43 return True