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-pattern-validator

v1.0.2

Published

A vue3 validator library to easily validate fields & handle validators in a sophisticated manner

Downloads

3

Readme

Vue Pattern Validator

Build Status Tests Status

A Vue3 validator library to easily validate fields & handle validators in a sophisticated manner

Overview

Vue Pattern Validator is a versatile validation library for Vue 3 applications. It provides an easy way to validate form fields, supporting both single and multiple field validations. This library also supports integration with i18n for localized error messages.

Installation

Install Vue Pattern Validator using npm or yarn:

npm install vue-pattern-validator
# or
yarn add vue-pattern-validator

Usage

Setup

First, import and use the Vue Pattern Validator plugin in your main Vue file. Define your custom validators and pass them to the plugin.

import { createApp } from 'vue';
import App from './App.vue';
import { createVuePatternValidatorPlugin, Validators } from 'vue-pattern-validator';

// Define custom validators
const customValidators: Validators = {
  required: (val, t) => (val && val.length > 0) || (t ? t('field.required') : 'This field is required'),
  // ... other validators
};

const app = createApp(App);
app.use(createVuePatternValidatorPlugin(customValidators));
app.mount('#app');

Single Field Validation

To validate a single field, use the validate function provided by the library. It returns an error message if validation fails, or undefined if validation passes.

Params

  • validation: first parameter is the name of the validation - string
  • value: second parameter is the value of the field - any
import { inject } from 'vue';
import { ValidateFunction, ValidationResult } from 'vue-pattern-validator';

export default {
  setup() {
    const validate = inject<ValidateFunction>('validate');
    const errorMessage: ValidationResult = validate('required', 'testValue');
    // errorMessage will be 'This field is required' if testValue is empty, or undefined if validation passes
  }
};

Multiple Fields Validation

To validate multiple fields at once, pass an array of tuples to the validate function. Each tuple should contain the validator name and the field value. The function returns an object with the validation results.

Params

  • validationsArray: first parameter is the validations array Array<MultipleValidationObj>

MultipleValidationObj structure

  • fieldName: name of the field - string
  • validation: name of the validation - string
  • value: value of the field - any
import { inject } from 'vue';
import { ValidateFunction, MultipleValidationResult } from 'vue-pattern-validator';

export default {
  setup() {
    const validate = inject<ValidateFunction>('validate');
    const validationResults: MultipleValidationResult = validate([{
      fieldName: 'firstName',
      validation: 'required',
      value: 'john'
    }, {
      fieldName: 'lastName',
      validation: 'required',
      value: ''
    }]);
    /**
     * validationResults will only contain fields for which validation did not pass, it will be an object like:
     * { 
     *   lastName: 'This field is required'
     * }
     * This object doesn't contain firstName because for that field the validation passed
     */
  }
};

Using $ve aka validation error for Validations in Vue Templates

The Vue Pattern Validator library exposes a $ve function that is globally available in Vue templates. This function is particularly useful for form validations, where you can get error message for a specific field value & validation, if the validation passes it will return undefined (so nothing will be rendered on the screen).

Params

  • validation: first parameter is the name of the validation - string
  • value: second parameter is the value of the field - any
<template>
  <div>
    <div class="error-message">{{ $ve(email, 'required') }}</div>

    <input v-model="email" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      email: '',
    };
  },
};
</script>

Notes

  • The $ve function is accessible in all Vue templates without the need to import or pass it explicitly.
  • Ensure that the validators provided to $ve are correctly defined in your custom validators setup.
  • $ve is reactive and will re-evaluate its validators when the input value changes.
  • For more complex validations, consider using Vue's computed properties or methods.

i18n Integration

If your application uses Vue i18n, Vue Pattern Validator can utilize it to return localized error messages. Ensure i18n is set up in your Vue application before initializing the validator plugin.

Custom Validators

You can create custom validators as per your application's requirements. Each validator is a function that takes the field value and an optional translation function t. It should return a string with an error message when validation fails, or true if validation passes.

import { Validators } from 'vue-pattern-validator';

const customValidators: Validators = {
  customRule: (val, t) => {
    if (val does not meet some condition) {
      return t ? t('custom.error.message') : 'Default error message';
    }
    return true;
  }
};

License

Vue Pattern Validator is ISC licensed.

Contribute to the library or report issues on the GitHub repository.