From aef9d45c6c7917843089b06dfefc061ea0ce622c Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Tue, 1 Feb 2022 18:38:26 +0100 Subject: [PATCH] feat(api): schema restrictions for funnels, saved search and search --- api/schemas.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/api/schemas.py b/api/schemas.py index 11d882e79..af43400bd 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -470,7 +470,7 @@ class _SessionSearchEventRaw(__MixedSearchFilter): value: Union[str, List[str]] = Field(...) type: Union[EventType, PerformanceEventType] = Field(...) operator: SearchEventOperator = Field(...) - source: Optional[Union[ErrorSource,List[Union[int, str]]]] = Field(default=ErrorSource.js_exception) + source: Optional[Union[ErrorSource, List[Union[int, str]]]] = Field(default=ErrorSource.js_exception) sourceOperator: Optional[MathOperator] = Field(None) @root_validator @@ -479,14 +479,16 @@ class _SessionSearchEventRaw(__MixedSearchFilter): if values.get("type") == PerformanceEventType.fetch_failed: return values assert values.get("source") is not None, "source should not be null for PerformanceEventType" - assert values.get("sourceOperator") is not None \ - , "sourceOperator should not be null for PerformanceEventType" + assert isinstance(values["source"], list) and len(values["source"]) > 0, \ + "source should not be empty for PerformanceEventType" + assert values.get("sourceOperator") is not None, \ + "sourceOperator should not be null for PerformanceEventType" if values["type"] == PerformanceEventType.time_between_events: assert len(values.get("value", [])) == 2, \ f"must provide 2 Events as value for {PerformanceEventType.time_between_events}" assert isinstance(values["value"][0], _SessionSearchEventRaw) \ - and isinstance(values["value"][1], _SessionSearchEventRaw) \ - , f"event should be of type _SessionSearchEventRaw for {PerformanceEventType.time_between_events}" + and isinstance(values["value"][1], _SessionSearchEventRaw), \ + f"event should be of type _SessionSearchEventRaw for {PerformanceEventType.time_between_events}" else: for c in values["source"]: assert isinstance(c, int), f"source value should be of type int for {values.get('type')}" @@ -577,6 +579,12 @@ class FunnelSearchPayloadSchema(FlatSessionsSearchPayloadSchema): range_value: Optional[str] = Field(None) sort: Optional[str] = Field(None) order: Optional[str] = Field(None) + events_order: Optional[SearchEventOrder] = Field(default=SearchEventOrder._then, const=True) + + @root_validator(pre=True) + def enforce_default_values(cls, values): + values["eventsOrder"] = SearchEventOrder._then + return values class Config: alias_generator = attribute_to_camel_case @@ -686,4 +694,4 @@ class UpdateCustomMetricsSchema(CreateCustomMetricsSchema): class SavedSearchSchema(FunnelSchema): - pass + filter: FlatSessionsSearchPayloadSchema = Field([])