
Research
Security News
Malicious PyPI Package Exploits Deezer API for Coordinated Music Piracy
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
validator.js-validate
Advanced tools
Opinionated object validation function based on validator.js
Opinionated object validation function based on validator.js.
Install the package via yarn
:
$ yarn add validator.js-validate
or via npm
:
$ npm install validator.js-validate --save
This module exports a function that creates a validate function, for instance:
import createValidateFunction from 'validator.js-validate';
const validate = createValidateFunction();
The created validate function works just like validating an object, but replaces the last groups
argument with an options object:
validate(data[Object], constraint[Object|Constraint], options[Object])
mask
(Default: true
)Returns given data masked with given constraint keys:
const data = { foo: 'bar', qux: 'qix' };
const constraint = { foo: is.equalTo('bar') };
console.log(validate(data, constraint));
// { foo: 'bar' }
console.log(validate(data, constraint, { mask: false }));
// true
throws
(Default: true
)Throws a new error when validation fails. To enable this option you must pass an error class when creating the validate function as argument.
This error constructor should be prepared to receive violations as argument, for example:
import StandardError from 'standard-error';
import createValidateFunction from 'validator.js-validate';
class ValidationFailedError extends StandardError {
constructor(errors) {
super({ errors });
}
}
const validate = createValidateFunction(ValidationFailedError);
const data = { foo: 'bar' };
const constraint = { foo: is.equalTo('biz') };
try {
validate(data, constraint);
} catch (e) {
console.log(e);
// ValidationFailedError {
// errors: {
// foo: [{
// __class__: 'Violation',
// assert: {
// __class__: 'EqualTo',
// ...
// }
// }]
// }
// }
}
}
console.log(validate(data, constraint, { throws: false }));
// {
// foo: [{
// __class__: 'Violation',
// assert: {
// __class__: 'EqualTo',
// ...
// }
// }]
// }
groups
Use this option to validate with validation groups:
const data = { foo: 'bar' };
const constraint = { foo: [is('bar').EqualTo('bar'), is('biz').equalTo('biz')] };
console.log(validate(data, constraint, { groups: 'biz' }));
// {
// foo: [{
// __class__: 'Violation',
// assert: {
// __class__: 'EqualTo',
// ...
// }
// }]
// }
console.log(validate(data, constraint, { groups: 'bar' }));
// { foo: 'bar' }
Use the test
script to run the test suite:
$ yarn test
To test check coverage use the cover
script:
$ yarn cover
A full coverage report will be generated on the coverage folder.
$ yarn release [<version> | major | minor | patch]
FAQs
Opinionated object validation function based on validator.js
We found that validator.js-validate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers 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.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.
Security News
Newly introduced telemetry in devenv 1.4 sparked a backlash over privacy concerns, leading to the removal of its AI-powered feature after strong community pushback.