Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
revalidator
Advanced tools
The revalidator npm package is a versatile tool for validating JavaScript objects against a defined schema. It supports a wide range of validation rules and custom validation logic, making it suitable for various data validation needs.
Basic Schema Validation
This feature allows you to define a schema with various properties and validate an object against it. The code sample demonstrates how to validate a person's name and age.
const revalidator = require('revalidator');
const schema = {
properties: {
name: {
description: 'Name of the person',
type: 'string',
required: true,
minLength: 2
},
age: {
description: 'Age of the person',
type: 'integer',
minimum: 0
}
}
};
const person = { name: 'John', age: 30 };
const result = revalidator.validate(person, schema);
console.log(result);
Custom Validation
This feature allows you to add custom validation logic using the 'conform' function. The code sample shows how to validate a password to ensure it is at least 8 characters long.
const revalidator = require('revalidator');
const schema = {
properties: {
password: {
description: 'User password',
type: 'string',
conform: function (value) {
return value.length >= 8;
},
messages: {
conform: 'Password must be at least 8 characters long'
}
}
}
};
const user = { password: 'short' };
const result = revalidator.validate(user, schema);
console.log(result);
Nested Object Validation
This feature supports validation of nested objects. The code sample demonstrates how to validate an address object within a person object.
const revalidator = require('revalidator');
const schema = {
properties: {
address: {
type: 'object',
properties: {
street: { type: 'string', required: true },
city: { type: 'string', required: true }
}
}
}
};
const person = { address: { street: '123 Main St', city: 'Anytown' } };
const result = revalidator.validate(person, schema);
console.log(result);
AJV (Another JSON Schema Validator) is a popular JSON schema validator that supports JSON Schema draft-07 and later. It is known for its high performance and extensive features, including asynchronous validation and custom keywords. Compared to revalidator, AJV offers more advanced features and better performance for large-scale applications.
Joi is a powerful schema description language and data validator for JavaScript. It allows you to create blueprints for JavaScript objects to ensure validation. Joi is known for its expressive and readable syntax, making it easier to define complex validation rules. Compared to revalidator, Joi provides a more intuitive API and better integration with Node.js applications.
Yup is a JavaScript schema builder for value parsing and validation. It is often used in conjunction with form libraries like Formik. Yup provides a fluent API for building schemas and supports various validation methods. Compared to revalidator, Yup is more focused on form validation and offers better integration with React applications.
A cross-browser / node.js validator used by resourceful and flatiron.
The core of revalidator
is simple and succinct: revalidator.validate(obj, schema)
:
var revalidator = require('revalidator');
console.dir(revalidator.validate(someObject, {
properties: {
url: {
description: 'the url the object should be stored at',
type: 'string',
pattern: '^/[^#%&*{}\\:<>?\/+]+$',
required: true
},
challenge: {
description: 'a means of protecting data (insufficient for production, used as example)',
type: 'string',
minLength: 5
},
body: {
description: 'what to store at the url',
type: 'any',
default: null
}
}
}));
This will return with a value indicating if the obj
conforms to the schema
. If it does not, a descriptive object will be returned containing the errors encountered with validation.
{
valid: true // or false
errors: [/* Array of errors if valid is false */]
}
In the browser, the validation function is exposed on window.validate
by simply including revalidator.js
.
$ curl http://npmjs.org/install.sh | sh
$ [sudo] npm install revalidator
revalidator
takes json-schema as input to validate objects.
This will return with a value indicating if the obj
conforms to the schema
. If it does not, a descriptive object will be returned containing the errors encountered with validation.
{
valid: true // or false
errors: [/* Array of errors if valid is false */]
}
validateFormats
is true treat unrecognized formats as validation errors (default false)validateFormats
is true also validate formats defined in validate.formatExtensions
(default true)For a property an value
is that which is given as input for validation where as an expected value
is the value of the below fields
If true, the value should not be empty
{ required: true }
The type of value
should be equal to the expected value
{ type: 'string' }
{ type: 'number' }
{ type: 'integer' }
{ type: 'array' }
{ type: 'boolean' }
{ type: 'object' }
{ type: 'null' }
{ type: 'any' }
{ type: ['boolean', 'string'] }
The expected value regex needs to be satisfied by the value
{ pattern: /^[a-z]+$/ }
The length of value must be greater than or equal to expected value
{ maxLenght: 8 }
The length of value must be lesser than or equal to expected value
{ minLenght: 8 }
Value must be greater than or equal to the expected value
{ minimum: 10 }
Value must be lesser than or equal to the expected value
{ maximum: 10 }
Value must be greater than expected value
{ exclusiveMinimum: 9 }
Value must be lesser than expected value
{ exclusiveMaximum: 11 }
Value must be divisible by expected value
{ divisibleBy: 5 }
{ divisibleBy: 0.5 }
Value must contain more then expected value number of items
{ minItems: 2 }
Value must contains less then expected value number of items
{ maxItems: 5 }
Value must hold a unique set of values
{ uniqueItems: true }
Value must be present in the array of expected value
{ enum: ['month', 'year'] }
Value must be a valid format
{ format: 'url' }
{ format: 'email' }
{ format: 'ip-address' }
{ format: 'ipv6' }
{ format: 'date-time' }
{ format: 'date' }
{ format: 'time' }
{ format: 'color' }
{ format: 'host-name' }
{ format: 'utc-millisec' }
{ format: 'regex' }
Value must conform to constraint denoted by expected value
{ conform: function (v) {
if (v%3==1) return true;
return false;
}
}
Value is valid only if the dependent value is valid
{
town: { required: true, dependencies: 'country' },
country: { maxLength: 3, required: true }
}
We also allow nested schema
{
properties: {
title: {
type: 'string',
maxLength: 140,
required: true
},
author: {
type: 'object',
required: true,
properties: {
name: {
type: 'string',
required: true
},
email: {
type: 'string',
format: 'email'
}
}
}
}
}
We also allow custom message for different constraints
{
type: 'string',
format: 'url'
messages: {
type: 'WOAH! Do you use a number for url in your village?',
format: 'DUDE! Its a goddamn url'
}
{
conform: function () { ... },
message: 'This can be used as a global message'
}
All tests are written with vows and should be run with npm:
$ npm test
FAQs
A cross-browser / node.js validator powered by JSON Schema
The npm package revalidator receives a total of 336,954 weekly downloads. As such, revalidator popularity was classified as popular.
We found that revalidator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
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.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.