Skip to content

Commit

Permalink
Add field constraints to action api models
Browse files Browse the repository at this point in the history
  • Loading branch information
daryllimyt committed Jan 10, 2025
1 parent 0af7143 commit 2296dc0
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions tracecat/workflow/actions/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from typing import Any

from pydantic import BaseModel, Field
import orjson
from pydantic import BaseModel, Field, field_validator
from pydantic_core import PydanticCustomError

from tracecat.dsl.enums import JoinStrategy
from tracecat.dsl.models import ActionRetryPolicy
from tracecat.identifiers.action import ActionID
from tracecat.identifiers.workflow import WorkflowID


class ActionControlFlow(BaseModel):
run_if: str | None = None
for_each: str | list[str] | None = None
run_if: str | None = Field(default=None, max_length=1000)
for_each: str | list[str] | None = Field(default=None, max_length=1000)
retry_policy: ActionRetryPolicy = Field(default_factory=ActionRetryPolicy)
start_delay: float = Field(
default=0.0, description="Delay before starting the action in seconds."
Expand All @@ -17,7 +21,7 @@ class ActionControlFlow(BaseModel):


class ActionRead(BaseModel):
id: str
id: ActionID
type: str
title: str
description: str
Expand All @@ -28,15 +32,15 @@ class ActionRead(BaseModel):

class ActionReadMinimal(BaseModel):
id: str
workflow_id: str
workflow_id: WorkflowID
type: str
title: str
description: str
status: str


class ActionCreate(BaseModel):
workflow_id: str
workflow_id: WorkflowID
type: str
title: str = Field(min_length=1, max_length=100)

Expand All @@ -47,3 +51,13 @@ class ActionUpdate(BaseModel):
status: str | None = None
inputs: dict[str, Any] | None = None
control_flow: ActionControlFlow | None = None

@field_validator("inputs")
def validate_inputs(cls, v: dict[str, Any]) -> dict[str, Any]:
if len(orjson.dumps(v)) >= 100:
raise PydanticCustomError(
"value_error.inputs_too_long",
"Inputs must be less than 100 characters",
{"loc": ["inputs"]},
)
return v

0 comments on commit 2296dc0

Please sign in to comment.