INNER CODE UNIT · Python

_clean_json_schema

phillipdupuis/pydantic-to-typescript · pydantic2ts/cli/script.py:161

def _clean_json_schema(schema: Dict[str, Any], model: Any = None) -> None:
    """
    Clean up the resulting JSON schemas via the following steps:

    1) Get rid of descriptions that are auto-generated and just add noise:
        - "An enumeration." for Enums
        - `inspect.getdoc(str)` for Literal types
    2) Remove titles from JSON schema properties.
       If we don't do this, each property will have its own interface in the
       resulting typescript file (which is a LOT of unnecessary noise).
    3) If it's a V1 model, ensure that nullability is properly represented.
       https://github.com/pydantic/pydantic/issues/1270
    """
    description = schema.get("description")

    if "enum" in schema and description == _USELESS_ENUM_DESCRIPTION:
        del schema["description"]
    elif description == _USELESS_STR_DESCRIPTION:

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…