//intevel.h3-valibotbysandros94

intevel.h3-valibot

馃 Schema validation for h3 using Valibot

0
0
0
TypeScript

h3-valibot

npm version
npm downloads
Github Actions CI
License

Schema validation for h3 using Valibot 馃

Install


# Using npm
npm install h3-valibot

# Using yarn
yarn add h3-valibot

Validation

import { useValidatedBody, v } from 'h3-valibot'

import { createApp, createRouter, eventHandler } from "h3";
import { email, minLength, string, objectAsync } from 'valibot';

export const app = createApp();
const LoginSchema = v.object({
    email: v.pipe(v.string(), v.email()),
    password: v.pipe(v.string(), v.minLength(8)),
 });

const router = createRouter();
app.use(router);

router.post("/login", eventHandler(async (event) => {
    const body = await useValidatedBody(event, LoginSchema);
    return body;
  }),
);

Errors

h3-valibot throws an ValiError when the validation fails:

Exampl

{
  "statusCode": 400,
  "statusMessage": "Bad Request",
  "stack": [],
  "data": {
    "issues": [
      {
        "validation": "email",
        "origin": "value",
        "message": "Invalid email",
        "input": "github@conner-bachmande",
        "path": [
          {
            "schema": "object",
            "input": {
              "email": "github@conner-bachmande",
              "password": "12345678"
            },
            "key": "email",
            "value": "github@conner-bachmande"
          }
        ],
        "reason": "string"
      }
    ],
    "name": "ValiError"
  }
}

Nuxt auto-imports

This library supports Nuxt鈥檚 auto-imports, just add it in your nuxt.config.ts:

export default defineNuxtConfig({
  modules: [
    // ...
    'h3-valibot/nuxt',
  ],
})

License

Published under MIT - Made with 鉂わ笍 by Conner Bachmann

[beta]v0.14.0