fastify-ata 0.2.9 → 0.2.11
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/README.md +42 -8
- package/package.json +2 -2
- package/standalone.js +94 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# fastify-ata
|
|
2
2
|
|
|
3
|
-
Fastify plugin for [ata-validator](https://ata-validator.com)
|
|
3
|
+
Fastify plugin for [ata-validator](https://ata-validator.com) - JSON Schema validation powered by simdjson.
|
|
4
4
|
|
|
5
5
|
Drop-in replacement for Fastify's default ajv validator. Standard Schema V1 compatible.
|
|
6
6
|
|
|
@@ -49,9 +49,41 @@ fastify.register(fastifyAta, {
|
|
|
49
49
|
})
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
## Standalone Mode (Pre-compiled)
|
|
53
|
+
|
|
54
|
+
Drop-in replacement for `@fastify/ajv-compiler/standalone`. Same API.
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
const StandaloneValidator = require('fastify-ata/standalone')
|
|
58
|
+
|
|
59
|
+
// Build phase (once) - compile schemas to JS files
|
|
60
|
+
const app = fastify({
|
|
61
|
+
schemaController: { compilersFactory: {
|
|
62
|
+
buildValidator: StandaloneValidator({
|
|
63
|
+
readMode: false,
|
|
64
|
+
storeFunction(routeOpts, code) {
|
|
65
|
+
fs.writeFileSync(generateFileName(routeOpts), code)
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
}}
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
// Read phase (every startup) - load pre-compiled, near-zero compile time
|
|
72
|
+
const app = fastify({
|
|
73
|
+
schemaController: { compilersFactory: {
|
|
74
|
+
buildValidator: StandaloneValidator({
|
|
75
|
+
readMode: true,
|
|
76
|
+
restoreFunction(routeOpts) {
|
|
77
|
+
return require(generateFileName(routeOpts))
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
}}
|
|
81
|
+
})
|
|
82
|
+
```
|
|
83
|
+
|
|
52
84
|
## Standard Schema V1
|
|
53
85
|
|
|
54
|
-
ata-validator natively implements [Standard Schema V1](https://github.com/standard-schema/standard-schema)
|
|
86
|
+
ata-validator natively implements [Standard Schema V1](https://github.com/standard-schema/standard-schema) - the emerging standard for TypeScript-first schema libraries.
|
|
55
87
|
|
|
56
88
|
```js
|
|
57
89
|
const { Validator } = require('ata-validator')
|
|
@@ -91,16 +123,18 @@ Works with Fastify v5's Standard Schema support, tRPC, TanStack Form, Drizzle OR
|
|
|
91
123
|
| **Serverless cold start** (50 schemas) | 7.7ms | 96ms | **12.5x faster** |
|
|
92
124
|
| **ReDoS protection** (catastrophic pattern) | 0.3ms | 765ms | **immune** |
|
|
93
125
|
| **Batch NDJSON** (10K items, multi-core) | 13.4M/sec | 5.1M/sec | **2.6x faster** |
|
|
94
|
-
| **validate(obj)** valid (isolated) |
|
|
95
|
-
| **validate(obj)** invalid (isolated) |
|
|
126
|
+
| **validate(obj)** valid (isolated) | 68M ops/sec | 8M ops/sec | **8.5x faster** |
|
|
127
|
+
| **validate(obj)** invalid (isolated) | 17M ops/sec | 8M ops/sec | **2.1x faster** |
|
|
128
|
+
| **validateJSON(str)** valid | 3.0M ops/sec | 1.9M ops/sec | **1.6x faster** |
|
|
129
|
+
| **Fastify startup** (500 routes) | 46ms | 77ms (standalone) | **1.7x faster** |
|
|
96
130
|
| **Schema compilation** | 113K ops/sec | 818 ops/sec | **138x faster** |
|
|
97
131
|
|
|
98
132
|
### Things only ata can do
|
|
99
133
|
|
|
100
|
-
- **RE2 regex engine**
|
|
101
|
-
- **Multi-core parallel validation**
|
|
102
|
-
- **Standard Schema V1**
|
|
103
|
-
- **138x faster compilation**
|
|
134
|
+
- **RE2 regex engine** - linear-time guaranteed, immune to ReDoS attacks
|
|
135
|
+
- **Multi-core parallel validation** - NDJSON batch at 12.5M items/sec
|
|
136
|
+
- **Standard Schema V1** - native support, ajv doesn't have it
|
|
137
|
+
- **138x faster compilation** - serverless cold starts, dynamic schemas
|
|
104
138
|
|
|
105
139
|
## License
|
|
106
140
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fastify-ata",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.11",
|
|
4
4
|
"description": "Fastify plugin for ata-validator — beats ajv on every valid-path benchmark. 2.7x faster validate(obj), 151x faster compilation, simdjson + multi-core.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/ata-core/fastify-ata#readme",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"ata-validator": "^0.4.
|
|
29
|
+
"ata-validator": "^0.4.10",
|
|
30
30
|
"fastify-plugin": "^5.1.0",
|
|
31
31
|
"sanitize-filename": "^1.6.4"
|
|
32
32
|
},
|
package/standalone.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { Validator } = require('ata-validator')
|
|
4
|
+
|
|
5
|
+
// Fastify schemaController compatible standalone compiler.
|
|
6
|
+
// Same API as @fastify/ajv-compiler/standalone — drop-in replacement.
|
|
7
|
+
//
|
|
8
|
+
// WRITE mode (build phase):
|
|
9
|
+
// const { StandaloneValidator } = require('fastify-ata/standalone')
|
|
10
|
+
// buildValidator: StandaloneValidator({
|
|
11
|
+
// readMode: false,
|
|
12
|
+
// storeFunction(routeOpts, code) { fs.writeFileSync(fileName, code) }
|
|
13
|
+
// })
|
|
14
|
+
//
|
|
15
|
+
// READ mode (startup):
|
|
16
|
+
// buildValidator: StandaloneValidator({
|
|
17
|
+
// readMode: true,
|
|
18
|
+
// restoreFunction(routeOpts) { return require(fileName) }
|
|
19
|
+
// })
|
|
20
|
+
|
|
21
|
+
function StandaloneValidator(options = { readMode: true }) {
|
|
22
|
+
if (options.readMode === true && !options.restoreFunction) {
|
|
23
|
+
throw new Error('You must provide a restoreFunction when readMode is true')
|
|
24
|
+
}
|
|
25
|
+
if (options.readMode !== true && !options.storeFunction) {
|
|
26
|
+
throw new Error('You must provide a storeFunction when readMode is false')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (options.readMode === true) {
|
|
30
|
+
// READ MODE: load pre-compiled validator from file
|
|
31
|
+
return function wrapper() {
|
|
32
|
+
return function buildValidatorFunction(opts) {
|
|
33
|
+
const mod = options.restoreFunction(opts)
|
|
34
|
+
// mod is either a function (standalone) or { boolFn, hybridFactory, errFn }
|
|
35
|
+
if (typeof mod === 'function') {
|
|
36
|
+
// Direct function — just wrap for Fastify
|
|
37
|
+
return wrapValidator(mod)
|
|
38
|
+
}
|
|
39
|
+
// Module from toStandalone() — restore via fromStandalone
|
|
40
|
+
if (mod.boolFn || mod.hybridFactory) {
|
|
41
|
+
const v = Validator.fromStandalone(mod, opts.schema)
|
|
42
|
+
return wrapValidator((data) => v.validate(data))
|
|
43
|
+
}
|
|
44
|
+
// Fallback: compile fresh
|
|
45
|
+
const v = new Validator(opts.schema)
|
|
46
|
+
return wrapValidator((data) => v.validate(data))
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// WRITE MODE: compile schema, generate standalone code, store to file
|
|
52
|
+
return function wrapper() {
|
|
53
|
+
return function buildValidatorFunction(opts) {
|
|
54
|
+
const v = new Validator(opts.schema)
|
|
55
|
+
const standalone = v.toStandalone()
|
|
56
|
+
|
|
57
|
+
if (standalone) {
|
|
58
|
+
options.storeFunction(opts, standalone)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return wrapValidator((data) => v.validate(data))
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function wrapValidator(validateFn) {
|
|
67
|
+
// Fastify expects: return true on valid, return false on invalid.
|
|
68
|
+
// On invalid, validate.errors must contain ajv-compatible error objects.
|
|
69
|
+
function validate(data) {
|
|
70
|
+
const result = validateFn(data)
|
|
71
|
+
if (result && result.valid !== undefined) {
|
|
72
|
+
if (result.valid) {
|
|
73
|
+
validate.errors = null
|
|
74
|
+
return true
|
|
75
|
+
}
|
|
76
|
+
validate.errors = result.errors.map(e => ({
|
|
77
|
+
message: e.message,
|
|
78
|
+
instancePath: e.path || '',
|
|
79
|
+
schemaPath: '',
|
|
80
|
+
keyword: e.code || 'validation',
|
|
81
|
+
params: {},
|
|
82
|
+
}))
|
|
83
|
+
return false
|
|
84
|
+
}
|
|
85
|
+
// Boolean result
|
|
86
|
+
if (result) { validate.errors = null; return true }
|
|
87
|
+
validate.errors = [{ message: 'validation failed', instancePath: '', schemaPath: '', keyword: 'validation', params: {} }]
|
|
88
|
+
return false
|
|
89
|
+
}
|
|
90
|
+
validate.errors = null
|
|
91
|
+
return validate
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = StandaloneValidator
|