Coverage for gws-app/gws/plugin/model_validator/regex/__init__.py: 0%
17 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"""Regular expression validator for strings.
3Checks that the field value is a string that matches a regular expression.
4The match uses ``re.search``, so the expression can match anywhere in the
5value; add ``^`` and ``$`` anchors to match the whole value. A value that is
6not a string fails the check.
8Example::
10 fields+ {
11 name "zip"
12 type "text"
13 validators+ { type "regex" regex "^[0-9]{5}$" }
14 }
15"""
17import re
19import gws
20import gws.base.model.validator
23@gws.ext.config.modelValidator('regex')
24class Config(gws.base.model.validator.Config):
25 """Checks that a value matches a regular expression."""
27 regex: gws.Regex
28 """Regular expression, matched anywhere in the value unless anchored."""
31@gws.ext.object.modelValidator('regex')
32class Object(gws.base.model.validator.Object):
33 """Regular expression validator object."""
35 regex: str
36 """The regular expression."""
38 def configure(self):
39 self.regex = self.cfg('regex')
41 def validate(self, field, feature, mc):
42 val = feature.attributes.get(field.name)
43 if not isinstance(val, str):
44 return False
45 m = re.search(self.regex, val)
46 return m is not None