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

light-vue3-validation

v2.1.1

Published

Form validations for vue 3

Downloads

7

Readme

Vue 3 validation hooks

Fully typed form validation hooks for vue 3.

Installation

npm i -S light-vue3-validation

Usage

Validate single form item

For validating a single item you have to import a useValidate hook
useValidate hook returns a reactive typed object

reactive({
    model: Ref<T>;
    isValid: Ref<boolean>;
    modelRules: UnwrapRef<Record<TModelRule, boolean>>;
})
<template>
  <form @submit.prevent="submitForm">
    ...
    <input
        type="number"
        v-model="userIncome.model"
        class="input"
    />
    <p v-if="userIncomeError" class="input_error-message">{{ userIncomeError }}</p>
    ...
  </form>
</template>

<script setup lang="ts">
  import { computed } from "vue"
  import { useValidate } from "light-vue3-validation"
  
  const userIncome = useValidate<number>(0, ['required', 'numeric', { type: 'maxValue', value: 2000 }])
  
  const userIncomeError = computed<boolean | string>(() => {
    if (!userIncome.modelRules.required) return 'User income field is required'
    if (!userIncome.modelRules.numeric) return 'User income must be a numeric'
    if (!userIncome.modelRules.maxValue) return 'User income must be a equal or less then 2000$'
    return false
  })

  const submitForm = () => {
    if (userIncome.isValid) {
      // Do something ...
    }
  }
</script>

Validate an object

For validating an object import a useValidateObject hook
useValidateObject hook returns a reactive typed object

reactive({
    ...validatedFields,
    __isValid: boolean,
    __clean: // Clean object without model
})
<template>
  <form @submit.prevent="submitForm">
    ...
    <div class="mb-4">
      <input id="name" type="text" v-model="user.name.model" />
      <p v-if="userNameErrors" class="input_error-message">{{ userNameErrors }}</p>
    </div>
    
    <div class="mb-4">
      <input type="tel" v-model="user.name.model" />
      <p v-if="userPhoneErrors" class="input_error-message">{{ userPhoneErrors }}</p>
    </div>
    
    <div class="mb-4">
      <input type="text" v-model="user.role.model" />
      <p v-if="userRoleErrors" class="input_error-message">{{ userRoleErrors }}</p>
    </div>
    ...
  </form>
</template>

<script setup lang="ts">
  import { computed } from "vue"
  import { useValidateObject } from "light-vue3-validation"
  
  type User = {
    name: string
    phone: number
    role: 'Admin' | 'User'
  };
  
  const user = useValidateObject<User>({
    name: { model: '', rules: ['required'] },
    phone: { model: '', rules: ['required', 'numeric', { type: 'minLength', value: 9 }] },
    role: { model: '', rules: ['required'] }
  })

  const userNameErrors = computed<boolean | string>(() => {
    if (!user.modelRules.name.required) return 'Name is required'
    return false
  })
  const userPhoneErrors = computed<boolean | string>(() => {
    if (!user.modelRules.phone.required) return 'Phone is required'
    if (!user.modelRules.phone.numeric) return 'Phone must a numeric'
    if (!user.modelRules.phone.minLength) return 'Phone must be at least 9 digits'
    return false
  })
  const userRoleErrors = computed<boolean | string>(() => {
    if (!user.modelRules.role.required) return 'Role is required'
    return false
  })

  const submitForm = () => {
    if (user.__isValid) {
      // Do something ...
    }
  }
</script>

If some filed in your object not need validation just write an empty array in rules { model: '', rules: [] }

Also, you can use it without typescript, just don't write a types

Errors object in useValidate hooks will be soon

useValidate retrun

Key | Value --- | --- model | Generic <T> isValid | boolean modelRules | An object with key of validation keyword required, numeric etc... and value of boolean

useValidateObject return

key | Value --- | --- Keys of your object | Keys wrapped by useValidate hook key: { model: ..., isValid: ..., modelRules: { ... } } __isValid | boolean. Returns true if all isValid keys of your object items becomes true __clean | Returns computed clean object that you passed to hook with no wrapping by useValidate hook for each key

Rules

Rule name | Properties --- | --- alpha | Accepts only alphabetic characters (a-z and A-Z). Numbers will be failed alphaNum | Accepts only alphabetic and numeric characters (a-z A-Z 0-9) between | Accepts an array of range that your number will be. Example [10, 30]. Your number will be valid if it in this range email | Accepts a string which is email ipAddress | Accepts a string like ###.###.###.### maxLength | Accepts a string which length is less then max value you passed minLength | Accepts a string which length is more then min value you passed maxValue | Accepts a number which is less then max value you passed minValue | Accepts a number which is more then min value you passed numeric | Accepts only number 0-9 required | Accepts anything and will be failed if model is empty