🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

validate.js

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validate.js

Declarative validations for JavaScript

0.3.2
Source
npm
Version published
Weekly downloads
454K
-23.66%
Maintainers
1
Weekly downloads
 
Created

What is validate.js?

Validate.js is a lightweight JavaScript library for data validation. It provides a simple and flexible way to validate data structures, ensuring that they meet specified criteria. The library supports a wide range of validation rules and can be easily extended to include custom validations.

What are validate.js's main functionalities?

Basic Validation

This feature allows you to validate basic data structures against a set of predefined constraints. In this example, the 'name' field must be present and have a minimum length of 3 characters, and the 'email' field must be present and follow a valid email format.

const validate = require('validate.js');

const constraints = {
  name: {
    presence: true,
    length: {
      minimum: 3
    }
  },
  email: {
    presence: true,
    email: true
  }
};

const data = {
  name: 'John Doe',
  email: 'john.doe@example.com'
};

const validationResult = validate(data, constraints);
console.log(validationResult);

Custom Validation

This feature allows you to define custom validation rules. In this example, a custom validator is created to check if the 'customField' matches the expected value. If it doesn't, a custom error message is returned.

const validate = require('validate.js');

validate.validators.customValidator = function(value, options, key, attributes) {
  if (value !== options.expectedValue) {
    return options.message || `${key} is not valid`;
  }
};

const constraints = {
  customField: {
    customValidator: {
      expectedValue: 'expectedValue',
      message: 'Custom validation failed'
    }
  }
};

const data = {
  customField: 'wrongValue'
};

const validationResult = validate(data, constraints);
console.log(validationResult);

Asynchronous Validation

This feature supports asynchronous validation, which is useful for scenarios where validation depends on external data or processes. In this example, an asynchronous validator checks if the 'asyncField' matches the expected value after a delay.

const validate = require('validate.js');

validate.validators.asyncValidator = function(value, options, key, attributes) {
  return new validate.Promise(function(resolve, reject) {
    setTimeout(function() {
      if (value !== options.expectedValue) {
        resolve(options.message || `${key} is not valid`);
      } else {
        resolve();
      }
    }, 1000);
  });
};

const constraints = {
  asyncField: {
    asyncValidator: {
      expectedValue: 'expectedValue',
      message: 'Asynchronous validation failed'
    }
  }
};

const data = {
  asyncField: 'wrongValue'
};

validate.async(data, constraints).then(function(validationResult) {
  console.log(validationResult);
});

Other packages similar to validate.js

Keywords

validation

FAQs

Package last updated on 05 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts