#932·expr

Any way to enforce strict mode on all maps by default?

Author: ar-sematellCreated Feb 19, 2026Updated Feb 20, 2026

I'm handling a dynamic env, which is represented by a map[string]any. When using expr.Env, it only enforces the strict mode to the immediate map, but not any nested maps. I want to enforce that any path or reference to this dynamic environment is accessible and valid, pretty much like a strict validation.

  • Only allow accessing existing variables and their nested attributes, erroring otherwise, like in the existing expr.Env behavior
  • Only allow accessing slice indexes that are given in the expr.Env or any nested attribute

Example:

go
// This compiles without error, but should error:
_, err := expr.Compile(`var1.attr2`,
	expr.Env(map[string]any{
		"var1": map[string]any{
			"attr1": 1,
		},
	}),
)
require.Error(t, err)