fastify-ata
Fastify plugin for ata-validator - JSON Schema validation powered by simdjson.
Drop-in replacement for Fastify's default ajv validator. Standard Schema V1 compatible.
Install
npm install fastify-ata
Usage
const fastify = require('fastify')()
const fastifyAta = require('fastify-ata')
fastify.register(fastifyAta)
fastify.post('/user', {
schema: {
body: {
type: 'object',
properties: {
name: { type: 'string', minLength: 1 },
age: { type: 'integer', minimum: 0 },
role: { type: 'string', default: 'user' }
},
required: ['name']
}
}
}, (req, reply) => {
reply.send({ ok: true, name: req.body.name })
})
fastify.listen({ port: 3000 })
All your existing JSON Schema route definitions work as-is.
TypeScript
Write plain JSON Schema and get typed route handlers, no builder DSL. Add the AtaTypeProvider and author schemas with defineSchema:
import Fastify from 'fastify'
import fastifyAta from 'fastify-ata'
import { defineSchema } from 'ata-validator'
const app = Fastify().withTypeProvider<fastifyAta.AtaTypeProvider>()
await app.register(fastifyAta)
app.post('/user', {
schema: {
body: defineSchema({
type: 'object',
properties: { name: { type: 'string' }, age: { type: 'integer' } },
required: ['name'],
}),
},
}, (req, reply) => {
req.body.name
req.body.age
reply.send({ ok: true })
})
defineSchema preserves the schema's literal types, so request.body, request.query, request.params, and request.headers are inferred from the schema. Same idea as @fastify/type-provider-typebox, from plain JSON Schema.
ata-validator falls back to a pure-JS engine where the native addon is not available (Cloudflare Workers, browsers, Bun), so fastify-ata runs in those environments too.
Chainable authoring (TypeBox-style)
If you prefer a chainable builder over JSON Schema literals, ata-validator/t emits the same plain JSON Schema under the hood, so route schemas, the type provider, and the AOT path all keep working without an adapter. The migration from TypeBox is one import rename:
import Fastify from 'fastify'
import fastifyAta from 'fastify-ata'
import { t } from 'ata-validator/t'
const app = Fastify().withTypeProvider<fastifyAta.AtaTypeProvider>()
await app.register(fastifyAta)
const Body = t.object({
name: t.string({ minLength: 1 }),
age: t.integer({ minimum: 0 }),
email: t.optional(t.string({ format: 'email' })),
role: t.union([t.literal('admin'), t.literal('user')]),
})
app.post('/users', { schema: { body: Body } }, (req, reply) => {
req.body.name
req.body.email
req.body.role
reply.send({ ok: true })
})
Options
Defaults mirror Fastify's stock validator: coerceTypes: 'array' (path params and querystring values arrive as strings and coerce to the schema types, single-element arrays collapse for querystrings) and removeAdditional: true (properties outside the schema are stripped, not rejected). Override either to opt out:
fastify.register(fastifyAta, {
coerceTypes: false,
removeAdditional: false,
abortEarly: true,
prettyErrors: true,
})
With prettyErrors, a failed request returns a compiler-style message instead of the plain ajv text:
body must have required property 'name' [ATA7001] (did you mean `name` instead of `nme`?)
Off by default to keep the ajv-compatible message shape.
abortEarly replaces the error list with a shared stub. Good for public endpoints where only the accept/reject decision reaches the caller. On a 10-property schema the invalid path drops from roughly 15 ns/op to 3.7 ns/op.
Two ways to plug in
Plugin (encapsulated)
fastify.register(fastifyAta) sets the validator compiler in the context it is registered into. Register it on the root instance and it applies everywhere; register it inside a plugin and only that subtree uses ata, while the rest of the app keeps the default validator. Use this when you want ata for some routes and the default for others, or when you are adding ata to an existing app without touching the server construction.
Global default (full replacement)
If you want ata to be the validator for the whole server, pass the compiler factory at construction time instead. This is the same schemaController.compilersFactory.buildValidator hook that @fastify/ajv-compiler and joi-compiler use, so the default ajv validator is never built.
const Fastify = require('fastify')
const AtaCompiler = require('fastify-ata/compiler')
const app = Fastify({
schemaController: { compilersFactory: { buildValidator: AtaCompiler() } },
})
app.post('/user', {
schema: {
body: {
type: 'object',
properties: { name: { type: 'string' }, age: { type: 'integer' } },
required: ['name'],
},
},
}, (req, reply) => reply.send({ ok: true }))
The factory mirrors @fastify/ajv-compiler: it defaults to Fastify's own ajv behavior (coerceTypes: 'array', removeAdditional: true, first error only) and reads addSchema registrations for cross-schema $ref. Override through customOptions:
buildValidator: AtaCompiler()
const app = Fastify({
ajv: { customOptions: { coerceTypes: false, allErrors: true } },
schemaController: { compilersFactory: { buildValidator: AtaCompiler() } },
})
Note on memory: this replaces the ajv validator, so it is never instantiated. The ajv module itself can still be pulled into the process by Fastify's serializer (fast-json-stringify uses it to check serialization schemas), independent of which validator you choose. Replacing the validator removes ajv from the request validation path, not necessarily from the module graph.
Standalone Mode (Pre-compiled)
Drop-in replacement for @fastify/ajv-compiler/standalone. Same API.
const StandaloneValidator = require('fastify-ata/standalone')
const app = fastify({
schemaController: { compilersFactory: {
buildValidator: StandaloneValidator({
readMode: false,
storeFunction(routeOpts, code) {
fs.writeFileSync(generateFileName(routeOpts), code)
}
})
}}
})
const app = fastify({
schemaController: { compilersFactory: {
buildValidator: StandaloneValidator({
readMode: true,
restoreFunction(routeOpts) {
return require(generateFileName(routeOpts))
}
})
}}
})
Standard Schema V1
ata-validator natively implements Standard Schema V1 - the emerging standard for TypeScript-first schema libraries.
const { Validator } = require('ata-validator')
const v = new Validator(schema)
const result = v['~standard'].validate(data)
Works with Fastify v5's Standard Schema support, tRPC, TanStack Form, Drizzle ORM.
What it does
- Registers a custom
validatorCompiler using ata-validator
- Applies
default values, coerceTypes, removeAdditional during validation
- Caches compiled schemas (WeakMap) for reuse across routes
- Returns Fastify-compatible validation errors on invalid requests (400)
- Works with Fastify v4 and v5
Performance
All numbers below are reproducible on M4 Pro / Node 25 with the benchmarks in this repo and in ata-validator/benchmark. Run-to-run noise is roughly +/- 5% at these scales.
Fastify pipeline (autocannon, 10 connections, pipelining 10)
| valid (10 fields) | ~70,000 req/s | ~70,500 req/s | tied |
| invalid (10 fields) | ~51,000 req/s | ~52,500 req/s | +3% |
| invalid (abortEarly) | ~51,000 req/s | ~52,800 req/s | +3.5% |
HTTP + JSON.parse + routing dominate the pipeline, so validator choice is small on throughput. The real difference is elsewhere.
Where ata-validator moves the needle
| Serverless cold start (10 routes, first request) | 12.4 ms | 0.5 ms | 24x faster |
| Startup (200 routes) | 7.0 ms | 2.4 ms | 2.9x faster |
| Invalid validation (with abortEarly) | ~15 ns/op | 3.7 ns/op | 4x faster |
ReDoS pattern ^(a+)+$ | 765 ms | 0.3 ms | immune (RE2) |
Serverless cold start is the scenario that matters for Vercel, Cloudflare Workers, Fly.io and similar platforms. On a long-running box the gap closes, so classic servers will not see a throughput jump.
Build-time compile (optional)
For browser / edge deployments, ata ships an ata compile CLI that turns a JSON Schema into a self-contained .mjs plus TypeScript declarations.
npx ata compile schemas/user.json -o src/user.validator.mjs --name User
A 10-field schema produces:
| ata runtime bundle | 117 KB | 27 KB |
ata compile standard | 4.9 KB | 1.2 KB |
ata compile --abort-early | 1.3 KB | 0.6 KB |
Generated file has zero runtime dependency on ata-validator. isValid is emitted as a TypeScript type predicate, so consumers get narrowing out of the box.
Features worth calling out
- RE2 regex - linear-time guaranteed, immune to catastrophic backtracking
- simdjson - SIMD-accelerated JSON parsing for buffer-input paths
- Multi-core -
countValid(ndjsonBuf) validates many messages in one native call
- Standard Schema V1 - native support, works with Fastify v5, tRPC, TanStack Form, Drizzle
- Draft 2020-12 and Draft 7 - every applicable draft 2020-12 case in the official JSON Schema Test Suite passes with the native engine installed (1190/1190); 99.8% pure JS
- Fastify's own suite - 181 of 187 tests pass with ata as the default validator; the remaining six test the default validator's private extension API. See compat/COMPATIBILITY.md
License
MIT