[Feature request] Resolve directives in conditions (visible, $cond)

Open
#367 5 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@harold18m is already working on this.

Since Nov 7, 2026.

  • #523 by @Railly — merged

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
68/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
typescript
Domain
frontend

Research direction

Start with resolveComparisonValue and resolveConditionValue in @json-render/core, then trace the visibility condition types and VisibilityConditionSchema and VisibilityConditionStrictSchema. Check the conditions section of catalog.prompt({ directives }) for the prompt update. Done means registered directives resolve on either side of comparisons, accepted types and schemas validate them, and runtime behavior is covered for visible and $cond.

Written by the indexing model from the issue text.

Description

Hi! Thanks for json-render, directives have been really useful for us. Here's a small feature request around conditions.

Context

Directives (defineDirective) are resolved in props and action params (via resolvePropValue), but not in conditions. In @json-render/core 0.20:

  • resolveComparisonValue (the right side of eq, neq, gt, lt…) only resolves { "$state" }; any other object is compared as is.
  • resolveConditionValue (the left side) only accepts $state, $item or $index.
Use case

Comparing against a value computed at render time, with the standard directives or a custom one:

{ "visible": { "$count": { "$state": "/cart/items" }, "gt": 0 } }
{
  "visible": {
    "$state": "/product/stock",
    "lt": { "$math": "multiply", "a": { "$state": "/product/minStock" }, "b": 2 }
  }
}
{ "visible": { "$item": "dueDate", "eq": { "$today": true } } }

($today would be a custom directive that returns today's date.)

Current behavior
  • On the right side, the directive is compared as a literal object: the condition never matches, with no warning.
  • On the left side, the condition has no $state, $item or $index: its value is undefined, and validateSpec rejects it (VisibilityConditionStrictSchema).

The workaround today is to save the value to state first (a setState on some event) and compare against { "$state" }. It's more verbose for whoever writes the spec (or the model generating it), and the value goes stale until that event runs again.

Proposal

Resolve a registered directive on both sides of a comparison. The ctx already carries directives both in an element's visible (the renderer builds the full context) and in $cond (resolvePropValue):

function resolveComparisonValue(value, ctx) {
  if (typeof value === "object" && value !== null) {
    if ("$state" in value && typeof value.$state === "string") {
      return getByPath(ctx.stateModel, value.$state);
    }
    const directive = findDirective(value, ctx.directives);
    if (directive) return directive.resolve(value, ctx);
  }
  return value;
}

function resolveConditionValue(cond, ctx) {
  // … $index and $item as today
  const directive = findDirective(cond, ctx.directives);
  if (directive) return directive.resolve(cond, ctx);
  return getByPath(ctx.stateModel, cond.$state);
}

On the left side, the directive gets the object with its operator included (gt, eq…), which its resolve ignores.

To be consistent, this would also need:

  • Types: gt/gte/lt/lte accepting a directive, and a SingleCondition variant whose value is a directive.
  • Schemas: VisibilityConditionSchema and VisibilityConditionStrictSchema accepting those forms, so validateSpec doesn't flag them as invalid.
  • Prompt: a line in the conditions section of catalog.prompt({ directives }), so the model knows it can use a directive on either side.

We tested it with a local patch of @json-render/core 0.20, covering runtime and types. Happy to open a PR if this sounds good, thanks!

Dominant language
TypeScript
Stars
20k
Forks
1.1k
Avg merge
1d 18h
Merged PRs (30d)
25

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vercel-labs/json-render

All issues in vercel-labs/json-render

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.