mjdi

Search documentation

Search the mjdi v0.10.0 documentation

mjdi docsv0.10.0latest

Runtime validation (P1-06 / P1-07)

Compile-time graph validation remains the primary validation

Source: mjdi/docs/runtime-validation.md

Compile-time graph validation remains the primary validation mechanism (missing providers, cycles, lifetime conflicts — see Compile-time validation). Runtime validation is the narrow, deliberate complement: the checks that depend on runtime values, run before any application work begins, and never construct a service or execute a factory.

The value-provider inventory (P1-06)

Every value provider records its kind at compile time and receives its value at runtime (set_value). The inventory surfaces all of them with their status, in registration order:

var app = Container[type_of(reg)]()

print(app.value_inventory())
#   Endpoint: MISSING
#   Retries: MISSING

app.set_value[Endpoint](Endpoint("db://prod"))
print(app.missing_value_count())   # 1
print(app.missing_values())
# missing runtime values (1):
#   Retries: MISSING

missing_values() returns the empty string once every value is supplied. missing_value_count() returns just the number. All three inspect slots only — no service is constructed and no factory runs (proved by the cache-statistics counters in tests/test_runtime_validation.mojo).

validate_runtime() (P1-07)

var verdict = app.validate_runtime()
if not verdict.valid:
    print(verdict.report)
    return 1

validate_runtime() returns a structured RuntimeValidation:

  • valid — true exactly when problem_count is zero,
  • problem_count — one problem per missing value, plus one if a resolution is currently in progress,
  • report — the human-readable problem list.

Verified conditions (all inspectable without construction):

  1. every registered value provider has its runtime value set — missing values are reported with their type names;
  2. no resolution is in progress — calling validate_runtime from inside a factory or a post_construct hook is invalid startup state, and the check reports it.

Deliberately not checked: anything requiring service construction or factory execution. The graph itself is guaranteed by compile-time validation; runtime validation is about configuration readiness, not graph correctness.

Scopes mirror the same surface (scope.value_inventory(), scope.missing_values(), scope.validate_runtime()).

The startup pattern

The composition-root templates use the check before any application work:

var app = Container[type_of(app_registry)]()
app.set_value[AppConfig](AppConfig("standard", 2))
var verdict = app.validate_runtime()
if not verdict.valid:
    print(verdict.report)
    raise Error("startup validation failed")

Calling it before supplying values is the useful order: the report tells you every type the application still needs.

Why missing values are not a compile-time check

The registry records only that T is value-provided; the value itself arrives per container instance at runtime, so "is it set?" is necessarily a runtime question. Resolution of an unset value provider fails at runtime with MJDI-R002; validate_runtime() surfaces every such gap up front, with type names, before the first resolution attempt.

The test helpers

mjdi.testing pins these behaviors: assert_missing_values, assert_value_inventory, assert_validate_runtime (and assert_resolve_fails_with for the deferred MJDI-R002 path). See Testing.