@stone-js/validation
v0.8.15
Published
Framework-agnostic input validation for Stone.js. Define a schema once (Zod, Valibot, ArkType — anything Standard Schema) and validate it identically on the backend and the frontend.
Maintainers
Readme
Stone.js · Validation
Framework-agnostic input validation for Stone.js. Define a schema once (Zod, Valibot, ArkType — anything Standard Schema) and validate it identically on the backend and the frontend.
Part of Stone.js, the reference implementation of the Continuum Architecture: write your domain once, and the context (runtime, protocol, caller) applies to it at run time.
Install
npm i @stone-js/validationEnabling it
Like every Stone.js module, it is enabled in one of two ways, and configured afterwards under
stone.validation. It registers the validation provider, so the validator is injectable and the route decorators resolve.
import { Validation } from '@stone-js/validation'
import { StoneApp } from '@stone-js/core'
@Validation()
@StoneApp({ name: 'my-app' })
export class Application {}import { defineStoneApp } from '@stone-js/core'
import { validationBlueprint } from '@stone-js/validation'
export const Application = defineStoneApp({ name: 'my-app' }, [validationBlueprint])Configure it from a @Configuration class or defineConfig:
export const AppConfig = defineConfig((blueprint) => blueprint.set('stone.validation', { /* ... */ }))Usage
import { z } from 'zod'
import { validate } from '@stone-js/validation'
import { EventHandler, Post } from '@stone-js/router'
export const NewTask = z.object({ title: z.string().min(1).max(120) })
@EventHandler('/tasks')
export class TaskController {
// Rejects a malformed body with 422 before the handler runs. Same schema validates the form.
@Post('/', { middleware: [validate({ body: NewTask })] })
create (event) { return event.get('body') }
}Documentation
Full documentation: stonejs.dev/docs/extensions/validation.
License
MIT © Evens Pierre ("Mr. Stone") and the Stone.js contributors.
