Registry / devops / web3-validator

web3-validator

JSON →
library2.0.6jsnpmunverified

JSON-Schema compatible validator used internally by web3.js v4 for validating addresses, bytes, numbers, and other Ethereum data types. Current version 2.0.6 (stable), part of the web3.js v4 monorepo with a release cadence aligned to web3.js releases. It wraps ajv and adds Ethereum-specific validators. Key differentiator: integrates directly with web3.js 4.x for automatic input validation, but can be used standalone. Compared to @ethersproject/address or @ethereumjs/util, it provides a full JSON Schema validator with custom keywords (e.g., 'bytes', 'address') and supports Ethereum ABI types.

npm install web3-validator
INSTALL
IMPORT
SIG · WEB3-VALIDATOR
W
web3-validator
devopsjavascriptv2.0.6
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Web3Validator
import { Web3Validator } from 'web3-validator'
const { Web3Validator } = require('web3-validator')
ESM-only since v2; CommonJS require will fail. Use dynamic import() if needed.
isAddress
import { isAddress } from 'web3-validator'
import { isAddress } from 'web3-utils'
isAddress was moved from web3-utils to web3-validator in web3@4.x. Using the old import will break.
validate
import { validate } from 'web3-validator'
Default export not available; always use named import.

Creates a Web3Validator instance and uses it to validate an Ethereum address and a hex string against JSON Schema with custom formats.

import { Web3Validator } from 'web3-validator'; const validator = new Web3Validator(); // Validate an Ethereum address const schema = { type: 'string', format: 'address' }; try { validator.validate(schema, '0x742d35Cc6634C0532925a3b844Bc9e7595f9b9c8'); console.log('Valid address'); } catch (error) { console.error('Invalid:', error.message); } // Validate a hex string const hexSchema = { type: 'string', format: 'hexString', minLength: 64, maxLength: 64 }; try { validator.validate(hexSchema, '0x' + 'a'.repeat(64)); console.log('Valid hex'); } catch (error) { console.error('Invalid:', error.message); }
Debug
Known issues
breakingRemoved CommonJS support — ESM-only since v2.0.0. require() will throw Module not found.
fix
Use import syntax or dynamic import('web3-validator').
affects: >=2.0.0
breakingisAddress was moved from web3-utils to web3-validator in web3@4.x. Importing from web3-utils returns undefined.
fix
Import { isAddress } from 'web3-validator'.
affects: >=2.0.0
deprecatedThe validate method with single argument (instance-only) is deprecated; pass schema and data separately.
fix
Use validator.validate(schema, data) instead of validator.validate(data).
affects: >=1.0.0 <3.0.0
gotchaCustom formats like 'address' are only recognized if the format keyword is used; 'pattern' or custom keywords may not validate addresses correctly.
fix
Always use 'format': 'address' for Ethereum address validation.
affects: >=1.0.0
gotchaCross-fetch dependency may cause issues in non-browser/Node environments (e.g., React Native).
fix
Set environment polyfills or use a bundler plugin to handle fetch.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Web3Validator is not a constructor
Using require() instead of import with ESM-only package.
fix
Change to 'import { Web3Validator } from 'web3-validator'.
Error: Cannot find module 'web3-validator'
Package not installed or using CommonJS in Node <14.
fix
Run 'npm install web3-validator' and ensure Node >=14.
ValidationError: Invalid address provided
Passing an invalid Ethereum address to validate with format 'address'.
fix
Ensure the address is a 40-character hex string prefixed with '0x' and checksummed if using EIP-55.
Upgrade
Version history
2.0.6latest on npm
Audit
Dependencies
ethereum-cryptographyrequiredused internally for address/bytes validation
ajvrequiredJSON-Schema validation engine
cross-fetchrequiredrequired for network-related validations
Agent activity
13 hits · last 30 days
node
12
Resources