
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
form is a lightweight Node.js library designed for form validation and data filtering. It leverages async to handle validation and filtering tasks asynchronously and uses validator to check and sanitize form inputs.
validator for common validation and filtering tasks (e.g., checking if a field is empty, matching a regex pattern, trimming, etc.).npm install form
const form = require('form');
// Define validation rules and filters for fields
const fields = {
name: [
form.filter(form.Filter.trim), // Trim whitespace
form.validator(form.Validator.notEmpty, 'Empty name'), // Name cannot be empty
form.validator(form.Validator.is, /[0-9]+/, 'Bad name') // Must contain numbers
],
text: [
form.filter(form.Filter.trim), // Trim whitespace
form.validator(form.Validator.notEmpty, 'Empty text'), // Text cannot be empty
form.validator(form.Validator.len, 30, 1000, 'Bad text length') // Text must be between 30 and 1000 characters
]
};
// Create a form instance with defined fields
const textForm = form.create(fields);
// Process incoming form data
textForm.process({ text: 'some short text', name: 'tester' }, (error, data) => {
if (error) {
console.log('Errors:', error); // Print any validation errors
} else {
console.log('Valid data:', data); // Print valid form data
}
});
Fields are defined as an object where each key is the name of the form field and its value is an array of filters and validators applied in sequence.
const fields = {
email: [
form.filter(form.Filter.trim), // Trim whitespace from the email
form.validator(form.Validator.isEmail, 'Invalid email address') // Ensure the field is a valid email address
],
age: [
form.filter(form.Filter.toInt), // Convert age to an integer
form.validator(form.Validator.isInt, { min: 18 }, 'Must be 18 or older') // Ensure age is at least 18
]
};
The process function validates and filters incoming data, returning the sanitized data or any validation errors.
textForm.process({ field1: 'some input' }, function (error, data) {
if (error) {
console.log('Validation errors:', error);
} else {
console.log('Sanitized data:', data);
}
});
You can also create custom filters and validators.
form.filter(function (fields, field, callback) {
fields[field] = fields[field].toUpperCase(); // Convert input to uppercase
callback();
});
form.validator(function (fields, field, callback) {
if (fields[field] !== 'customValue') {
callback('Invalid value');
} else {
callback();
}
}, 'Invalid value');
form.create(fields)Creates a new Form instance with the given fields and rules.
fields: An object where each key represents a form field, and its value is an array of filters and validators.form.process(request, callback)Processes incoming form data.
request: The form data to validate and filter (usually from an HTTP request).callback(error, data): A callback that receives any validation errors and the sanitized form data.form.Filter.trim: Trims whitespace from a string.form.Filter.toInt: Converts a string to an integer.form.Validator.notEmpty: Ensures the field is not empty.form.Validator.isEmail: Validates that the field contains a valid email address.form.Validator.isInt(options): Ensures the field is an integer (with optional range options).form.Validator.len(min, max): Ensures the field length is between min and max.FAQs
Form processor for filter and validation form data
The npm package form receives a total of 2,244 weekly downloads. As such, form popularity was classified as popular.
We found that form demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.