Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
typeof-properties
Advanced tools
typeof-properties
is a module that validates value type of the properties of objects.
of-type
package that checks whether the given value is of particular type (typeof-properties
is based on of-type
package).typeof-arguments
to validate value type of the arguments passed through functions.npm install typeof-properties
const propType = require('typeof-properties');
propType(actual,expected[,callback])
actual
[Object]actual
object will be validated.expected
[Object]expected
object should contain properties, that cohere with the properties of actual
objectexpected
object's properties should indicate the expected type of the coherent properties of actual
objectactual
properties are ommited in expected
object, they will not be validated (can be of any type)expected
TypesThere are three ways to check the type of properties:
null
or undefined
values'null'
, 'undefined'
, or any value equal to constructor.name
, eg: 'string'
, 'number'
, 'regexp'
, 'array'
, 'object'
, 'boolean'
,'buffer'
, etc.'String'
, 'string'
, 'StRiNg'
checks if the property is of type [String].|
. eg: 'array|object'
checks if the property is of type [Array] OR
of type [Object].const actual = {
name: 'Paul',
age: 26
};
const expected = {
name: 'string',
age: 'number|string|undefined'
};
propType(actual, expected);
/null/
, /undefined/
, or any value matching the constructor.name
, eg: /String/
, /Number/
, /RegExp/
, /Array/
, /Object/
, /Boolean/
,/Buffer/
, /Promise/
, etc.i
flag, eg: /string/i
, /regexp/i
, /typeerror/i
(x|y)
expression, eg: /String|Number/
, /TypeError|Error/
/(Type|Range|Syntax)Error/
will match TypeError
, RangeError
and SyntaxError
/[A-Z].+/
will match String
, Array
, but will not match undefined
, null
, etc.const actual = {
name: 'Paul',
age: 26
};
const expected = {
name: /string/i,
age: /Number|String|undefined/
};
propType(actual, expected);
null
, undefined
or any constructor object, eg: String
, TypeError
, Promise
, Array
, etc.[String,Object,Array,null]
const actual = {
name: 'Paul',
age: 26
};
const expected = {
name: String,
age: [Number,String,undefined]
};
propType(actual, expected);
'arguments'
or /arguments/
. It returns true
if the property is defined as the arguments
Object'truthy'
or /truthy/
. It returns true
if the property has the value like: "abc"
, true
, 1
, {}
, []
,function(){}
, etc.'falsy'
or /falsy/
. It returns true
if the property has the value like: ""
, false
, 0
, null
, undefined
, etc.''
or 'any'
or /any/
or []
, It returns true
if the property is of any type.callback
[Function] (optional)Invalid property ["name"]. The [undefined] value has been assigned, while the value of type matching string expression "string|null" is expected.
Invalid property ["name"]. The [undefined] <<falsy>> value has been assigned, while the value of type matching string expression "truthy|null" is expected.
Invalid property ["name"]. The [undefined] value has been assigned, while the value of type matching regular expression /String|null/ is expected.
Invalid property ["name"]. The [undefined] value has been assigned, while the value of type [String|null] is expected.
callback
function.throw
statement!callback
function is executed only if at least one property's value is of invalid type.callback
function with the following properties:
name
"name"
, "age"
actual
"String"
expected
"Array"
, "Boolean|Number"
, "/array|object/i"
message
var propType = require('typeof-properties');
const person = {
name: 'Paul',
age: '27',
experience: 7,
male: true,
skills: ['js','nodejs','mongodb'],
talk: function(){
return `hello I'm ${this.name}`;
}
};
const validation = {
name: 'string',
age: /(number|string)/i,
talk: 'function|falsy',
skills: [Array,Object,null],
male: Boolean
//experience property is ommited - not validated - can be of any type
};
propType(person,validation,(o)=>{
console.error(o.message);
//console.error('Not good! Use ' + o.expected + ' instead of ' + o.actual + ' for the property ' + o.name);
//throw new TypeError('Aborted: ' + o.message);
});
The function propType()
returns true
when all checked properties are of valid types.
The function propType()
returns false
when at least one of the checked properties is of invalid type.
if(!propType(person,validation,()=>console.log('Aborted.'))) return;
> git clone https://github.com/devrafalko/typeof-properties.git
> cd typeof-properties
> npm install
> npm test
> npm test deep //displays error messages
const propType = require('typeof-properties');
const weatherData = {
city: "Warsaw",
latitude: 52.229676,
longitude: 21.012229,
date: new Date('2017-08-22'),
temperature: {day:24,night:18},
humidity: .71,
winter: {kph:18,mph:11.3}
};
const weatherValid = {
city: 'string',
latitude: [Number,String],
longitude: [Number,String],
date: 'date',
temperature: /object|number/i,
humidity: 'number|falsy',
winter: 'object|number'
};
const tempValid = {
day: 'number|undefined',
night: [Number,undefined]
};
const winterValid = {
kph: 'number|falsy',
mph: /number|falsy/i
};
propType(weatherData,weatherValid);
propType(weatherData.temperature,tempValid);
propType(weatherData.winter,winterValid);
FAQs
Validate the type of object's properties.
The npm package typeof-properties receives a total of 533 weekly downloads. As such, typeof-properties popularity was classified as not popular.
We found that typeof-properties 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 uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.