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

pixel-validate-vue

v1.0.6

Published

Vue 3 validation module. Very simple and lightweight. Fully typed

Downloads

24

Readme

pixel-validate-vue

Vue 3 validation module. Very simple and lightweight. Fully typed.

To install package run:

# installation
npm install pixel-validate-vue

Basic sage

<script>
  import {$validate, email, required, sameAs} from 'pixel-validate-vue';
  
  # In $validate you have 
  # errorList - object with errors. Keys equals keys in rules and data 
  # validate(rules, data) - check data by rules, returns true or false 
  # set(key: string, message: string) - set some key with error message 
  # unset(key: string) - reset error by key 
  # clearErrors() - clears all errors in list 
  const {errorsList, validate} = $validate;
  
  # data keys and rules keys must be equal, or you can make your own object with rules keys to send it to validate func.  
  const data = ref({
    email: '',
    password: '',
    passwordRepeat: '',
  });
  
  #You can place rules in any file and then import. This will do your script clearer.
  const rules = {
    # plugin has default messages. You can show custom message by sending a string to a method
    # !!! in minLength and sameAs custom message is a second argument
    email: {email: email('The email is invalid'), reqired: required()},
    password: {required: required()},
    passwordRepeat: {required: required(), sameAs: sameAs('password')},
  };
  
  const onSubmit = () => {
    # You can call validate func in any file (any store for example) send data from view and import rules
    # This will make your script setup clearer 
    if (validate(rules, data.value)) {
      return true;
    }  
  }
</script>

<template>
  <form @submit.prevent="onSubmit">
    <div class="form-group">
      <input v-model="data.email" />
      # This key will show one actual error text by priority in rules. Will show nothing when all the rules are done.  
      <div v-if="errorList?.email" class="error">{{errorList.email}}</div>
    </div>
    <div class="form-group">
      <input v-model="data.password" />
      <div v-if="errorList?.password" class="error">{{errorList.password}}</div>
    </div>
    <div class="form-group">
      <input v-model="data.passwordRepeat" />
      <div v-if="errorList?.passwordRepeat" class="error">{{errorList.passwordRepeat}}</div>
    </div>
    <button>Submit</button>
  </form>
</template>

Examples

<script>
  import {$validate, email, required, sameAs} from 'pixel-validate-vue';
  
  const {errorsList, validate, clearErrors, unset, set} = $validate;
  
  const data = ref({
    email: '',
    password: '',
    passwordRepeat: '',
  });
  
  const rules = {
    email: {email: email('The email is invalid'), required: required()},
    password: {required: required(), minLength(8, 'Password is too short')},
    passwordRepeat: {required: required(), sameAs: sameAs('password')},
  };
  
  const onSubmit = () => {
    if (validate(rules, data.value)) {
      return true;
    }
    # setting error with custom key
    set('global', 'Form is invalid');
  }
</script>

<template>
  <form @submit.prevent="onSubmit">
    # will clear all errors
    <div class="clear" @click="clearErrors">Reset errors</div>
    <div class="form-group">
      # unset will clear error on input
      <input v-model="data.email" @input="unset('email')" />
      <div v-if="errorList?.email" class="error">{{errorList.email}}</div>
    </div>
    <div class="form-group">
      <input v-model="data.password" />
      <div v-if="errorList?.password" class="error">{{errorList.password}}</div>
    </div>
    <div class="form-group">
      <input v-model="data.passwordRepeat" />
      <div v-if="errorList?.passwordRepeat" class="error">{{errorList.passwordRepeat}}</div>
    </div>
    <button>Submit</button>
    # custom error
    <div class="global" v-if="errorList.global">{{errorList.global}}</div>
  </form>
</template>

List validators

file(customMessage: string) - checks that the file is valid (field will not be required)
required(customMessage: string) - checks that value not zerolength
minLength(min: number, customMessage: string) - checks length of the string
maxLength(max: number, customMessage: string) - checks length of the string
email(customMessage: string) - checks that email is correct
sameAs(key: string, customMessage: string) - checks that value of the field equals value from key in data object
checked(customMessage: string) - checks that true/false field are checked

You can write your own method and set it in rules! This will work as native.