Skip to main content

Validation Hook

The validation hook enforces event schema contracts at runtime. When an event is dispatched, the hook fetches its spec from the registry and validates all properties against the JSON Schema — before any provider receives the event.

Setup

import (
"github.com/dejanradmanovic/event-spec/hooks/validation"
"github.com/dejanradmanovic/event-spec/registry/local"
"github.com/dejanradmanovic/event-spec/spec"
)

reg, _ := local.New(local.Config{SpecsDir: "./specs"})

lookup := func(name string) (*spec.EventDef, bool) {
def, err := reg.GetEvent(ctx, namespace, name, "")
if err != nil {
return nil, false
}
return def, true
}

client := analytics.NewClient(
analytics.WithProviders(amp),
analytics.WithHooks(validation.New(lookup)),
)

Behavior

ScenarioOutcome
Event matches specPasses through to providers
Missing required propertyDropped + error logged
Wrong type (e.g. integer for string)Dropped + error logged
Unknown event name (no spec found)Configurable: pass or drop
status: deprecated eventWarning logged, event passes
status: deleted eventDropped

What gets validated

  • Required properties are present and non-null
  • Property types match the spec (string, number, integer, boolean, object, array)
  • Enum constraints — value must be one of the declared enum values
  • Pattern constraints — string matches the regex pattern
  • Minimum / maximum constraints — number is within range