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

16 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +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. (added in 8.1)""" 

17 regex: gws.Regex 

18 

19 

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

21 regex: str 

22 

23 def configure(self): 

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

25 

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

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

28 if not isinstance(val, str): 

29 return False 

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

31 return m is not None