Skip to content

feat: add custom directives API and @json-render/directives package#279

Merged
ctate merged 10 commits intomainfrom
ctate/directives
May 7, 2026
Merged

feat: add custom directives API and @json-render/directives package#279
ctate merged 10 commits intomainfrom
ctate/directives

Conversation

@ctate
Copy link
Copy Markdown
Collaborator

@ctate ctate commented Apr 28, 2026

Summary

  • Adds a defineDirective API to @json-render/core that lets users register custom $-prefixed dynamic values with Zod schemas, resolver functions, and AI prompt instructions — extending the spec language from userland without modifying core.
  • Wires directive support through all four renderers (React, Vue, Svelte, Solid) via a new directives prop on providers and createRenderer.
  • Ships a new @json-render/directives package with 8 pre-built directives: $format, $math, $concat, $count, $truncate, $pluralize, $join, and createI18nDirective (factory for $t).

Renderer changes

Each renderer (React, Vue, Svelte, Solid) now:

  1. Accepts an optional directives prop (array of DirectiveDefinition)
  2. Converts it to a DirectiveRegistry (Map)
  3. Passes it through context into fullCtx.directives

@json-render/directives package

Directive Description
$format Locale-aware formatting (date, currency, number, percent) via Intl
$math Arithmetic (add, subtract, multiply, divide, mod, min, max, round, floor, ceil, abs)
$concat String concatenation from multiple dynamic values
$count Array/string length
$truncate Text truncation with configurable length and suffix
$pluralize Singular/plural/zero forms based on count
$join Join array elements with a separator
createI18nDirective Factory for $t translation directive with {{param}} interpolation

Directives compose naturally — each resolver calls resolvePropValue on its inputs, so $format can wrap $math which reads from $state.

Add a `defineDirective` API that lets users register custom `$`-prefixed
dynamic values with schemas, resolvers, and prompt instructions — extending
the spec language without forking core.
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Apr 28, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
json-render Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-chat-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-dashboard-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-game-engine-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-image-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-no-ai Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-react-email-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-react-pdf-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-react-three-fiber-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-remotion-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-solid-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-svelte-chat-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-svelte-demo Ready Ready Preview, Comment May 7, 2026 3:26am
json-render-vue Ready Ready Preview, Comment May 7, 2026 3:26am

…ect keys

Flip the loop from O(object-keys) to O(registry-size) by iterating
the directive registry and checking `key in value` rather than scanning
all object keys with Object.keys() and startsWith("$").
… coercion

$format relative dates now support future timestamps ("2h from now")
and return "just now" for zero diff. $math emits a console.warn in
dev mode when a non-numeric value is silently coerced to 0.
The directives package doesn't include @types/node, so referencing
process.env fails during tsup's DTS generation. The console.warn is
unconditional now — it only fires on actual misuse (non-numeric input)
so the cost is negligible.
ctate added 4 commits May 6, 2026 22:01
… in prompt, add docs

- Rename `prompt` to `description` on DirectiveDefinition — a short
  behavioral label rather than the full AI prompt blob
- Auto-generate directive schema signatures in the system prompt using
  formatZodType, so the AI always sees every field, type, and optionality
- Add docs: guide page, API reference page, nav/title entries, docs-chat
Export a pre-assembled standardDirectives array (all 7 non-factory
directives) for convenience. Add a composition hint to the generated
AI prompt so agents know directives can nest inside each other.
- Add skills/directives/SKILL.md with full directive API reference
- Add @json-render/directives row to root README packages table
- Add "directives" to the Available skills list in docs-chat prompt
Resolvers are already defensive (coercion, fallbacks, switch defaults),
so runtime validation on every render adds overhead without safety.
The schema remains used for prompt generation and TypeScript inference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant