{"id":12342,"library":"validate.js","title":"Validate.js: Declarative JavaScript Validations","description":"validate.js provides a declarative, schema-based approach for validating JavaScript objects, suitable for both client-side and server-side applications. The current stable version is 0.13.1. While the release cadence appears to be moderate, the library has a long-standing history and focuses on stability and a comprehensive set of built-in validators, including new additions like a type validator. Its key differentiator lies in its concise, constraint-based syntax, allowing developers to define complex validation rules with minimal boilerplate. It is often chosen for projects requiring robust data integrity checks without external framework dependencies, and it ships with TypeScript types for improved developer experience and static analysis.","status":"maintenance","version":"0.13.1","language":"javascript","source_language":"en","source_url":"https://github.com/ansman/validate.js","tags":["javascript","validation","validate","server","client","typescript"],"install":[{"cmd":"npm install validate.js","lang":"bash","label":"npm"},{"cmd":"yarn add validate.js","lang":"bash","label":"yarn"},{"cmd":"pnpm add validate.js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary validation function `validate` is the default export. For CommonJS, use `const validate = require('validate.js');` directly.","wrong":"import { validate } from 'validate.js';","symbol":"validate","correct":"import validate from 'validate.js';"},{"note":"Extension methods and utility functions are attached as properties to the default `validate` object.","symbol":"validate.extend","correct":"import validate from 'validate.js';\nvalidate.extend(validate.validators, {\n  // custom validator\n});"},{"note":"TypeScript types for defining constraints, attributes, and expected error structures are exported directly from the package.","symbol":"Constraints","correct":"import type { Constraints, Attributes, Errors } from 'validate.js';"}],"quickstart":{"code":"import validate from 'validate.js';\n\ninterface UserData {\n  email?: string;\n  password?: string;\n  passwordConfirmation?: string;\n  age?: number;\n}\n\nconst user: UserData = {\n  email: 'test@example.com',\n  password: 'Password123!',\n  passwordConfirmation: 'Password123!',\n  age: 25\n};\n\nconst constraints = {\n  email: {\n    presence: true,\n    email: true\n  },\n  password: {\n    presence: true,\n    length: { minimum: 8, message: 'must be at least 8 characters' },\n    format: {\n      pattern: '[a-zA-Z0-9!@#$%^&*()_+]{8,}',\n      message: 'can only contain letters, numbers, and common symbols'\n    }\n  },\n  passwordConfirmation: {\n    equality: 'password'\n  },\n  age: {\n    numericality: { \n      onlyInteger: true, \n      greaterThanOrEqualTo: 18, \n      message: 'must be an integer 18 or older'\n    },\n    type: 'integer' // New type validator from 0.13.1\n  }\n};\n\nconst errors = validate(user, constraints);\n\nif (errors) {\n  console.error('Validation failed:\\n', JSON.stringify(errors, null, 2));\n} else {\n  console.log('Validation successful!');\n}\n\n// Example of invalid data\nconst invalidUser: UserData = {\n  email: 'invalid-email',\n  password: 'short',\n  passwordConfirmation: 'mismatch',\n  age: 17.5\n};\n\nconst invalidErrors = validate(invalidUser, constraints);\nif (invalidErrors) {\n  console.error('\\nInvalid user validation failed as expected:\\n', JSON.stringify(invalidErrors, null, 2));\n}","lang":"typescript","description":"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."},"warnings":[{"fix":"Upgrade `validate.js` to version `0.13.1` or newer.","message":"Versions prior to 0.13.1 might inadvertently leak variables to the global namespace when used in certain environments due to a missing 'var' keyword. It's recommended to upgrade to 0.13.1 or later to avoid potential global scope pollution.","severity":"gotcha","affected_versions":"<0.13.1"},{"fix":"Upgrade `validate.js` to version `0.13.1` or newer to ensure accurate email validation.","message":"Earlier versions contained issues with the email validation logic that could lead to incorrect validation results (e.g., falsely validating invalid emails or rejecting valid ones). These issues were addressed in version 0.13.1.","severity":"gotcha","affected_versions":"<0.13.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use a default import: `import validate from 'validate.js';`","cause":"Attempting to destructure the default export using named import syntax (e.g., `import { validate } from 'validate.js';`) instead of a default import.","error":"TypeError: validate is not a function"},{"fix":"Ensure 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';`.","cause":"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.","error":"ReferenceError: validate is not defined"},{"fix":"Confirm 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(...);`","cause":"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.","error":"TypeError: Cannot read properties of undefined (reading 'validators')"}],"ecosystem":"npm"}