npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@triadjs/forms

v0.2.2

Published

Form validator generator for react-hook-form and @tanstack/form

Readme

@triadjs/forms

Generate typed form validators from a Triad router. For every endpoint with a request body, emits a validateXxx(input) function that returns a discriminated { ok: true, value } | { ok: false, errors } result. Optionally emits thin adapter wrappers for react-hook-form and @tanstack/form.

The generated runtime is self-contained — it does NOT import @triadjs/core at runtime, so your frontend bundle stays small.

Install

npm install --save-dev @triadjs/forms

If you plan to use the adapter wrappers:

npm install react-hook-form          # optional
npm install @tanstack/react-form     # optional

Usage

triad frontend generate --target forms --output ./src/generated/forms

Or via triad.config.ts:

import { defineConfig } from '@triadjs/test-runner';

export default defineConfig({
  frontend: {
    target: 'forms',
    output: './src/generated/forms',
    // reactHookForm: true,
    // tanstackForm: true,
  },
});

Output layout

src/generated/forms/
  runtime.ts          — minimal evaluator (~140 lines, self-contained)
  types.ts            — every named interface referenced by a validator
  <context>.ts        — one file per bounded context with its validators
  react-hook-form.ts  — (optional) resolver wrappers
  tanstack-form.ts    — (optional) validator wrappers
  index.ts            — barrel

Example generated validator

// Generated: library.ts
import { validateWith, type ValidationResult } from './runtime.js';
import type { CreateBook } from './types.js';

const validateCreateBookDescriptor = {
  kind: 'object',
  optional: false,
  nullable: false,
  fields: {
    title: { kind: 'string', optional: false, nullable: false },
    author: { kind: 'string', optional: false, nullable: false },
    isbn: { kind: 'string', optional: false, nullable: false },
    publishedYear: { kind: 'number', optional: false, nullable: false },
  },
} as const;

export function validateCreateBook(input: unknown): ValidationResult<CreateBook> {
  return validateWith<CreateBook>(validateCreateBookDescriptor, input);
}

Using the raw validator

import { validateCreateBook } from './generated/forms/library.js';

const result = validateCreateBook(formValues);
if (!result.ok) {
  for (const err of result.errors) {
    console.error(`${err.path}: ${err.message}`);
  }
  return;
}
await api.createBook(result.value);

Using with react-hook-form

With reactHookForm: true, the generator emits a xxxResolver() factory that plugs directly into useForm:

import { useForm } from 'react-hook-form';
import { createBookResolver } from './generated/forms/react-hook-form.js';

const form = useForm({ resolver: createBookResolver() });

The resolver converts the ValidationResult errors into react-hook-form's { [path]: { type, message } } shape automatically.

Using with @tanstack/form

With tanstackForm: true, the generator emits a xxxValidator object that plugs into useForm({ validators: {...} }):

import { createBookValidator } from './generated/forms/tanstack-form.js';

const form = useForm({
  validators: { onChange: createBookValidator.onChange },
});

Supported schema features (v1)

  • Primitive types: string, number, int32, datetime, boolean
  • enum (value-list check)
  • literal (exact-value check)
  • array (with recursive item validation)
  • model / value (recursive object validation)
  • Required vs optional (from SchemaNode.isOptional)
  • Nullable vs non-nullable (from SchemaNode.isNullable)

Not yet supported (intentionally minimal for v1):

  • min / max / length / pattern refinements
  • union of primitives
  • tuple
  • record

These can be added later without breaking the generated API — just richer descriptors + richer runtime checks.

Design notes

  • Why JSON descriptors, not JSON Schema? The descriptor is smaller, schema-library-agnostic, and easier to evolve. A JSON-Schema emitter could be added later alongside.
  • Why embed the runtime, not import @triadjs/core? Bundle size — @triadjs/core pulls in OpenAPI machinery you don't need at form- validation time. The runtime.ts template is under 150 lines.
  • Why one file per bounded context? Consistency with the other codegen packages (@triadjs/tanstack-query, @triadjs/solid-query, @triadjs/vue-query, @triadjs/svelte-query), so the output directory has a predictable shape regardless of target.