fastify-ata 0.2.9 → 0.2.10
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/package.json +1 -1
- package/standalone.js +94 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fastify-ata",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.10",
|
|
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",
|
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
|