
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.
mini-type-assert
Advanced tools
Concise type assertions like a<map<fn:s>>. Expressions are parsed just once: it generates functions and caches them. Supports the types of kind-of and has short aliases for most.
const t = require('mini-type-assert')
class Example {
constructor(age, mixed, words, store, opts = {}) {
this.age = t(age, 'n', 'age')
this.mixed = t(mixed, 'a<s|n>', 'mixed')
this.store = t(store, 'map<fn:re>', 'store')
// Spaces are allowed
this.words = t(words, 'a < /^[a-z]+$/ >', 'words')
// Or use placeholders
this.words = t(words, 'a<$>', 'words', /^[a-z]+$/)
// Throw if a is defined and not a boolean
const { a = false } = t(opts, 'o<bool>', 'opts')
}
}
// Instantiate maps with key-value pairs
const goodboy = new Map([ [function(){}, /a/] ])
const badboy = new Map([ ['not a function', /a/] ])
// Okay
new Example(27, [1, 'a'], ['beep'], goodboy, { a: true })
// Throws
new Example('bad', [null], ['BEEP'], badboy, { a: 0 })
If you like ES7 decorators, check out mini-type-decorator.
const t = require('mini-type-decorator')
@t('a<s>', ['n', (n) => n <= 10], 'o<b>')
class Example {
constructor(tags, grade, flags = {}) {
this.tags = tags
}
@t('s')
say(what) {
console.log(what)
}
}
t(value, assertion, name, ...placeholders)The first three arguments are required. Throws if value does not pass assertion. Otherwise, returns value. The name will be used in the error message.
If assertion is a string or regular expression, it will be treated as a type expression (see below). If it's a function, it will receive the value and should return false if the value is invalid. A boolean assertion works like assert(assertion === true). If assertion is an array, each element must pass assertion. For example, to assert that an argument is a number and greater than two: t(arg, ['n', (v) => v > 2 ], 'arg').
a<s|f> is arr<str|fn> is array<string|function>. See the full list.a<n> (an array of numbers). Note that the entire iterable (array, object, map, ..) will be traversed, so use it wisely.map<r:n> (a Map with regular expressions for keys and numbers for values). Again, traversed entirely. Key types are ignored for arrays (can only be numbers) and objects (can only be strings).n|s (a number or string)/\d/ (a string containing at least one number)a < /^x$/i > (an array containing "x" or "X" strings)map < /^[a-z]+$/ : /^[a-z]+$/ > (a Map with lowercase strings for both keys and values)!fn (anything but a function)s | a is s|a. Except within regular expressions, spaces are ignored.s|a<n|b> (a string, or an array containing numbers or booleans)t(1, '$|$', 'arg', 's', 'n') is the same as t(1, 's|n', 'arg')With npm do:
npm install mini-type-assert --save
MIT © Vincent Weevers
FAQs
Concise type assertions like a<map<fn:s>>
We found that mini-type-assert 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.