
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
value-validator
Advanced tools
Low-level rule manager for validating values.
$ npm install value-validator --save
const Validator = require('value-validator')
const validator = new Validator([
/.{4,}/,
/^[a-z0-9_]+$/i,
function (v) {
return checkExistsPromise(v)
}
])
validator
.valiate(value)
.then((result) => {
result // whether passes the validation
})
.catch((err) => {
err // if any error throws or returns
})
// Examples
validator.validate('foo').then(pass => {
pass // false, to short
})
validator.validate('foo.bar').then(pass => {
pass // false, only letters, numbers and underscores.
})
validator.validate('steve').catch(err => {
err // maybe `new Error('username "steve" already exists.')`
})
RegExp|function()|String|Array.<mixed> rule could be
Promise or normal valueany value to be validatedfunction(err, pass)= using callback is deprecated since 2.2.0, and the parameter will be removed in the next major version.returns a Promise if no callback, or undefined
Object specify this object for all validator functions.Returns this
ruleThe function should accept only one argument, which is the value to be validated.
If the function returns a Boolean, it indicates whether the validation is passed, and the err will be null
const validator = new Validator(v => v > 10)
validator.validate(5).then(pass => {
pass // false
})
If the function returns an Error, it means the validation fails, and the error will passed to the callback function of validate(v, callback) as the first parameter err.
const validator = new Validator(v => {
if (v > 10) {
return true
}
return new Error('should larger than 10')
})
validator.validate(5).catch(err => {
err // new Error('should larger than 10')
})
To define an asynchronous validator, just returns a Promise.
Pre-defines certain option of Validator and returns a constructor.
Validator presets are an abbreviation of a certain validation, or a set of validations.
const presets = {
// To define a function-typed preset
unique: function (v) {
return new Promise((resolve, reject) => {
asyncCheckExists(v, exists => {
if (exists) {
return reject(new Error(`username "${v}" already exists.`))
}
resolve(true)
})
})
},
min4: /.{4,}/,
// A preset could be a set of presets.
username: [
'min4',
/^[a-z0-9_]+$/i,
'unique'
]
}
const MyValidator = Validator.defaults({presets})
// Then we could use `username` as the test rule.
const validator = new MyValidator('username')
MIT
FAQs
Low-level rule manager for validating values.
We found that value-validator 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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.