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

vuex-validator

v0.2.7

Published

Validation support for Vuex

Downloads

44

Readme

Vuex ValidatorSponsored by Version Downloads Build Status Dependencies

Vuex Plugin for validation of store values.

Links

Installation

Should be installed locally in your project source code:

Installation via JSPM:

jspm install npm:vuex-validator

Alternatively you can also use plain old NPM:

npm install vuex-validator --save

Integration

In your main application javascript file:

// app.js

import { VuexValidator } from "vuex-validator";
import validators from "./vuex/validators";
import store from "./vuex/store"; // Inside there should be a Vue.use(Vuex) (see Vuex documentation)

Vue.use(VuexValidator, {
  validators
})

Your validator configurator:

// ./vuex/validators

import testValidator from "./validation/test";

const validators = [ testValidator ];

export default validators;

A sample validator:

// ./vuex/validation/test.js

import { BaseValidator } from "vuex-validator/lib/BaseValidator";

class TestValidator extends BaseValidator {
	constructor() {
		super("test"); // Name of validation are, should correlate with Vuex store module

		this.rule("test-name", ["test", "test2"], this.testName); // Name of rule, All properties that are tested, Test function itself
	}

	testName(state) { // State from Vuex
		if (typeof(state.test) !== "number") {
			this.invalid(["test"], "TEST_NOT_A_NUMBER"); // Failed properties and technical error key as string
		}

		if (typeof(state.test2) !== "string") {
			this.invalid(["test2"], "TEST2_NOT_A_STRING"); // Failed properties and technical error key as string
		}

		if (state.test > 20 && state.test2 === "low number") {
			this.invalid(["test", test2"], "TEST_IS_NO_LOW_NUMBER"); // Failed properties and technical error key as string
		}

		return this.errors(); // Return null if no errors, otherwise array of errors
	}
}

export default new TestValidator();

A sample state for this could be:

{
	"test": 123,
	"test2": "a string"
}

Usage

There are two ways to use this validation.

Active validation

To actively validate values you can call

store.$validator.isValid("test-name")

This validates all values of Validator named test-name. It returnes true if all values are valid as defined by your rules in validator test-name. This could be used for backend connection middleware before sending data.

Validation getter in components

Validation getters are added to a component's option object with key validators. This bears analogy to Vuex getters itself mapping global state to component's local state.

export default Vue.extend({
	...

	validators: {
		testInvalid: (validator) => validator.isInvalid("test"),
		test2Invalid: (validator) => validator.isInvalid("test2")
	}
});

isInvalid takes a property path as string. This is either the property name itself or module name and property name seperated via dot.

"<property name>"
"<module name>.<property name>"

All validator functions are mapped to the component's local computed getters. So it is possible to access validation properties in template:

My property {{test}} is invalid: {{testInvalid}}

A falsy value (undefined, null or false) is returend in case of the property validated through all rules. Otherwise an array of failing rules return values are returned. the return structure can be something like:

[{
	error: "TEST_IS_NO_LOW_NUMBER",
	fields: ["test", "test2"],
	valid: false
}]

fields is an array of properties that are tested against in the failing rule. error is the technical error key defined.

Copyright

Copyright 2015-2016Sebastian Software GmbH