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

@surveyplanet/validator

v1.0.1

Published

Validate html forms.

Downloads

6

Readme

Validator

Validate HTML forms.

Install

npm install --save @surveyplanet/validator

Example

import Validator from '@surveyplanet/validator'
validator = new Validator(
	[
		{
			name: "full-name",
			rules: ["required", "alpha"],
		},
		{
			name: "password",
			rules: ["required", "minLength[8]"],
		},
		{
			name: "password_confirm",
			label: "password confirmation",
			rules: ["required","matches[password]"],
		},
		{
			name: "zip-code",
			label: "Zip Code",
			rules: "exactLength[5]",
			message: "Hey! your %s has to be at exactly %s characters.",
		}
		{
			name: "agree-checkbox",
			rules: "required",
			message: "You must agree to the terms of service.",
		},
	]
);
validator.showValidationErrors = true;
const errors = validator.validate();
if (errors.length) {
	return alert(errors[0].message)
}
// process form...

Options

| Param | Type | Description | | -------------- | ---------------- | ------------------------------------------------------------------------------------- | | fields | Array.<Object> | A collection of field inputs to validate. | | fields.id | String | The input id. | | fields.name | String | Then input name (required), | | fields.label | String | The input label used to parse error message (if not provided filed name is used). | | fields.rules | String\|Array | A single rule name or a list of rule names to use for validation default: 'required'. | | fields.value | Array | Input value (required if input name is not provided). | | fields.message | String | A custom message for the input use %s to parse input label and param respectively. |

Rules

| Name | Violation Message | Description | | --------------------- | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------ | | required | "The %s field is required." | Must not be empty. | | matches[String] | "The %s field does not match the %s field." | Must match another field value. | | url | "The %s field must contain a valid url." | Must be a valid url. | | email | "The %s field must contain a valid email address." | Must be a valid email address. | | emails | "The %s field must contain all valid email addresses." | Must be a comma separated list of valid email addresses. | | minLength[Number] | "The %s field must be at least %s characters in length." | Must be at least X characters long. | | maxLength[Number] | "The %s field must not exceed %s characters in length." | Must be no longer than X characters. | | exactLength[Number] | "The %s field must be exactly %s characters in length." | Must be exactly X characters long. | | greaterThan[Number] | "The %s field must contain a number greater than %s." | Must be greater than X. | | equals[Number] | "The %s field must equal %s." | Must be equal to X. | | lessThan[Number] | "The %s field must contain a number less than %s." | Must be less than X. | | alpha | "The %s field must only contain alphabetical characters." | Must only contain alphabetical characters (A-z). | | alphaNumeric | "The %s field must only contain alpha-numeric characters." | Must only contain alpha-numeric characters (A-z, 0-9). | | alphaDash | "The %s field must only contain alpha-numeric characters, underscores, and dashes." | Must only contain alpha-numeric characters, underscores, or dashes | | numeric | "The %s field must only contain numbers." | Must be a whole number. | | integer | "%s must be a whole number." | Must be an integer. | | decimal | "The %s field must contain a decimal number." | Must be a valid decimal. | | ip | "The %s field must contain a valid IP address." | Must be a valid IP address. | | base64 | "The %s field must contain a base64 string." | Must be a base64 string. | | cvc | "The %s field must contain a valid CVC." | Must be a valid credit card cvc. | | creditCard | "The %s field must contain a valid credit card number." | Must be a valid credit card number. | | phone | "The %s field must contain a valid phone number." | Must be a valid phone number. | | fileType | "The %s field must contain only %s files." | Must be a comma separated list of file types (e.g.: gif,png,jpg). | | hasNumber | "The %s field must contain at least one number." | Must contain a number. | | hasUpper | "The %s field must contain at least one upper case letter." | Must contain an upper case letter. | | hasLower | "The %s field must contain at least one lower case letter." | Must contain a lower case letter. | | custom[Regex] | "The %s field is invalid." | Must match a Regular Expression. |

Methods

validate()

Validates the form. Returns: Array.<Object> - A list of all validation errors.

createValidationErrorMessages(error)

Add a validation error messages to the dom.

| Param | Type | Description | | ----- | -------- | -------------------------------------------- | | error | Object | A single error object from the errors array. |

removeAllValidationErrorMessages()

Removes all the validation error messages from the dom.

Properties

validator.errors

| Name | Type | Description | | -------------- | ---------------- | ------------------------------- | | errors | Array.<Object> | A collection of errors. | | errors.id | String | The input id. | | errors.name | String | The input name | | errors.class | String | The input class | | errors.message | String | The error message | | errors.rule | String | The rule name that was violated |

Default: []

validator.showValidationErrors

| Name | Type | Description | | -------------------- | --------- | ---------------------------------------------------------- | | showValidationErrors | Boolean | Whether or not the errors should be rendered into the dom. |

Default: false

validator.fields

| Name | Type | Description | | ------ | ---------------- | ----------------------------------- | | fields | Array.<Object> | A collection of fields to validate. |

Default: []

Validator.RULES

| Name | Type | Description | | ----- | ---------------- | ----------------------------- | | RULES | Array.<Object> | All the validation rule data. |

Testing

npm install
npm test