npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@adbros/vue-validation

v0.2.3

Published

Composable for schema-based form validation in Vue 3 using Valibot

Readme

📋 @adbros/vue-validation

A flexible, schema-driven form validation composable for Vue 3 using Valibot, with support for:

  • Deeply nested schemas
  • Dirty field tracking
  • Custom errors
  • Full form or per-field validation
  • MaybeRefOrGetter inputs
  • Composable-first design (external state as source of truth)
  • "Reward early, punish late" validation behavior

🧩 Components

🔹 useValidation<TSchema>()

Parameters

  • schema: MaybeRefOrGetter<TSchema> A Valibot schema (BaseSchema or BaseSchemaAsync) defining the structure and rules for validation.

  • data: MaybeRefOrGetter<Record<string, unknown>> The reactive object that holds the form data to be validated.

Returns

{
  handleSubmit: (onSubmit, onError?) => Promise<void>;
  errors: ComputedRef<Record<string, string>>;
  output: Ref<InferOutput<TSchema> | undefined>;

  makeFieldDirty: (name: string) => void;
  cleanField: (name: string) => void;
  makeFormDirty: () => void;
  cleanForm: () => void;

  setCustomError: (field: string, message: string) => void;
  clearCustomError: (field: string) => void;
  clearAllCustomErrors: () => void;

  validate: () => Promise<SafeParseReturn<TSchema>>;
  silentErrors: Ref<FlatErrors<TSchema> | undefined>;
  isDirty: (name: string) => boolean;
  isFormValid: ComputedRef<boolean>;
  dirtyFields: Ref<string[]>;
  validDirtyFields: Ref<string[]>;
}

🧠 Behavior Summary

  • Validation: Runs automatically on schema/data changes and can be manually triggered via validate().
  • Dirty tracking: Optionally tracks which fields have been interacted with via makeFieldDirty(), makeFormDirty() and cleanField().
  • Custom errors: Allows adding external (non-schema) validation messages using setCustomError() etc.

🔸 Key Concepts

✅ Dirty State

  • Fields become dirty via:

    • makeFieldDirty(name)
    • makeFormDirty() (marks all fields dirty)
  • A dirty field is considered valid if no Valibot error exists for it.

❌ Error Reporting

  • Errors are a computed merge of:

    • Valibot field-level errors (only for dirty + invalid fields)
    • Manually set customErrors

📥 handleSubmit(onSubmit, onError?)

Triggers a full validation of the form and invokes the onSubmit callback if validation passes. Optionally calls onError() if the form is invalid.

await handleSubmit(
  async (data) => {
    // Do something with validated `data`
  },
  () => {
    // Optional error handler
  }
)

✏️ Example Usage

<script setup lang="ts">
import { ref } from 'vue'
import * as v from 'valibot'
import { useValidation } from '@adbros/vue-validation'

const schema = v.object({
  username: v.pipe(
    v.string(),
    v.nonEmpty('Zadejte uživatelské jméno.')
  ),
  password: v.pipe(
    v.string(),
    v.nonEmpty('Zadejte heslo.')
  ),
})

const form = ref({
  username: '',
  password: '',
})

const {
  errors,
  makeFieldDirty,
  handleSubmit,
} = useValidation(schema, form)

const submitForm = handleSubmit(
  (values) => {
    console.log('Success:', values);
  },
  () => {
    console.warn('Form has errors', errors.value)
  }
)
</script>

<template>
  <form @submit.prevent="submitForm">
    <label>
      Uživatelské jméno
      <input
        v-model="form.username"
        @blur="makeFieldDirty('username')"
      />
      <span class="error" v-if="errors.username">{{ errors.username }}</span>
    </label>

    <label>
      Heslo
      <input
        type="password"
        v-model="form.password"
        @blur="makeFieldDirty('password')"
      />
      <span class="error" v-if="errors.password">{{ errors.password }}</span>
    </label>

    <button type="submit">Přihlásit se</button>
  </form>
</template>

🔄 Auto Validation

The schema and data are both deeply watched, meaning any change triggers validation automatically unless throttled externally.


🔐 Type Safety

  • Fully type-safe thanks to InferOutput<TSchema> and Valibot's schema inference.
  • Validation schema and input are treated as generic inputs (TSchema), enabling reusable, scalable logic across forms.

💡 Notes

  • You can control the timing and granularity of field validation using your own inputs, e.g., @blur or @change events.
  • Form state (data) is kept external and not mutated internally.
  • This approach is ideal for decoupled forms, such as those using Vue's Composition API and <script setup> pattern.