
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.
v2.1.2
npm install --save 2valid
Check for integer
var v = require('2valid');
console.log( v.validate('integer', 111) ); //null
console.log( v.validate('integer', 'aaa') ); // { notMatched: 'integer' }
Check for password. Minimum 4 chars, at least one caps and one small letter, digit and special
var v = require('2valid');
console.log( v.validate('password', 'As1eRR') ); // { notMatched: 'password' }
console.log( v.validate('password', '!A1e') ); // null
type | description |
---|---|
string | string |
integer | integer number |
float | float number |
date | date methods |
simple e-mail | |
password | password, minimum 4 chars, at least one caps and one small letter, digit and special |
md5 | MD5 string |
uuid | UUID string |
If validate passed, then result is null
.
Otherwise validate result is object with these keys:
notMatched
[object] - which key does not match which typenotRequired
[array of string] - list of keys are not in modelnotFound
[array of string] - which keys in model are not found in checked objecttext
[string] - simple description of all errorsFor example:
{ notMatched: { '.id': 'integer' },
text: 'Field .id not matched with type integer. Field .secondName not required. Field .name not found',
notRequired: [ '.secondName' ],
notFound: [ '.name' ] }
Check if integer
var v = require('2valid');
v.validate( 'integer', 111, function(err) {
console.log(err); //null
});
v.validate( 'integer', '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', function(err) {
console.log(err); // { notMatched: 'integer' }
});
Check if valid to simple object with integer id
and string name
var vm = require('./index');
var userModel = {
id: {type: 'integer'},
name: {type: 'string', required: true}
};
vm.validate( userModel,
{ id: 123, secondName: 'Max Validator' },
function(err) {
console.log(err); // null
}
);
Validation failed with all types of errors
var vm = require('./index');
var userModel = {
id: {type: 'integer'},
name: {type: 'string', required: true}
};
vm.validate( userModel,
{ id: 'Max', secondName: 'Validator' },
function(err) {
console.log(err);
}
);
Result
{ notMatched: { '.id': 'integer' },
text: 'Field .id not matched with type integer. Field .secondName not required. Field .name not found',
notRequired: [ '.secondName' ],
notFound: [ '.name' ] }
Rregister user
model to check validation by model name
var v = require('2valid');
// register model 'user' to check integer 'id' and string 'name'
v.registerModel('user', {
id: {type: 'integer'},
name: {type: 'string'}
});
// object to validate
var userObject = {id: 123, name: 'Alex Validates'}
// check if object is valid sync
var valid = v.validate('user', userObject);
console.log(valid.text || 'object is valid');
// check if object is valid with callback
v.validate('user', userObject, function(err) {
console.log(err || 'object is valid');
});
Result
object is valid
Validate name.first
and name.second
var v = require('2valid');
// property 'name' of 'user' model must have 'first' (required) and 'last' keys.
v.registerModel('user', {
name: {
first : { type: 'string', required: true },
last : { type: 'string' },
}
});
// {}
console.log(v.validate('user', {name: {first: 'Alex', last: 'Validator'}}));
console.log(v.validate('user', {name: {first: 'Marry'}}));
// { notFound: [ '.id', '.name.first' ],
// text: 'Field .id not found in registered model. Field .name.first not found in registered model' }
console.log(v.validate('user', {id: 123}));
// { notFound: [ '.name.first' ], text: 'Field .name.first not found in registered model' }
console.log(v.validate('user', {name: {last: 'Alex'}}));
Only 'cyan', 'magenta', 'yellow' or 'key' can passed in cmyk
model to validate
var v = require('2valid');
// property 'name' of 'cmyk' model must be 'cyan', 'magenta', 'yellow' or 'key' value
v.registerModel('cmyk', {name: { type: 'string', match : /^cyan|magenta|yellow|key$/i }});
// {}
console.log(v.validate('cmyk', {name: 'Magenta'}));
// { notMatched: { '.name': 'string' }, text: 'Field .name not matched with type string' }
console.log(v.validate('cmyk', {name: 'black'}));
console.log(v.validate('cmyk', {name: 123}));
Check if field is required
var v = require('2valid');
// property “id” required and must be uuid
v.registerModel('user', {id: { type: 'uuid', required: true }});
// {}
console.log(v.validate('user', {id: '61cecfb4-da43-4b65-aaa0-f1c3be81ec53'}));
// { notMatched: { '.id': 'uuid' }, text: 'Field .id not matched with type uuid' }
console.log(v.validate('user', {id: 123}));
// { notFound: [ '.name', '.id' ],
// text: 'Field .name not found in registered model. Field .id not found in registered model' }
console.log(v.validate('user', {name: 'Alex'}));
Checking for fixed length of the string
var v = require('2valid');
// property “name” must be exacly 2 chars length
v.registerModel('ISO 3166-2', {name: { type: 'string', min: 2, max: 2 }});
// {}
console.log(v.validate('ISO 3166-2', {name: 'US'}));
// { notMatched: { '.name': 'string' }, text: 'Field .name not matched with type string' }
console.log(v.validate('ISO 3166-2', {name: 123}));
console.log(v.validate('ISO 3166-2', {name: 'USA'}));
console.log(v.validate('ISO 3166-2', {name: 'U'}));
You can add new type to validate in to types.js. 'check' method is required to check new inserted type.
For example, new type 'password'. It type must contains minimum 4 chars: at least one lower and one upper case, digit and special chars. Add code below to types.js in list property:
password : {
min : 4, // string.min Minimum length of the string
max : Infinity, // string.max Maximum length of the string
check : function( password ){ // check password type and size
if ( ( typeof string === 'string' || string instanceof String )
&& string.length >= this.min
&& string.length <= this.max
&& string.match(/((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).+)/)
)
return true
else
return false;
}
}
npm test
For register model you need to use registerModel method.
v.registerModel( 'user', {
id: { type: 'uuid', required: true }, // property “id” must be uuid
name: { type: 'string', min: 4, max: 128, required: true }, // property “name” must be String and contain 4-128
password: { type: 'password', max: 128, required: true }, // property “password” must be String and contain 4-128 chars:
});
v.validate( 'user', { id : '61cecfb4-da33-4b15-aa10-f1c6be81ec53', name : 'Validator', password : 'A1z!' })
myLibrary.registerModel( 'Name', { id: { type: 'uuid', required: true } } );
myLibrary.registerModel( 'modelName', NaN );
myLibrary.registerModel( 'modelName', { id: { type: 'uuid', min: 1, max: 5, required: true } } );
myLibrary.registerModel( 'modelName', { id: { type: 'name' } } );
myLibrary.consoleTrueOrError ( myLibrary.validate( 'modelName', { name : 'Alex Validates' }) );
myLibrary.registerModel( 'name_exception', { date: { parameter: 'date' } } );
myLibrary.registerModel( 'name_exception', { id: { type: 'guid' } } );
Dimitry, 2@ivanoff.org.ua
curl -A cv ivanoff.org.ua
FAQs
JavaScript simple data validator
The npm package 2valid receives a total of 13 weekly downloads. As such, 2valid popularity was classified as not popular.
We found that 2valid 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.
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.