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

0 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""Model validators. 

2 

3Each subpackage implements one validator type (``gws.ext.object.modelValidator``). 

4Validators are configured in the ``validators`` list of a model field and check 

5the field value of a feature before it is created or updated. A validator returns 

6``False`` when the value is invalid; the field then adds the validator's message 

7to the feature errors. 

8 

9Every field runs a ``notEmpty`` and a ``format`` validator first; configured 

10validators of these types replace the default ones. An empty value stops the 

11validation, and is an error only if the field is required. A value that could 

12not be parsed is an error. The other validators run only after these checks 

13pass, and the first one that fails stops the validation. 

14 

15- ``date_range``: the value is a date within the given bounds. 

16- ``format``: the value could be parsed for the field type. 

17- ``not_empty``: the value is not empty. 

18- ``number_range``: the value is a number within the given bounds. 

19- ``regex``: the value is a string that matches a regular expression. 

20 

21The bounds of ``date_range`` and ``number_range`` are model values 

22(see ``gws.plugin.model_value``), so they can be static or computed. 

23 

24Example:: 

25 

26 fields+ { 

27 name "code" 

28 type "text" 

29 isRequired true 

30 validators+ { type "regex" regex "^[A-Z]{2}[0-9]+$" } 

31 } 

32 

33 fields+ { 

34 name "built" 

35 type "date" 

36 validators+ { 

37 type "dateRange" 

38 min { type "static" value "1900-01-01" } 

39 max { type "currentTimestamp" } 

40 } 

41 } 

42"""