Docs / Validation

Validation

Validation schemas are derived from models. Valibot under the hood.

Validation is generated from the model — no separate schema to maintain. Every CRUD POST/PUT/PATCH enforces it.

Failures return 400:

{
  "error": "VALIDATION_ERROR",
  "message": "Invalid body",
  "details": {
    "title": ["Invalid length: Expected >=3 but received 2"],
    "content": ["Invalid key: Expected \"content\" but received undefined"]
  }
}

For custom routes, build a schema from a model on demand:

import { buildModelSchema, validate } from '@hopak/core';
import postModel from '../models/post';

const schema = buildModelSchema(postModel, { omitId: true, partial: true });
const result = validate(schema, await ctx.body());
if (!result.ok) {
  // result.errors: Record<field, string[]>
}