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 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-use-validation

v1.0.0

Published

Light validation composable for Vue 3.

Downloads

1

Readme

vue-use-validation

Light validation composable for Vue 3.

Examples

Example with native input elements

Example with custom input components (recommended)

Simple example

<template>
  <input
    v-model="username"
    type="text"
    placeholder="Username"
    @blur="inputs.username.touch()"
  />
  <ul v-if="inputs.username.state.value === 'invalid'">
    <li v-for="m in inputs.username.messages.value">
      {{ m }}
    </li>
  </ul>

  <input
    v-model="password"
    type="text"
    @blur="inputs.password.touch()"
  />
  <ul v-if="inputs.password.state.value === 'invalid'">
    <li v-for="m in inputs.password.messages.value">
      {{ m }}
    </li>
  </ul>
</template>

<script setup>
import { ref } from 'vue';
import useValidation from './use-validation';

let username = ref('');
let password = ref('');

let inputs = useValidation([
  {
    name: 'username',
    value: username,
    rules: [
      "required",
      { minLength: 5 },
      "alphanumeric",
    ],
  },
  {
    name: 'password',
    value: password,
    rules: [
      "required",
      { minLength: 8 },
      "atLeastOneLowercase",
      "atLeastOneUppercase",
      "atLeastOneDigit",
      "atLeastOneSpecial",
    ],
    options: {
      validateOn: 'immediate',
      validateMode: 'eager',
    },
  },
]);
</script>

Usage:

useValidation function takes as argument object (or array of objects) describing your input (or inputs). It contains required value to validate (for example inputs model), rules property and number of optional properties to customize validation for specific input. Function returns and constantly updates object that contains validation results (status, state, messages) and number of functions (touch, formValidate, reset) to perform validation related actions on input.

let inputs = useValidation(inputs: Input | Input[], options?: GlobalOptions): Validation

Arguments:

type Input = {
  form?: object,
  name?: string,
  value: Ref,
  rules: object,
  options?: {
    validateOn?: string,
    validateMode?: string,
  },
  externalState?: Ref,
  onUpdate?: function,
  onReset?: function,
}

type GlobalOptions = {
  form?: object,
  rules?: object,
  options?: object,
  externalState?: Ref,
  onUpdate?: function,
  onReset?: function,
}

Required properties:

  • value - value to validate (v-model of input)
  • rules - an array of validation rules where each item is a name of global validator (string or object) or object with single function and tested value as argument. Functions should return true for valid values or the string message if the result is invalid. For list of available validators check validators.js

Optional properties:

  • form - form object. Same form object can be used in multiple inputs to allow manual validation and resetting of entire group. Create form object with useFormValidation function:
let form = useFormValidation()
  • name - name of input. Name is required when adding multiple inputs.
  • externalState - external validation state to override calculated state
  • options - object with following options:
    • validateOn - defines conditions to start validation. Possible conditions are:
      • "blur" - validate after input loses focus (default)
      • "immediate" - starts validating immediately after first input
      • "form" - validate after calling formValidate function
    • validateMode - defines how to update state according to validation results. Possible modes are:
      • "silent" - valid values does not change inputs validaton state to valid unless it was invalid before (only for validate on blur)(default)
      • "eager" - invalid and valid values always change inputs validation state

Optional callbacks:

  • onUpdateState - optional callback to run on each update of validation state. This function has 3 arguments: status, state and messages.
  • onReset - optional callback to run on form reset. This can be used to perform additional actions on reset, for example set input value to its default, as internally useValidation resets only validation related data.

Returns:

type Validation = {
  form: object,
  name: string,
  value: Ref,
  status: Ref,
  state: Ref,
  messages: Ref,
  touch: function,
  formValidate: function,
  reset: function,
}

For single input one validation object is returned. For arrays of inputs validation objects are nested under their input names.

Validation results:

  • status - object containg the results of validation and the current state of input (for example touched, dirty etc). It is updated once initially and then after each value change.
  • state - final validation state of input. This state is based on current status and is updated only when conditions in validateOn and validateMode options are fulfilled. By default it is empty string for initial state of input, "valid" for valid input and "invalid" for invalid input.
  • messages - object containing validation messages.

Functions:

  • touch - function that should be set as handler for inputs blur event.
  • formValidate - function to manually validate and update validation state of the input.
  • resetValidation - function that resets validation to default state.

Input properties (same as in arguments):

  • form - form object
  • name - name of the input
  • value - current value of input