Skip to content

About

Run valibot schemas on the ata engine: same answers, verdicts in nanoseconds.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

@ata-project/valibot

Run a valibot schema on the ata engine. The schema stays valibot, the answers always match valibot's own, and the verdicts arrive in nanoseconds.

npm install @ata-project/valibot

valibot and ata-validator are installed alongside; valibot is a peer.

import * as v from 'valibot'
import { compile } from '@ata-project/valibot'

const user = v.object({
  id: v.pipe(v.number(), v.integer(), v.minValue(1)),
  name: v.pipe(v.string(), v.minLength(1)),
})

const check = compile(user)
check.isValid(data)        // ata answers
check.safeParse(data)      // valibot-shaped result, valibot-produced value
check.parse(data)          // throws a real ValiError
check.isValidBytes(bytes)  // verdict from a Buffer or JSON string

How it stays correct

valibot ships an official JSON Schema conversion (@valibot/to-json-schema). The bridge asks it for the input side with unrepresentable features ignored, which can only make the emitted schema looser than valibot, never stricter. Then the schema is classified by walking valibot's own tree before anything runs:

Mode When Who answers
ata the conversion is exact ata alone
hybrid provably looser (transforms, checks, formats, native types) ata's rejections are final, valibot confirms acceptances
valibot valibot accepts what the conversion rejects (fallback, records, async) or a node is unknown valibot

The classification is conservative: an unrecognised node lands in valibot mode, so a new valibot feature can make the bridge slower, never wrong. compiled.engine tells you which mode you got, compiled.reasons says why.

Two valibot semantics survive nowhere in JSON Schema and are handled explicitly rather than papered over. valibot's number accepts Infinity, which JSON has no word for: schemas that constrain numbers get a scan of the rejected value, and a rejection that might be Infinity-caused is handed to valibot for the final word. And v.record runs on arrays too, so records stay valibot's entirely.

The package is differential-tested against valibot on 11,788 generated values across all three modes, including NaN and Infinity corners and the compiled modules the build entry emits, and the whole suite runs a second time with code generation blocked.

What it costs, measured

One representative API-boundary object schema (nine fields, nested arrays of objects, picklist, nullable), interleaved medians of 7 rounds on an M-series Mac, Node 25, valibot 1.5.0, ata-validator 1.36.0:

valibot safeParse this package
accept, verdict only 757 ns 20 ns
reject, verdict only 833 ns 83 ns
reject, safeParse 833 ns 83 ns
accept, safeParse 757 ns 777 ns

The last row is by design, not a gap: an accepted value's output is valibot's to make. Plain v.object strips unknown keys, defaults fill, transforms rewrite, so safeParse hands every accepted value to valibot and returns exactly what valibot returns. What this package owns is the verdict and the rejection, and a rejected safeParse builds its issues only when somebody reads them, by running valibot once at that moment.

The reject rows carry the Infinity scan described above; on schemas with no number constraints they drop to the bare engine verdict.

With code generation blocked, the way a strict CSP or a locked-down edge runtime blocks it: valibot stays at its usual speed, and the bridge falls back to ata's interpreted engine at 630 ns for accepts and 191 ns for rejects, still ahead on both.

Ahead of time, for the browser

The runtime bridge above carries a general engine that can validate any schema handed to it. In a browser that is the wrong trade, and valibot users of all people know why: a bundle is not the place for a compiler.

So compile the schema instead, at build time:

import { compileToModule } from '@ata-project/valibot/build'
import { writeFileSync } from 'node:fs'

writeFileSync('validate-product.js', compileToModule(productSchema))

The emitted module imports nothing, not valibot and not ata. It exports isValid(data) and validate(data). Measured on the four-field product schema in test.js (title, price, tags, images), each entry bundled with esbuild --bundle --minify for the browser and gzipped, valibot 1.5.0 and ata-validator 1.36.0:

what ships gz
valibot, tree-shaken for that schema 1.56 KB
this package's runtime bridge (valibot and ata's engine) 116.3 KB
the compiled module 1.40 KB

Both the compiled module and valibot's 1.56 KB include error reporting, so that is a fair pairing: about the same size, no runtime dependency at all, and a function that was compiled rather than walked.

Only schemas the classifier calls exact can be compiled. Anything valibot checks at runtime (transforms, custom checks, formats, native types) throws NotExactError rather than being quietly compiled without those checks; use the runtime bridge for those. canCompile(schema) tells you which you have before you build.

Emitting a module uses code generation, so run it at build time in Node. What it emits is plain code and runs anywhere, a strict CSP included.

The same thing from the bundler: the from option of @ata-project/unplugin (0.13.0) takes modules that export valibot schemas, converts each with @valibot/to-json-schema and writes a compiled { validate, isValidObject } beside the module, typed as InferInput<...>, so the page carries neither valibot nor a validator runtime. On a four-field event schema, esbuild minified and gzipped, valibot 1.5.0 and ata-validator 1.63.1, the page carries 1.1 KB compiled against 1.7 KB with valibot. valibot is already small, so the saving is 0.6 KB on that schema; what changes is that the page runs a compiled check and the server can import the same module. A check or transform that reads only its own parameter is carried, in the order of the pipe; a validation written after a transform is refused by export and path.

Raw bytes

isValidBytes answers from a Buffer, Uint8Array or JSON string. On an engine: 'ata' schema with no number constraints and the native engine present, the verdict comes straight off the bytes with no JSON.parse; other schemas parse first so number semantics stay exact. Bytes that are not valid JSON return false rather than throwing.

Limitations, plainly

  • Accepted values in hybrid mode and every value in valibot mode run valibot, so those paths are valibot-speed. The win is the rejection and the pure-schema case.
  • validate() reports ata's errors for the schema-representable part, which are JSON Schema errors, not valibot issues. Use safeParse when you need valibot's issue shape.
  • The classifier reads valibot's public schema tree and the peer range is pinned to valibot 1; the differential suite is the tripwire for internals moving.
  • Async schemas are not supported; safeParse is synchronous, as in valibot.

Standard Schema

The compiled object implements Standard Schema V1, so anything that accepts a standard schema runs the fast path without knowing either library.

License

MIT

About

Run valibot schemas on the ata engine: same answers, verdicts in nanoseconds.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages