fastify-ata 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mert Can Altin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # fastify-ata
2
+
3
+ Fastify plugin for [ata-validator](https://ata-validator.com) — JSON Schema validation powered by simdjson.
4
+
5
+ Replaces the default ajv validator compiler with ata-validator for **120x faster schema compilation**.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install fastify-ata
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```js
16
+ const fastify = require('fastify')()
17
+ const fastifyAta = require('fastify-ata')
18
+
19
+ fastify.register(fastifyAta)
20
+
21
+ fastify.post('/user', {
22
+ schema: {
23
+ body: {
24
+ type: 'object',
25
+ properties: {
26
+ name: { type: 'string', minLength: 1 },
27
+ age: { type: 'integer', minimum: 0 }
28
+ },
29
+ required: ['name']
30
+ }
31
+ }
32
+ }, (req, reply) => {
33
+ reply.send({ ok: true, name: req.body.name })
34
+ })
35
+
36
+ fastify.listen({ port: 3000 })
37
+ ```
38
+
39
+ That's it. All your existing JSON Schema route definitions work as-is.
40
+
41
+ ## What it does
42
+
43
+ - Registers a custom `validatorCompiler` using ata-validator
44
+ - Caches compiled schemas for reuse across routes
45
+ - Returns Fastify-compatible validation errors on invalid requests (400)
46
+ - Works with Fastify v4 and v5
47
+
48
+ ## Why
49
+
50
+ | | ata-validator | ajv |
51
+ |---|---|---|
52
+ | Schema compilation | **120x faster** | baseline |
53
+ | Engine | simdjson + RE2 + codegen | JS |
54
+ | Spec compliance | 98.6% Draft 2020-12 | ~100% |
55
+ | Standard Schema V1 | Yes | No |
56
+
57
+ ## License
58
+
59
+ MIT
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { FastifyPluginCallback } from 'fastify'
2
+
3
+ declare const fastifyAta: FastifyPluginCallback<{}>
4
+
5
+ export = fastifyAta
package/index.js ADDED
@@ -0,0 +1,39 @@
1
+ 'use strict'
2
+
3
+ const fp = require('fastify-plugin')
4
+ const { Validator } = require('ata-validator')
5
+
6
+ function fastifyAta(fastify, opts, done) {
7
+ const cache = new WeakMap()
8
+
9
+ fastify.setValidatorCompiler(({ schema }) => {
10
+ let validator = cache.get(schema)
11
+ if (!validator) {
12
+ validator = new Validator(schema)
13
+ cache.set(schema, validator)
14
+ }
15
+ return (data) => {
16
+ const result = validator.validate(data)
17
+ if (result.valid) {
18
+ return { value: data }
19
+ }
20
+ const err = new Error(result.errors.map(e => e.message).join(', '))
21
+ err.statusCode = 400
22
+ err.validation = result.errors.map(e => ({
23
+ message: e.message,
24
+ instancePath: e.path || '',
25
+ schemaPath: '',
26
+ keyword: '',
27
+ params: {},
28
+ }))
29
+ return { error: err }
30
+ }
31
+ })
32
+
33
+ done()
34
+ }
35
+
36
+ module.exports = fp(fastifyAta, {
37
+ fastify: '>=4.0.0',
38
+ name: 'fastify-ata',
39
+ })
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "fastify-ata",
3
+ "version": "0.1.0",
4
+ "description": "Fastify plugin for ata-validator — JSON Schema validation powered by simdjson",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "scripts": {
8
+ "test": "node test.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/mertcanaltin/fastify-ata.git"
13
+ },
14
+ "keywords": [
15
+ "fastify",
16
+ "json-schema",
17
+ "validator",
18
+ "simdjson",
19
+ "ata-validator",
20
+ "standard-schema"
21
+ ],
22
+ "author": "Mert Can Altin <mertcanaltin01@gmail.com>",
23
+ "license": "MIT",
24
+ "bugs": {
25
+ "url": "https://github.com/mertcanaltin/fastify-ata/issues"
26
+ },
27
+ "homepage": "https://github.com/mertcanaltin/fastify-ata#readme",
28
+ "dependencies": {
29
+ "ata-validator": "^0.2.0",
30
+ "fastify-plugin": "^5.1.0"
31
+ },
32
+ "peerDependencies": {
33
+ "fastify": ">=4.0.0"
34
+ },
35
+ "devDependencies": {
36
+ "fastify": "^5.8.4"
37
+ }
38
+ }
package/test.js ADDED
@@ -0,0 +1,113 @@
1
+ 'use strict'
2
+
3
+ const fastify = require('fastify')
4
+ const fastifyAta = require('./index')
5
+
6
+ let pass = 0
7
+ let fail = 0
8
+
9
+ function assert(cond, msg) {
10
+ if (cond) { pass++; console.log(` PASS ${msg}`) }
11
+ else { fail++; console.log(` FAIL ${msg}`) }
12
+ }
13
+
14
+ async function run() {
15
+ console.log('\nfastify-ata Tests\n')
16
+
17
+ // 1. Plugin registers without error
18
+ const app = fastify()
19
+ await app.register(fastifyAta)
20
+ assert(true, 'plugin registers')
21
+
22
+ // 2. Route with schema — valid request
23
+ app.post('/user', {
24
+ schema: {
25
+ body: {
26
+ type: 'object',
27
+ properties: {
28
+ name: { type: 'string', minLength: 1 },
29
+ age: { type: 'integer', minimum: 0 },
30
+ },
31
+ required: ['name'],
32
+ },
33
+ },
34
+ }, (req, reply) => {
35
+ reply.send({ ok: true, name: req.body.name })
36
+ })
37
+
38
+ await app.ready()
39
+ assert(true, 'app.ready() with schema route')
40
+
41
+ // 3. Valid request
42
+ const r1 = await app.inject({
43
+ method: 'POST',
44
+ url: '/user',
45
+ payload: { name: 'Mert', age: 26 },
46
+ })
47
+ assert(r1.statusCode === 200, `valid request returns 200 (got ${r1.statusCode})`)
48
+ assert(JSON.parse(r1.payload).ok === true, 'valid request body correct')
49
+
50
+ // 4. Invalid request — missing required
51
+ const r2 = await app.inject({
52
+ method: 'POST',
53
+ url: '/user',
54
+ payload: { age: 26 },
55
+ })
56
+ assert(r2.statusCode === 400, `missing required returns 400 (got ${r2.statusCode})`)
57
+
58
+ // 5. Invalid request — wrong type
59
+ const r3 = await app.inject({
60
+ method: 'POST',
61
+ url: '/user',
62
+ payload: { name: 123 },
63
+ })
64
+ assert(r3.statusCode === 400, `wrong type returns 400 (got ${r3.statusCode})`)
65
+
66
+ // 6. Multiple routes with different schemas
67
+ const app2 = fastify()
68
+ await app2.register(fastifyAta)
69
+
70
+ app2.post('/a', {
71
+ schema: { body: { type: 'object', properties: { x: { type: 'integer' } }, required: ['x'] } },
72
+ }, (req, reply) => reply.send({ route: 'a' }))
73
+
74
+ app2.post('/b', {
75
+ schema: { body: { type: 'object', properties: { y: { type: 'string' } }, required: ['y'] } },
76
+ }, (req, reply) => reply.send({ route: 'b' }))
77
+
78
+ await app2.ready()
79
+
80
+ const ra = await app2.inject({ method: 'POST', url: '/a', payload: { x: 1 } })
81
+ const rb = await app2.inject({ method: 'POST', url: '/b', payload: { y: 'hello' } })
82
+ assert(ra.statusCode === 200, 'multi-route: /a valid')
83
+ assert(rb.statusCode === 200, 'multi-route: /b valid')
84
+
85
+ // 7. Schema caching — same schema reuses validator
86
+ const app3 = fastify()
87
+ await app3.register(fastifyAta)
88
+
89
+ const sharedSchema = { type: 'object', properties: { id: { type: 'integer' } }, required: ['id'] }
90
+ app3.post('/c', { schema: { body: sharedSchema } }, (req, reply) => reply.send({ ok: true }))
91
+ app3.post('/d', { schema: { body: sharedSchema } }, (req, reply) => reply.send({ ok: true }))
92
+
93
+ await app3.ready()
94
+ const rc = await app3.inject({ method: 'POST', url: '/c', payload: { id: 1 } })
95
+ const rd = await app3.inject({ method: 'POST', url: '/d', payload: { id: 2 } })
96
+ assert(rc.statusCode === 200 && rd.statusCode === 200, 'schema caching works')
97
+
98
+ // 8. Error message is descriptive
99
+ const errBody = JSON.parse(r2.payload)
100
+ assert(errBody.message && errBody.message.length > 0, `error message present: "${errBody.message}"`)
101
+
102
+ await app.close()
103
+ await app2.close()
104
+ await app3.close()
105
+
106
+ console.log(`\n${pass}/${pass + fail} tests passed\n`)
107
+ process.exit(fail > 0 ? 1 : 0)
108
+ }
109
+
110
+ run().catch((err) => {
111
+ console.error(err)
112
+ process.exit(1)
113
+ })