From 9acb8b71c6c04eca72e077838008194235ddca14 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 21 Nov 2025 08:27:22 +0100 Subject: [PATCH] feat: add boolean return_type in validators --- src/rougail/convert/object_model.py | 34 ++++++++++++++++++++++++----- tests/shorthand/00-test.yml | 12 ++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 tests/shorthand/00-test.yml diff --git a/src/rougail/convert/object_model.py b/src/rougail/convert/object_model.py index 399944246..8277a0121 100644 --- a/src/rougail/convert/object_model.py +++ b/src/rougail/convert/object_model.py @@ -35,7 +35,7 @@ from ..utils import ( PROPERTY_ATTRIBUTE, ) from ..i18n import _ -from ..error import DictConsistencyError, VariableCalculationDependencyError +from ..error import DictConsistencyError, VariableCalculationDependencyError, RougailWarning from ..tiramisu import CONVERT_OPTION, RENAME_TYPE, display_xmlfiles, convert_boolean BASETYPE = Union[StrictBool, StrictInt, StrictFloat, StrictStr, None] @@ -311,9 +311,9 @@ class JinjaCalculation(Calculation): idx += 1 objectspace.jinja[jinja_path] = self.jinja if return_type in RENAME_TYPE: - warning = f'the variable "{ self.path }" has a depreciated return_type "{return_type}", please use "{RENAME_TYPE[return_type]}" instead in {display_xmlfiles(variable.xmlfiles)}' + warning = _('the variable "{0}" has a depreciated return_type "{1}", please use "{2}" instead in {3}') warn( - warning, + warning.format(self.path, return_type, RENAME_TYPE[return_type], display_xmlfiles(self.xmlfiles)), DeprecationWarning, stacklevel=2, ) @@ -393,13 +393,35 @@ class JinjaCalculation(Calculation): objectspace, ) elif self.attribute_name == "validators": - if self.return_type: - raise Exception("pfff") + return_type = self.return_type + if return_type is None: + return_type = "string" + if return_type not in ["string", "boolean"]: + if self.ori_path is None: + path = self.path + else: + path = self.ori_path + msg = _( + 'variable "{0}" has a calculating "{1}" with an invalid return_type, should be boolean or string, not "{2}"' + ).format(path, self.attribute_name, return_type) + raise DictConsistencyError(msg, 81, self.xmlfiles) + if return_type == 'boolean': + description = self.description + if description is None: + warning = _('the variable "{0}" has a return_type "{1}", for attribute "{2}" but has not description in {3}') + warn( + warning.format(self.path, return_type, self.attribute_name, display_xmlfiles(self.xmlfiles)), + RougailWarning, + ) + self.description = _('value is invalid') + else: + description = None return self._jinja_to_function( "valid_with_jinja", - "string", + return_type, False, objectspace, + params={'description': description} ) elif self.attribute_name in PROPERTY_ATTRIBUTE: return_type = self.return_type diff --git a/tests/shorthand/00-test.yml b/tests/shorthand/00-test.yml new file mode 100644 index 000000000..4aec49b22 --- /dev/null +++ b/tests/shorthand/00-test.yml @@ -0,0 +1,12 @@ +%YAML 1.2 +--- +version: 1.1 + +# before +first_variable: + +second_variable: # in place + +third_variable: +# after +...