feat: generate user-facing write/read schema models + offline validation

Merged
DG
4mo ago
+2360-346

Summary

Adds generated, user-facing write and read schema models to the SDK plus an offline validate_schema() so a schema can be checked for correctness with no running Infrahub server. Part of the user-facing schema separation (opsmill/infrahub, INFP-234).

Key Changes

  • New generated models infrahub_sdk/schema/generated/{write,read}.py — the write model is exactly what /api/schema/load accepts (constrained fields carry their allowed values as Literal[...]; extra="forbid"); read = write + visible-but-not-settable fields.
  • validate_schema() (infrahub_sdk/schema/validate.py) — field-level offline validation with dotted error locations; also gates schema extensions.
  • Drift/presence guard test so the generated models can't silently go stale.
  • Regenerated protocols.py (pre-existing drift vs current backend schema).

Related Context

Generated from the backend's schema definitions; consumed by the backend PR (opsmill/infrahub). Depends on nothing else in this repo.

Test Plan

uv run pytest tests/unit/test_schema_offline_validation.py tests/unit/test_schema_generated_models.py

14 offline-validation cases + generated-model guard pass, pydantic-only (no server).

Notes

Draft: the backend still ships a parallel hand-written model set; full consolidation of the SDK's hand-written schema models is tracked as a follow-up.

🤖 Generated with Claude Code


Summary by cubic

Adds generated user-facing write/read schema models and an offline validator so schemas can be checked without a server. This separates user schema from internal models and satisfies INFP-234; validation covers extensions and now requires the root version.

  • New Features

    • Committed generated models in infrahub_sdk/schema/generated/{enums,write,read}.py; export InfrahubSchema{Write,Read} and enums; read adds Profile/Template; write includes extensions; extra="ignore" with use_enum_values=True.
    • Attributes and computed attributes are kind‑discriminated unions; typed parameters and choices; enums live in generated/enums.py.
    • validate_schema() checks the full document (nodes, generics, extensions) via InfrahubSchemaWrite, returns SchemaValidationResult with dotted paths; drops non‑write/unknown fields; includes a generated‑model drift guard; requires version.
  • Refactors

    • Public SDK schema classes now wrap the generated models; *SchemaAPI inherit generated read models; enums re‑exported; CoreNodeBase._schema is MainSchemaTypesAPI.
    • Read‑only kind/hash moved to generated read models; write nodes expose a non‑serializing .kind; SchemaRoot exposes extensions and validates payloads via InfrahubSchemaWrite.
    • Relationship helpers inlined on RelationshipSchemaAPI; AttributeSchema construction is lenient; removed AttributeKind.STRING; defaults match the server (relationship min_count/max_count=0; node branch="aware", generate_profile=True, generate_template=False); regenerated protocols.py; skip IPAddress kind tests pending backend support.