Repository navigation
feat(ata-validator): add ata validator middleware - #2171
mertcanaltin wants to merge 2 commits into
Conversation
d76d806 to
25bb0fb
Compare
|
Some numbers, since the question with a new validator is usually "why another one". Same 7-field object schema (string, integer, array, nested object with a pattern) for every middleware, in-process Per request:
ata reports every error (two here) in the ajv error shape, keyword, instancePath, schemaPath, params and message; the ajv middleware stops at the first one (bare Same thing with
Fresh Node process, from the first import to the first validated response, median of 11:
Bundle for Workers (esbuild, browser platform, es2022, minified, gzip), a Hono app with one validated route: The runtime entry carries the whole engine, because a schema that arrives at run time can use any keyword; that is the 136 KB row, and it is why the PR has the Happy to share the harness, it's four small files. |

This adds
@hono/ata-validator, a validator middleware for ata-validator. I'm the maintainer of ata-validator and will maintain this package.It follows the same shape as
@hono/ajv-validator:ataValidator(target, schema, hook?),c.req.valid(target)typed from the schema, and a400with{ success: false, errors }when validation fails and no hook returns a response. The failure response is part of the route type, like in the standard-validator.A couple of differences from the ajv one:
as constand the type is inferred from it, noJSONSchemaType<T>neededValidatorinstead of a schema, which is how you getcoerceTypesanduseDefaultson query strings; the handler then receives the coerced valuenew Functionis not available (Cloudflare Workers, strict CSP) ata falls back to its interpreted engine, so the middleware works there without any flag. There is a test for this that replacesglobalThis.Functionwith a throwing one.Tests cover the same cases as ajv-validator plus the hook receiving the error list, a prepared validator with coercion and defaults, and the no-eval case, with
Expect<Equal<...>>checks on the route types.Ran
pnpm typecheck,pnpm test,pnpm lint,pnpm buildanddeno publish --dry-runin the package,pnpm formatat the root.pnpm changesetat the top of this repo and push the changeset