Skip to content

Ability to parse JSON with arbitrary keys #19

Description

@jaredculp

Is it possible to enforce the presence of certain keys but ignore all others? My use case is parsing JSON payloads from an event bus where I want to ignore fields that may be present but that I don't care to include in the TypedDict.

This functionality would be similar to json-schema's additionalProperties: true keyword.

For example:

from typing import TypedDict, NotRequired
from trycast import trycast

class MyType(TypedDict):
    a: str
    b: NotRequired[str | None]

# currently fails validation because "c" is in the dict
trycast(MyType, {"a": "foo", "b": "bar", "c": "baz"})

Metadata

Metadata

Assignees

Labels

enhancementEnhancement to existing feature

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions