ADR 0008 — Query Matchers: matches / notMatches via a Pluggable Engine
Status: Accepted
Context
Section titled “Context”Targeting rules and segments are a flat AND of conditions (ADR 0006), and the runtime evaluator is intentionally tiny — pure, synchronous, zero-dep (ADR 0002). That model can’t express OR / nested boolean logic in a single condition: “pro plan AND (seats > 10 OR role = admin)” needs multiple rules, and there’s no general predicate over arbitrary context shape.
Two asks pointed at the same gap: support a MongoDB-style query engine
(sift / mingo)
against the context, and/or schema-style validation (zod/valibot/arktype). The key
question was whether to overload equals/notEquals and whether OpenFeature
restricts it.
Two facts settled the design. (1) A query is data; a schema is code. A sift/mingo
filter is a plain JSON object — it serializes into the snapshot like any condition
value. A zod schema is functions — it can’t be stored, so it can only be a matcher
registered in code that a condition references by name. (2) No OpenFeature
constraint. Matching runs in-process on the live context (the same reason
ADR 0007’s comparators work); nothing crosses the
SDK boundary. The only boundary is that the stored query must be JSON.
Decision
Section titled “Decision”Add a dedicated operator pair matches / notMatches (not an overload of equals,
which has precise primitive/value-object semantics) and a pluggable, named matcher
registry mirroring the comparator registry:
condition.valueis a JSON query document;condition.matchernames the engine (defaults to"default"). The subject iscontext[attribute]when an attribute is named, else the whole context — which is what unlocks OR/nested logic and cross-attribute queries (sift expresses sub-paths in the query itself).registerMatcher(name, fn)populates a process-wide registry; amatchersoption (Map/Record/ tuples) oncreateOpenFeatureProvider/createFlagsCorelayers an instance set over it, scoped synchronously viawithMatchers.MatcherFn = (query, subject, context) => boolean.- Fail-closed, never-throws: an unregistered matcher, a non-object query, or a
matcher that throws makes both operators evaluate to
false— the rule never fires on a broken or absent matcher rather than guessing. A cleanfalsemakesnotMatchestrue. - Two matchers ship: a built-in
regex(zero-dep, nativeRegExp, always resolvable from a non-clearable built-ins map — user registrations of the same name shadow it), andsiftbehind the@xtandard/flags/match/siftsubpath withsiftas an optional peer dependency (the core stays zero-dep, same pattern asstorage/postgres). - Validation:
matches/notMatchesrequire a JSON object query (attribute optional); semantic check lives beside theinSegmentper-operator checks.
Consequences
Section titled “Consequences”- Arbitrary boolean logic in one condition — the practical answer to the “no OR in the flat-AND model” gap, complementary to segments, with no evaluator rewrite.
- The core stays zero-dep and never-throws. Query engines are opt-in:
regexrides nativeRegExp;sift/mingo/custom are registered by the host. - Schema validators fit via the same mechanism — a named matcher closing over a zod/valibot/arktype schema in code; the stored query carries only JSON params.
- Authoring is free-form JSON. The admin UI offers a JSON editor + matcher-name
field; it can’t statically validate engine-specific operators (the publishing
process may not have the matcher). Unknown/never-registered matchers simply don’t
fire at eval — predictable, but a
matchesrule is only as good as the resolver’s registrations. - Module-level + scoped registry, same trade as comparators: a synchronous
dynamic scope (
withMatchers) localizes per-instance behavior without threading a parameter through the deep evaluator call chain.