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

Aperta
#367 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@harold18m ci sta già lavorando.

Dal 7/11/2026.

  • #523 di @Railly — integrata

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
68/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
typescript
Ambito
frontend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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!

Lingua principale
TypeScript
Stelle
20k
Fork
1.1k
Merge medio
1g 18h
PR unite (30g)
25

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di vercel-labs/json-render

Tutte le issue di vercel-labs/json-render

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.