[Feature request] Resolve directives in conditions (visible, $cond)
Maintainers usually reply within 2 days
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 ofeq,neq,gt,lt…) only resolves{ "$state" }; any other object is compared as is.resolveConditionValue(the left side) only accepts$state,$itemor$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,$itemor$index: its value isundefined, andvalidateSpecrejects 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/lteaccepting a directive, and aSingleConditionvariant whose value is a directive. - Schemas:
VisibilityConditionSchemaandVisibilityConditionStrictSchemaaccepting those forms, sovalidateSpecdoesn'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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from vercel-labs/json-render
-
React Quick Start examples fail at runtime: Renderer must be wrapped in JSONUIProviderPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
vercel-labs/json-render#443 ·
Maintainers usually reply within 2 days
-
shadcn Grid/Stack are not responsive (fixed grid-cols-N, no min-w-0)Possibly taken @sushanshakya77 claimed this 67 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
vercel-labs/json-render#355 ·
Maintainers usually reply within 2 days
-
Array path helpers accept malformed RFC 6902 indexesPossibly taken @simonyang08 claimed this 106 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
vercel-labs/json-render#326 ·
Maintainers usually reply within 2 days
-
Support zod 4.4+ — _zod.version.minor type check prevents upgradeMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
vercel-labs/json-render#297 · 2 reactions ·
Maintainers usually reply within 2 days
-
feat: Generic `Spec` type supportPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
vercel-labs/json-render#275 · 1 reaction ·
Maintainers usually reply within 2 days
All issues in vercel-labs/json-render
Similar issues
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/clickhouse-js#1293 ·
Maintainers usually reply within 1 day
-
area: middleware enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
agent/quality testing
Difficulty 1/5 Under an hour Newbie friendliness 95/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
juspay/agent-distro#203 ·
Maintainers usually reply within 1 day