Coverage for gws-app/gws/plugin/model_validator/not_empty/__init__.py: 94%
16 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 non-empty values.
3Fails if the field value is None or a string that is empty or consists only of
4whitespace. Fields with ``isAuto`` always pass when a feature is created,
5since their value is set by the database.
7Every field runs a default instance of this validator; a failure is reported
8only if the field is required (``isRequired``). A configured ``notEmpty``
9validator replaces the default one, e.g. to set a custom message.
11Example::
13 fields+ {
14 name "name"
15 type "text"
16 isRequired true
17 validators+ { type "notEmpty" message "Name is required" }
18 }
19"""
21import gws
22import gws.base.model.validator
25@gws.ext.config.modelValidator('notEmpty')
26class Config(gws.base.model.validator.Config):
27 """Checks that a value is not empty."""
29 pass
32@gws.ext.object.modelValidator('notEmpty')
33class Object(gws.base.model.validator.Object):
34 """Not-empty validator object."""
36 def validate(self, field, feature, mc):
37 val = feature.attributes.get(field.name)
39 if mc.op == gws.ModelOperation.create and field.isAuto:
40 return True
41 if isinstance(val, str):
42 return len(val.strip()) > 0
43 if val is not None:
44 return True
46 return False