Validate arguments during entity creation
During entity creation, some argument are checked (in the yams way) and other are converted only during database insertion.
For example considering the following schema:
class Example(EntityType):
a_time = TZDatetime()
a_int = Int()
In the shell, one can create an entity with the following command:
import datetime as dt
create_entity('Example', a_time='2021-10-21', a_int='4')
create_entity('Example', a_time=dt.datetime.now(), a_int=4)
This works because there is some conversions in different module.
Conversion defined in YAMS
a_int
is converted because:
- during the copy of edited a call is made to check in cubicweb.server.edition.
- This uses the underlying check function in yams.schema but this functions also convert the values.
- the convert values uses some converters defined here. If there is no converter, then the value is used as is.
The a_int
conversion replaces the values used inside the glob_add_entity (and before any hook is called).
Conversion defined in logilab database
a_time
is converted because:
- there is no conversion in yams
- during the glob_add_entity a call is made to source.add_entity
- during add_entity, a call is made to preprocess_entity. This functions returns processed arguments
attrs
but leave the entity not modified - The preproccess_entity function use some converters to build the processed arguments. The converters are defined in logilab_database.
- the processed arguments are consumed in the sql argument but forgotten otherwise.
The correct value is inserted in the database, but the values inside entity
in add_glob_entity are still string.
This is problematic for dates for example.
The fix for this issue is the following MR in yams, but releasing yams seems complicated.
This is kinda related to !346 (merged)