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

simple-validator-js

v1.8.0

Published

Simple form validator and autocorrector.

Downloads

20

Readme

SIMPLE VALIDATOR

This library validates content from input fields like: e-mail, name, ID, or anything. There is no visual implementation, it's vanilla JS, can be used on front-end or back-end.

A visual implementation in React is available in this repository. Features:

  • Simple: only needs a string (the text of the input field).
  • Returns a specific description of the validation problem.
  • Returns the position of the validation problem in the string (to implement underline).
  • Returns an autocorrected version of the string (to implement autocorrect).
  • You build the rules of the validation to cover any validation logic needed (see below).
  • Created in Typescript
  • Tested in production
  • Fully documented with jsdoc and TypeScript, you should see documentation with autocomplete.

Supported languages for error messages: English and Spanish.

Library core concept

Since the validation requirements are very different with each project and country, in my opinion there is no possible "1 line of code" magic solution to validate a form, this is what other tools try to offer. With this tool you can write your validator rules in the simplest syntax possible to cover any validation logic needed, requires more than 1 line of code but not much and solves an infinite validation requirements.

Getting Started

To install run:

npm i simple-validator-js

Everything is inside the "Validation" class, you need to chain methods creating like a query with the validation requirements, example:

import {Validation} from 'simple-validator-js';

const name = "roberto666";
const validationInfo = new Validation(name)
                        .noNumbersAllowed(false)    // We set autocorrect to false for this rule to see how errors are returned.
                        .noSpacesAllowed()
                        .noSpecialCharactersAllowed(true, null)
                        .maxChars(100)
                        .minWords(1)
                        .maxWords(1)
                        .wordsShouldStartWithUpperCase();

console.log(validationInfo.result);

Prints the following object:

{
    text: "Roberto666",             // Notice the uppercase first letter was auto-corrected and this is the correct version.
    textOriginal: "roberto666",
    isValid: false,                 // If isValid is false it means there are errors even after auto-correcting.
    errors: [
                {
                    description: {en: "numbers are not allowed.", es: "no se permite escribir números."},
                    locations: [7, 8, 9]
                }
            ]
}

Library configuration

By default error messages are objects that contains the error in all the languages, to return an error object with only one language call this before other library calls:

Validation.languageFilter = LanguageFilter.english;

With the language you want. This is useful server side, to improve performance when creating a JSON with the validation results.

Usage example with React

// Create the validators (Warning: in arrow functions if you write the {} you must also write return):
class MyComponent extends React.Component {
    validators = {
        email: (t) =>
            new Validation(t)
                .isEmail(),
        password: (t) =>
            new Validation(t)
                .minChars(6)
                .maxChars(70)
    };

    // Apply validators to text fields (Just an example of a possible implementation):
    render() {
        return (
            <MyInputComponent validator={this.validators.email} onChange={this.setState(...)}/>
            <MyInputComponent validator={this.validators.password} onChange={this.setState(...)}/>
        )
    }
    
    // This function checks if the fields are valid to enable the send button in the form:
    formIsValid() {
        const { password, phoneNumber } = this.state;

        return (
            this.validators.email(email).result.isValid &&
            this.validators.password(password).result.isValid
        );
    }
}

MyInputComponent renders the input with the validation results, that component is not provided here, there is an example component like this here.

Donate

If you like this library and want it to become something even better, please donate here.