Skip to content
Sarj Code Standards

no-generic-json-parser

Parse consumed JSON fields with a bespoke declarative model instead of an open JSON parser.

Why

An open JSON domain validates syntax and value kinds without declaring required fields or their relationships; consumers must then reconstruct the contract manually.

Fix

Declare only the fields the consumer needs in a named Pydantic model and use model_validate_json or model_validate. Allow unrelated fields when the wire contract permits them. Document an exact-code exception for genuinely schema-agnostic infrastructure.

Examples

Before — flagged An open JSON mapping does not declare the response fields
tests/test_api.py
from pydantic import JsonValue, RootModel
class Response(RootModel[dict[str, JsonValue]]):
pass
After — preferred Declare just the field this consumer needs
tests/test_api.py
from pydantic import BaseModel
class Response(BaseModel):
version: int