Open lafrech opened 7 years ago
From the discussions about validation decorators priorities in marshmallow, I think I'd rather stick to a simple hook approach where we keep control on the execution order.
Also, it would be great if the validation hook was called recursively on embedded documents.
Currently, when I need validation on an EmbeddedDocument
, I must call it manually from each Document
embedding it which is cumbersome and error-prone.
AFAIU, there is no explicit way to declare schema-level validation methods.
If I want to validate a
Document
at schema leval, I create a validation method and add it to bothpre_insert
andpre_update
.We could add a
validate
hook thatDocument
s could override, likepre_insert
and all.Or maybe a decorator a la Marshmallow like
@validates_schema
to allow the user to register schemas validators. This can look nicer. It allows to register several validation methods in the same class in a nice way, and it avoids bothering with callingsuper()
when subclassing. We lose control on the order of execution but does that really matter?The fact that this validation happens at commit time is intentional: just like with
required
, you may build yourDocument
in several steps, so raising a ValidationError at__init__
(load) time would be too restrictive.I don't see any way to transfer this validation method to the auto-generated Marshmallow schema as it may use any method of the
Document
while the schema-level validators in Marshmallow only get the input data as adict
. I'm not sure how much of an issue this is in practice.What do you think?
Or is there an existing mechanism I didn't find, already?