From a6f056821f719d6c52a502c1f0ea050cb2c56f7d Mon Sep 17 00:00:00 2001 From: Oleg Silkin Date: Tue, 31 Mar 2020 13:59:20 -0400 Subject: [PATCH] Adds a `strict` validator which fails upon possible FK violations --- src/server/validation.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/server/validation.py b/src/server/validation.py index ef1c841..3b251ba 100644 --- a/src/server/validation.py +++ b/src/server/validation.py @@ -51,11 +51,31 @@ def claim_id_is_valid(claim_id: str) -> bool: # default to None so params can be treated as kwargs; param count becomes more manageable -def is_valid_base_comment(comment: str = None, claim_id: str = None, parent_id: str = None, **kwargs) -> bool: - return comment and body_is_valid(comment) and \ - ((claim_id and claim_id_is_valid(claim_id)) or # parentid is used in place of claimid in replies - (parent_id and comment_id_is_valid(parent_id))) \ - and is_valid_credential_input(**kwargs) +def is_valid_base_comment( + comment: str = None, + claim_id: str = None, + parent_id: str = None, + strict: bool = False, + **kwargs, +) -> bool: + try: + assert comment and body_is_valid(comment) + # strict mode assumes that the parent_id might not exist + if strict: + assert claim_id and claim_id_is_valid(claim_id) + assert parent_id is None or comment_id_is_valid(parent_id) + # non-strict removes reference restrictions + else: + assert claim_id or parent_id + if claim_id: + assert claim_id_is_valid(claim_id) + else: + assert comment_id_is_valid(parent_id) + + except AssertionError: + return False + else: + return is_valid_credential_input(**kwargs) def is_valid_credential_input(channel_id: str = None, channel_name: str = None,