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

v1.3.5

Published

This package is inspired by Laravel validation, and aims to make body validation easier for Javascript developers

Downloads

2,528

Readme

Simple Body Validator

Simple Body Validator is an open source validation library inspired by the Laravel validation mechanism. The library's goal is to make data validation as easy as possible for Javascript developers on both client and server side.

The validation mechanism can be used with any popular framework like Angular, React, and Vue.

You can learn more about simple body validator by reading the full documentation in our docs

Installing Simple Body Validator

npm i simple-body-validator

Install with Yarn

yarn add simple-body-validator

Advantages

Simple

The main purpose of Simple Body Validator is to make the life of Javascript developers easier when it comes to validating simple and complex data. The methods are very declarative and can be understood easily by developers.

Built in Translation Mechanism

Simple Body Validator comes with a built-in translation mechanism that makes it convenient to retrieve error messages in multiple languages, thus allowing you to support multiple languages for your validation without the need of external libraries. You can follow the documentation to know more on how to implement translation for your error messages.

No Dependencies

The whole validation mechanism does not depend on any external package. There is no need to download several packages each time you want to use Simple Body Validator or to be linked to any third party library.

Flexible

Simple Body Validator is built in a way that gives the developer max flexibility. Rules can be registered easily, thus giving the developers the ability to register as many custom rules as needed. While at the same time taking advantage of the built-in localization mechanism.

Validation Quick Start

The easiest way to continue validation quick start, is to follow the validation quick start section in our docs.

To learn more about Simple Body Validator's validation feature, let's look at a complete example of validating a data object and displaying the error messages back to the user.

By reading the following examples, you will be able to gain a good understanding of the main validation features.

Create a new validation instance

To create a new validation instance you need to import the make method.

    import { make } from 'simple-body-validator';
    const { make } = require('simple-body-validator');

The first argument passed to the make method is the data under validation. The second argument is an object of the validation rules that should be applied to the data.

    const data = {
        name: 'John',
        email: '[email protected]',
        age: 28
    };

    const rules = {
        name: 'required|string|min:3',
        email: 'required|email',
        age: 'min:18'
    };

    const validator = make(data, rules);

As you can see the validation rules are passed as the second argument to the make method. All available validation rules are documented here.

Alternatively, validation rules may be specified as arrays of rules instead of a single | delimited string.

    const rules = {
        name: ['required', 'string', 'min:3'],
        email: ['required', 'email'],
        age: ['min:18']
    };

If you want a more expressive way to set your data and rules, you can chain the methods as shown below.

    const validator = make().setData(data).setRules(rules);

Run Validation

To run the validation against the defined rules you need to invoke the validate method, which will return false in case of failure and true in case of success.

In case of validation failure, an error object will be returned based on the failed rules. You can find out more about validation errors

    if (! validator.validate()) {
        console.log('Errors: ', validator.errors().all());
    }

Stopping On First Validation Failure

The stopOnFirstFailure method will inform the validator that it should stop validating all attributes once a single validation failure has occurred

    if (! validator.stopOnFirstFailure().validate()) {
        console.log('Error: ', validator.errors().first());
    }

Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute.

    validator.setRules({
        name: 'bail|required|string|min:3',
        email: 'bail|required|email',
        age: 'min:18'
    });

While the bail rule only stops a specific field when it encounters a validation failure, the stopOnFirstFailure method will inform the validator that it should stop validating all attributes once a single validation failure has occurred.

A Note On Nested Attributes

If the upcoming HTTP request contains "nested" field data, you may specify these fields in your validation rule using the "dot" syntax.

     validator.setRules({
        title: 'required|max:255',
        'author.name': 'required',
        'author.description': 'required',
    });

You can learn more on nested and wildcard validations using this link