feat: add boolean return_type in validators
This commit is contained in:
parent
8d6cf62204
commit
9acb8b71c6
2 changed files with 40 additions and 6 deletions
|
|
@ -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
|
||||
|
|
|
|||
12
tests/shorthand/00-test.yml
Normal file
12
tests/shorthand/00-test.yml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
%YAML 1.2
|
||||
---
|
||||
version: 1.1
|
||||
|
||||
# before
|
||||
first_variable:
|
||||
|
||||
second_variable: # in place
|
||||
|
||||
third_variable:
|
||||
# after
|
||||
...
|
||||
Loading…
Reference in a new issue