Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ata + Hono on the edge

A Hono app that validates request bodies with ata-validator, running on a Cloudflare Worker.

Live: https://ata-hono-demo.altinmert.workers.dev

curl -X POST https://ata-hono-demo.altinmert.workers.dev/signup \
  -H 'content-type: application/json' \
  -d '{"id":1,"email":"nope","age":9}'

The schema is compiled ahead of time into a static module (npm run build), so there is no eval at runtime, which is what the Workers runtime blocks. No native addon, no extra dependencies.

The adapter

ataValidator wraps an ata AOT-compiled validator as Hono middleware:

import { Hono } from 'hono'
import { ataValidator } from './ata-hono.mjs'
import { validate as validateSignup } from '../dist/signup.validator.mjs'

const app = new Hono()

app.post('/signup', ataValidator(validateSignup), (c) => {
  // only reached when the body is valid
  return c.json({ valid: true, value: c.req.valid('json') })
})

A failed validation returns ata's errors with stable codes and doc links:

{
  "valid": false,
  "errors": [
    { "code": "ATA3001", "instancePath": "/email",
      "message": "must match format \"email\"",
      "docUrl": "https://ata-validator.com/e/ATA3001" }
  ]
}

Run it

npm install
npm run build
npm run dev      # local Workers runtime
npm run deploy   # needs a Cloudflare account

Note

On Node (not the edge) you can skip the build step and use the runtime validator directly through Standard Schema: new Validator(schema)['~standard'] plugs into @hono/standard-validator. The AOT path above is what makes it run on the edge, where runtime compilation is not allowed.

About

ata-validator + Hono on Cloudflare Workers: AOT-compiled, eval-free edge validation with compiler-grade errors

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages