fastify-ata
v0.2.14
Published
Fastify plugin for ata-validator — beats ajv on every valid-path benchmark. 2.7x faster validate(obj), 151x faster compilation, simdjson + multi-core.
Downloads
1,227
Maintainers
Readme
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-ataUsage
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) => {
// req.body.role === 'user' (default applied)
reply.send({ ok: true, name: req.body.name })
})
fastify.listen({ port: 3000 })All your existing JSON Schema route definitions work as-is.
Options
fastify.register(fastifyAta, {
coerceTypes: true, // convert "42" → 42 for integer fields
removeAdditional: true, // strip properties not in schema
})Standalone Mode (Pre-compiled)
Drop-in replacement for @fastify/ajv-compiler/standalone. Same API.
const StandaloneValidator = require('fastify-ata/standalone')
// Build phase (once) - compile schemas to JS files
const app = fastify({
schemaController: { compilersFactory: {
buildValidator: StandaloneValidator({
readMode: false,
storeFunction(routeOpts, code) {
fs.writeFileSync(generateFileName(routeOpts), code)
}
})
}}
})
// Read phase (every startup) - load pre-compiled, near-zero compile time
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)
// Standard Schema V1 interface
const result = v['~standard'].validate(data)
// { value: data } on success
// { issues: [{ message, path }] } on failureWorks with Fastify v5's Standard Schema support, tRPC, TanStack Form, Drizzle ORM.
What it does
- Registers a custom
validatorCompilerusing ata-validator - Applies
defaultvalues,coerceTypes,removeAdditionalduring 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
Real-world HTTP benchmark (autocannon, 10 connections, 5s)
| Payload | ata | ajv | | |---|---|---|---| | 1 user (0.1KB) | 65.7K req/sec | 65.7K req/sec | equal | | 10 users (0.9KB) | 57.2K | 55.3K | +3% | | 50 users (4.6KB) | 36.0K | 33.8K | +6% | | 100 users (9.1KB) | 24.6K | 22.6K | +9% |
Where ata really shines
| Scenario | ata | ajv | | |---|---|---|---| | Serverless cold start (50 schemas) | 7.7ms | 96ms | 12.5x faster | | ReDoS protection (catastrophic pattern) | 0.3ms | 765ms | immune | | Batch NDJSON (10K items, multi-core) | 13.4M/sec | 5.1M/sec | 2.6x faster | | validate(obj) valid (isolated) | 68M ops/sec | 8M ops/sec | 8.5x faster | | validate(obj) invalid (isolated) | 17M ops/sec | 8M ops/sec | 2.1x faster | | validateJSON(str) valid | 3.0M ops/sec | 1.9M ops/sec | 1.6x faster | | Fastify startup (500 routes) | 46ms | 77ms (standalone) | 1.7x faster | | Schema compilation | 113K ops/sec | 818 ops/sec | 138x faster |
Things only ata can do
- RE2 regex engine - linear-time guaranteed, immune to ReDoS attacks
- Multi-core parallel validation - NDJSON batch at 12.5M items/sec
- Standard Schema V1 - native support, ajv doesn't have it
- 138x faster compilation - serverless cold starts, dynamic schemas
License
MIT
