Implement a redundant validation of JsonApiCondition scheme (RFC)

pull/3554/head
KPrasch 2024-07-02 15:51:42 +08:00 committed by derekpierre
parent 06af880616
commit 40b7b65508
No known key found for this signature in database
2 changed files with 24 additions and 0 deletions

View File

@ -323,6 +323,15 @@ class ReturnValueTest:
value = _resolve_context_variable(self.value, **context)
return ReturnValueTest(self.comparator, value=value, index=self.index)
def as_dict(self):
result = {
"comparator": self.comparator,
"value": self.value,
}
if self.index is not None:
result["index"] = self.index
return result
class ConditionLingo(_Serializable):
VERSION = "1.0.0"

View File

@ -10,6 +10,7 @@ from nucypher.policy.conditions.base import AccessControlCondition
from nucypher.policy.conditions.exceptions import (
ConditionEvaluationFailed,
InvalidCondition,
InvalidConditionLingo,
)
from nucypher.policy.conditions.lingo import ConditionType, ReturnValueTest
from nucypher.policy.conditions.utils import CamelCaseSchema
@ -83,6 +84,20 @@ class JsonApiCondition(AccessControlCondition):
f"{self.__class__.__name__} must be instantiated with the {self.CONDITION_TYPE} type."
)
# validate inputs using marshmallow schema
data = {
"conditionType": condition_type,
"endpoint": endpoint,
"query": query,
"returnValueTest": return_value_test.as_dict(),
}
if parameters:
data["parameters"] = parameters
schema = self.Schema()
errors = schema.validate(data)
if errors:
raise InvalidConditionLingo(errors)
self.endpoint = endpoint
self.parameters = parameters
self.query = query