fastify-ata 0.6.0 → 0.7.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/README.md CHANGED
@@ -123,6 +123,48 @@ Off by default to keep the ajv-compatible message shape.
123
123
 
124
124
  `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.
125
125
 
126
+ ## Two ways to plug in
127
+
128
+ ### Plugin (encapsulated)
129
+
130
+ `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.
131
+
132
+ ### Global default (full replacement)
133
+
134
+ 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`](https://github.com/Eomm/joi-compiler) use, so the default ajv validator is never built.
135
+
136
+ ```js
137
+ const Fastify = require('fastify')
138
+ const AtaCompiler = require('fastify-ata/compiler')
139
+
140
+ const app = Fastify({
141
+ schemaController: { compilersFactory: { buildValidator: AtaCompiler() } },
142
+ })
143
+
144
+ app.post('/user', {
145
+ schema: {
146
+ body: {
147
+ type: 'object',
148
+ properties: { name: { type: 'string' }, age: { type: 'integer' } },
149
+ required: ['name'],
150
+ },
151
+ },
152
+ }, (req, reply) => reply.send({ ok: true }))
153
+ ```
154
+
155
+ 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`:
156
+
157
+ ```js
158
+ buildValidator: AtaCompiler() // defaults match Fastify's ajv
159
+ // per-instance overrides are read from ajv.customOptions, e.g.:
160
+ const app = Fastify({
161
+ ajv: { customOptions: { coerceTypes: false, allErrors: true } },
162
+ schemaController: { compilersFactory: { buildValidator: AtaCompiler() } },
163
+ })
164
+ ```
165
+
166
+ 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.
167
+
126
168
  ## Standalone Mode (Pre-compiled)
127
169
 
128
170
  Drop-in replacement for `@fastify/ajv-compiler/standalone`. Same API.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify-ata",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Fastify plugin for ata-validator. Runtime-competitive with the default, 24x faster serverless cold start, Standard Schema V1.",
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.20.0",
29
+ "ata-validator": "^1.0.0",
30
30
  "fastify-plugin": "^5.1.0",
31
31
  "sanitize-filename": "^1.6.4"
32
32
  },
package/standalone.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { Validator } = require('ata-validator')
4
+ const { toStandaloneModule } = require('ata-validator/build')
4
5
 
5
6
  // Fastify schemaController compatible standalone compiler.
6
7
  // Same API as @fastify/ajv-compiler/standalone — drop-in replacement.
@@ -31,12 +32,15 @@ function StandaloneValidator(options = { readMode: true }) {
31
32
  return function wrapper() {
32
33
  return function buildValidatorFunction(opts) {
33
34
  const mod = options.restoreFunction(opts)
34
- // mod is either a function (standalone) or { boolFn, hybridFactory, errFn }
35
+ // mod is a function (standalone), a { validate, isValid } module
36
+ // (toStandaloneModule output), or a legacy { boolFn, hybridFactory,
37
+ // errFn } artifact from the pre-1.0 instance toStandalone().
35
38
  if (typeof mod === 'function') {
36
- // Direct function — just wrap for Fastify
37
39
  return wrapValidator(mod)
38
40
  }
39
- // Module from toStandalone() restore via fromStandalone
41
+ if (typeof mod.validate === 'function') {
42
+ return wrapValidator(mod.validate)
43
+ }
40
44
  if (mod.boolFn || mod.hybridFactory) {
41
45
  const v = Validator.fromStandalone(mod, opts.schema)
42
46
  return wrapValidator((data) => v.validate(data))
@@ -52,7 +56,8 @@ function StandaloneValidator(options = { readMode: true }) {
52
56
  return function wrapper() {
53
57
  return function buildValidatorFunction(opts) {
54
58
  const v = new Validator(opts.schema)
55
- const standalone = v.toStandalone()
59
+ // CJS so the stored file works with a plain require() in restoreFunction.
60
+ const standalone = toStandaloneModule(v, { format: 'cjs' })
56
61
 
57
62
  if (standalone) {
58
63
  options.storeFunction(opts, standalone)