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

17 statements  

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

1"""Regex validator for strings. 

2 

3Validates if the string matches regex. Uses ``re.search``, 

4that is, the start anchor must be included if necessary. 

5""" 

6 

7import re 

8 

9import gws 

10import gws.base.model.validator 

11 

12gws.ext.new.modelValidator('regex') 

13 

14 

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

16 """Regular expression validator.""" 

17 

18 regex: gws.Regex 

19 """Regular expression to match against the field value.""" 

20 

21 

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

23 regex: str 

24 

25 def configure(self): 

26 self.regex = self.cfg('regex') 

27 

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

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

30 if not isinstance(val, str): 

31 return False 

32 m = re.search(self.regex, val) 

33 return m is not None