You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can use Pydantic's `Field(discriminator="...")` with union types to create discriminated unions (also known as tagged unions). This allows the Event Handler to automatically determine which model to use based on a discriminator field in the request body.
1.`Field(discriminator="action")` tells Pydantic to use the `action` field to determine which model to instantiate
440
+
2.`Body()` annotation tells the Event Handler to parse the request body using the discriminated union
441
+
431
442
#### Validating responses
432
443
433
444
You can use `response_validation_error_http_code` to set a custom HTTP code for failed response validation. When this field is set, we will raise a `ResponseValidationError` instead of a `RequestValidationError`.
@@ -568,23 +579,6 @@ You can use the `Form` type to tell the Event Handler that a parameter expects f
!!! info "You must set `enable_validation=True` to use discriminated unions via type annotation."
574
-
575
-
You can use Pydantic's `Field(discriminator="...")` with union types to create discriminated unions (also known as tagged unions). This allows the Event Handler to automatically determine which model to use based on a discriminator field in the request body.
576
-
577
-
In the following example, we define two action types (`FooAction` and `BarAction`) that share a common discriminator field `action`. The Event Handler will automatically parse the request body and instantiate the correct model based on the `action` field value:
1.`Field(discriminator="action")` tells Pydantic to use the `action` field to determine which model to instantiate
584
-
2.`Body()` annotation tells the Event Handler to parse the request body using the discriminated union
585
-
586
-
When you send a request with `{"action": "foo", "foo_data": "example"}`, the Event Handler will automatically create a `FooAction` instance. Similarly, `{"action": "bar", "bar_data": 42}` will create a `BarAction` instance.
587
-
588
582
#### Supported types for response serialization
589
583
590
584
With data validation enabled, we natively support serializing the following data types to JSON:
0 commit comments