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-jvalidate

v1.0.8

Published

Vue 3 validation plugin

Downloads

6

Readme

A simple and flexible validation plugin for vue 3.

Installation

npm install vue-jvalidate

Usage

1. Import and register plugin
// main.js
import jvalidate from 'vue-jvalidate';
app.use(jvalidate);
2. Inject into component
// MyForm.vue
const jvalidate = inject('jvalidate');
3. Call jvalidate() with field and validation objects

The return value will be a Map object with key and value pairs equal to the field name and error message of any failed validations. If everything passes, it will be an empty map.

// MyForm.vue
const errors = ref(new Map());
const fields = ref({
	name: '',
	email: '',
});
const validation = {
	name: ['required'],
	email: ['required', 'email']
}

function onSubmit() {
	const errors.value = jvalidate(fields.value, validation)

	if (errors.value.size) {
		// Do something with the errors
		return;
	}
}

Handling errors

The return value will be a Map object with key and value pairs equal to the field name and error message of any failed validations. If everything passes, it will be an empty map.

Nested objects keys will joined with a colon.

Displaying error messages in your template
// Top level error
<div v-if="errors.has('email')">
{{ errors.get('email') }}
</div>

// Nested error
<div v-if="errors.has('address:city')">
{{ errors.get('address:city') }}
</div>

Field Object

This is an object of the data you wish to validate

const fields = ref({
	name: '',
	email: '',
	message: '',
})

Validation Object

The validation object contains the validation rules for the values in the field object. It should mirror the schema of your fields. You can omit fields that don't require any validation.

The value of each field should be a array of validators.

Validators

A validator is an object with two properties:

  1. rule: a function that takes a value and returns true or false
  2. message: a string to return if the field fails the rule (i.e. it returns false)
Example validator
# Require 20 characters
{
	rule: (value) => value.length >= 20,
	message: 'A minimum of 20 characters is required'
}
Validator usage
const validation = {
	name: [{
		rule: (value) => value ? true : false,
		message: 'This field is required'
	}],
	email: [{
		rule: (value) => value ? true : false,
		message: 'This field is required'
	}, {
		rule: (value) => value.match(/^(.+)@(.+)$/),
		message: 'Please enter a valid email'
	}]
}

The example above will:

  1. Check that name field is populated
  2. Check that email field is populated
  3. If email is populated, check that email field matches the regex pattern

The validation for each field will stop at the first failure.

Named Validators

Instead of an object, you can also pass a string that corresponds with the name of a pre-defined validator.

Using named validators
const validation = {
	name: ['required'],
	email: ['required', 'email']
}

The plugin contains a handful of pre-defined validators:

Pre-defined validators
required: {
	rule: (value) => value ? true : false,
	message: 'This field is required'
},
email: {
	rule: (value) => value.match(/^(.+)@(.+)$/),
	message: 'Invalid email'
},
phone: {
	rule: (value) => value.match(/^\(\d{3}\)\s\d{3}-\d{4}$/),
	message: 'Invalid phone number'
},
zipCode: {
	rule: (value) => value.match(/^\d{5}$/),
	message: 'Invalid zip code'
}

Adding your own named validators

You can merge your own list of validators with the pre-packaged ones in the options object when registering the plugin.

// main.js
import { myValidators } from '@/helpers/validators';
import { jvalidate } from 'vue-jvalidate';
app.use(jvalidate, { validators: myValidators });