You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Any way to enforce strict mode on all maps by default? #932
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:
// 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)
Activity
antonmedv commentedon Feb 19, 2026
If a map-like structure is required, use
types.Mapinstead ofmap[string]any. types.Map defines a closed set of allowed keys.If additional dynamic keys are needed,
types.Extracan be used to define a fallback type for unspecified keys.ar-sematell commentedon Feb 20, 2026
I will test it, but I don't know if a dynamically parsed json structure would default to a custom map type.
I don't control the input nor the program, but I want it to be strictly validated.
All I receive is a json parsed object and a program.
antonmedv commentedon Feb 20, 2026
You can construct typed env from you JSON document. Traverse the JSON and generate types.Map.