Coverage for gws-app/gws/plugin/model_validator/number_range/__init__.py: 0%
27 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
1"""Validator for number ranges.
3Checks that the field value is an ``int`` or ``float`` between ``min`` and
4``max``, both inclusive. The bounds are model values, so they can be fixed
5numbers or computed. A value that is not a number fails the check.
7Example::
9 fields+ {
10 name "floors"
11 type "integer"
12 validators+ {
13 type "numberRange"
14 min { type "static" value 1 }
15 max { type "static" value 99 }
16 }
17 }
18"""
20from typing import Optional
22import gws
23import gws.base.model.validator
26@gws.ext.config.modelValidator('numberRange')
27class Config(gws.base.model.validator.Config):
28 """Checks that a number lies within a range."""
30 min: Optional[gws.ext.config.modelValue]
31 """Smallest allowed value."""
32 max: Optional[gws.ext.config.modelValue]
33 """Largest allowed value."""
36@gws.ext.object.modelValidator('numberRange')
37class Object(gws.base.model.validator.Object):
38 """Number range validator object."""
40 minVal: Optional[gws.ModelValue]
41 """Value that computes the smallest allowed number."""
42 maxVal: Optional[gws.ModelValue]
43 """Value that computes the largest allowed number."""
45 def configure(self):
46 self.minVal = self.create_child_if_configured(gws.ext.object.modelValue, self.cfg('min'))
47 self.maxVal = self.create_child_if_configured(gws.ext.object.modelValue, self.cfg('max'))
49 def validate(self, field, feature, mc):
50 val = feature.attributes.get(field.name)
51 if not isinstance(val, (int, float)):
52 return False
54 if self.minVal:
55 v = self.minVal.compute(field, feature, mc)
56 if val < v:
57 return False
59 if self.maxVal:
60 v = self.maxVal.compute(field, feature, mc)
61 if val > v:
62 return False
64 return True