Introduction to Revalidator
Revalidator is a simple, extensible validation library for JavaScript objects. Whether you are building a web application, API, or any other form of data input handling, knowing how to validate your data efficiently is essential. This guide covers the basics of Revalidator and showcases dozens of useful API examples with code snippets.
Getting Started with Revalidator
npm install revalidator
First, let’s dive into the basics of Revalidator by using a simple validation example:
Basic Usage
const revalidator = require('revalidator');
const schema = {
properties: {
name: {
description: 'Name of the person',
type: 'string',
required: true,
allowEmpty: false
},
age: {
description: 'Age of the person',
type: 'integer',
minimum: 0,
maximum: 120
}
}
};
const person = {
name: 'John',
age: 30
};
const result = revalidator.validate(person, schema);
console.log(result.valid); // true
Advanced Usage
In addition to basic data types, Revalidator also supports more complex validation rules:
const schema = {
properties: {
email: {
type: 'string',
format: 'email',
required: true
},
password: {
type: 'string',
minLength: 8
},
tags: {
type: 'array',
items: {
type: 'string'
}
}
}
};
const user = {
email: 'example@domain.com',
password: 'mypassword',
tags: ['admin', 'user']
};
const result = revalidator.validate(user, schema);
console.log(result.valid); // true
Revalidator also allows the definition of custom validation functions for more granular control:
const schema = {
properties: {
username: {
type: 'string',
conform: function (value) {
return value.length >= 5;
}
}
},
messages: {
conform: 'is too short should be at least 5 characters.'
}
};
const user = {
username: 'admin'
};
const result = revalidator.validate(user, schema);
result.errors.forEach((error) => {
console.log(error.property, error.message);
});
Application Example
Here is a simple Node.js application that uses Revalidator to validate user input from a form:
const express = require('express');
const bodyParser = require('body-parser');
const revalidator = require('revalidator');
const app = express();
app.use(bodyParser.json());
const userSchema = {
properties: {
username: {
type: 'string',
required: true
},
email: {
type: 'string',
format: 'email',
required: true
},
password: {
type: 'string',
minLength: 6,
required: true
}
}
};
app.post('/register', (req, res) => {
const validationResult = revalidator.validate(req.body, userSchema);
if (!validationResult.valid) {
return res.status(400).json(validationResult.errors);
}
res.send('User registered successfully!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
This application will validate user input on the `/register` endpoint and respond with appropriate error messages if the input is invalid.
Revalidator is a powerful tool for validating JavaScript objects, and its flexible design makes it suitable for a wide range of applications. From simple data validation to complex custom rules, Revalidator has you covered.
Hash: 48d29418e2fa25a84407d38127f7270ff074eafa5705d8ac7bc1f36a6ce0bcb8