Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
validate
✓ import validate from 'validate.js';
✗ import { validate } from 'validate.js';
The primary validation function `validate` is the default export. For CommonJS, use `const validate = require('validate.js');` directly.
validate.extend
✓ import validate from 'validate.js';
validate.extend(validate.validators, {
// custom validator
});
Extension methods and utility functions are attached as properties to the default `validate` object.
Constraints
✓ import type { Constraints, Attributes, Errors } from 'validate.js';
TypeScript types for defining constraints, attributes, and expected error structures are exported directly from the package.
This quickstart demonstrates how to define validation constraints for a user object and use the default `validate` function to check the data integrity, handling both success and failure cases.
import validate from 'validate.js';
interface UserData {
email?: string;
password?: string;
passwordConfirmation?: string;
age?: number;
}
const user: UserData = {
email: 'test@example.com',
password: 'Password123!',
passwordConfirmation: 'Password123!',
age: 25
};
const constraints = {
email: {
presence: true,
email: true
},
password: {
presence: true,
length: { minimum: 8, message: 'must be at least 8 characters' },
format: {
pattern: '[a-zA-Z0-9!@#$%^&*()_+]{8,}',
message: 'can only contain letters, numbers, and common symbols'
}
},
passwordConfirmation: {
equality: 'password'
},
age: {
numericality: {
onlyInteger: true,
greaterThanOrEqualTo: 18,
message: 'must be an integer 18 or older'
},
type: 'integer' // New type validator from 0.13.1
}
};
const errors = validate(user, constraints);
if (errors) {
console.error('Validation failed:\n', JSON.stringify(errors, null, 2));
} else {
console.log('Validation successful!');
}
// Example of invalid data
const invalidUser: UserData = {
email: 'invalid-email',
password: 'short',
passwordConfirmation: 'mismatch',
age: 17.5
};
const invalidErrors = validate(invalidUser, constraints);
if (invalidErrors) {
console.error('\nInvalid user validation failed as expected:\n', JSON.stringify(invalidErrors, null, 2));
}
Errors
Common errors & fixes
TypeError: validate is not a function
Attempting to destructure the default export using named import syntax (e.g., `import { validate } from 'validate.js';`) instead of a default import.
fixUse a default import: `import validate from 'validate.js';`
ReferenceError: validate is not defined
This error often occurs in a browser environment when `validate.js` is included via a script tag, but the global `validate` object is not accessible, or in Node.js when the module is not correctly imported.
fixEnsure the script tag is loaded before your code, or if using CommonJS/ESM, verify the `require` or `import` statement is at the top of your file: `const validate = require('validate.js');` or `import validate from 'validate.js';`. TypeError: Cannot read properties of undefined (reading 'validators')
This typically happens when trying to access or extend `validate.validators` or other properties before the main `validate` object has been properly imported or initialized.
fixConfirm that `validate` is correctly imported as a default export before attempting to access its properties or methods, e.g., `import validate from 'validate.js'; validate.extend(...);`
Audit
Dependencies
No dependency data recorded yet.