From 6737aa1123a9aaf2207ffc2ff7ce03f70d76c6f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 28 Aug 2018 17:29:29 +0200 Subject: [PATCH] Markdown validator now also updates the doc with post_internal The post_internal function does `document = validator.document`, replacing the to-be-posted document by the copy that Cerberus made (and which we cannot add keys to because it iterates over the keys and the dict size thus isn't allowed to change). I hope this doesn't break other validators who expect to be able to write to `self.document`. --- pillar/api/custom_field_validation.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pillar/api/custom_field_validation.py b/pillar/api/custom_field_validation.py index b6d9143e..5e08425d 100644 --- a/pillar/api/custom_field_validation.py +++ b/pillar/api/custom_field_validation.py @@ -21,7 +21,13 @@ class ValidateCustomFields(Validator): def validate(self, document, *args, **kwargs): # Keep a reference to the actual document, because Cerberus validates copies. self.__real_document = document - return super().validate(document, *args, **kwargs) + result = super().validate(document, *args, **kwargs) + + # Store the in-place modified document as self.document, so that Eve's post_internal + # can actually pick it up as the validated document. + self.document = document + + return result def _get_child_validator(self, *args, **kwargs): child = super()._get_child_validator(*args, **kwargs) @@ -178,14 +184,18 @@ class ValidateCustomFields(Validator): def _validator_markdown(self, field, value): """Convert MarkDown. """ + my_log = log.getChild('_validator_markdown') + # Find this field inside the original document my_subdoc = self._subdoc_in_real_document() if my_subdoc is None: self._error(field, f'unable to find sub-document for path {self.document_path}') return + my_log.debug('validating field %r with value %r', field, value) save_to = pillar.markdown.cache_field_name(field) html = pillar.markdown.markdown(value) + my_log.debug('saving result to %r in doc with id %s', save_to, id(my_subdoc)) my_subdoc[save_to] = html def _subdoc_in_real_document(self):