Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

assert-args

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assert-args - npm Package Compare versions

Comparing version 1.1.1 to 1.1.2

44

index.js

@@ -40,12 +40,9 @@ var debug = require('debug')('assert-args')

debug('is spread key: ' + key)
var requiredOffset = argKeys.slice(i + 1).filter(isRequiredKey).length
spreadArgs = (!requiredOffset || (argKeys.length - i - requiredOffset === 0)) // last arg key
? argsLeft.slice()
: argsLeft.slice(0, argsLeft.length - Math.max(argKeys.length - i - requiredOffset))
debug('required offset: ' + requiredOffset)
debug('argKeys.length', argKeys.length)
debug('argsLeft.length', argsLeft.length)
debug('argKeys.length', argKeys.length)
debug('numArgKeysLeft', (argKeys.length - requiredOffset - i))
debug('numArgToSlice', spreadArgs.length)
var requiredKeysLeft = argKeys.slice(i + 1).filter(isRequiredKey)
debug('requiredKeysLeft', requiredKeysLeft)
spreadArgs = argsLeft.slice(0, argsLeft.length - requiredKeysLeft.length) // copy
debug('spreadArgs', spreadArgs)
debug('spreadArgs.length', spreadArgs.length)

@@ -57,7 +54,6 @@ if (isOptionalKey(key)) {

debug('possible spread args: ' + spreadArgs)
spreadArgs.forEach(function (arg) {
if (!exists(arg)) {
// non-existant args pass as optional args
firstOptionalErr = null
firstOptionalErr = null // reset after a pass
argsLeft.shift() // pass, remains [...]

@@ -73,7 +69,10 @@ return

} catch (err) {
if (firstOptionalErr && argsLeft.length > 1) {
// optional err was thrown before and this is not the last arg
debug('spread validate err: ' + err.message)
debug('spread validate argsLeft: ' + argsLeft)
if (firstOptionalErr) {
// other optional error already occurred, throw first.
throw firstOptionalErr
} else {
firstOptionalErr = err
}
throw err
}

@@ -90,7 +89,6 @@ })

}
debug('possible spread args: ' + spreadArgs)
spreadArgs.forEach(function (arg) {
try {
validate(key, arg, validator, true)
// optional arg passes validator
firstOptionalErr = null

@@ -100,7 +98,15 @@ ret[outKey].push(arg) // pass

} catch (err) {
if (firstOptionalErr && argsLeft.length > 1) {
// optional err was thrown before and this is not the last arg
debug('spread validate err: ' + err.message)
debug('spread validate argsLeft: ' + argsLeft)
debug('spread validate argKeys: ' + argKeys)
if (firstOptionalErr) {
// other optional error already occurred, throw first.
throw firstOptionalErr
} else if (argKeys.slice(i + 1).length === 0) {
// spread assumes all args passed are used.
// there are no args left. and this failed for spread. throw it.
throw err
} else {
firstOptionalErr = err
}
throw err
}

@@ -107,0 +113,0 @@ })

{
"name": "assert-args",
"version": "1.1.1",
"version": "1.1.2",
"description": "Validate and format function arguments ( handles types and optionals)",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -43,2 +43,12 @@ var Code = require('code')

})
it('should not throw for extra args', function (done) {
expect(function () {
var args = [1, 2, 3]
assertArgs(args, {
foo: '*',
bar: '*'
})
}).to.not.throw()
done()
})
})

@@ -352,21 +362,20 @@

describe('optional', function () {
describe('leading', function () {
var fn = function () {}
var fn = function () {}
describe('only', function () {
var validArgs = [
[fn],
[null, fn],
[undefined, fn],
[undefined, undefined, undefined, fn],
[10, fn],
[10, 10, fn],
[10, 10, 10, fn]
[],
[null],
[undefined, undefined],
[10],
[10, 10],
[10, 10, 10]
]
var expectedResults = [
{ foo: [], bar: fn },
{ foo: [], bar: fn },
{ foo: [], bar: fn },
{ foo: [], bar: fn },
{ foo: [10], bar: fn },
{ foo: [10, 10], bar: fn },
{ foo: [10, 10, 10], bar: fn }
{ foo: [] },
{ foo: [] },
{ foo: [] },
{ foo: [10] },
{ foo: [10, 10] },
{ foo: [10, 10, 10] }
]

@@ -377,4 +386,3 @@ validArgs.forEach(function (args, i) {

var out = assertArgs(args, {
'[...foo]': 'number',
'bar': 'function'
'[...foo]': 'number'
})

@@ -387,13 +395,7 @@ expect(out).to.deep.equal(expectedResults[i])

var invalidArgs = [
[null, null, 'no'],
['no', fn],
[{}, 10, 'no'],
[{}, 10, 10, 'no'],
[{}, 10, 'no', fn]
['no'],
[null, 'no']
]
var expectedErrors = [
'"bar" must be a function',
'"qux" must be an object',
'"bar" must be a function',
'"bar" must be a function',
'"...foo" must be numbers',
'"...foo" must be numbers'

@@ -406,5 +408,3 @@ ]

assertArgs(args, {
'[qux]': 'object',
'[...foo]': 'number',
'bar': 'function'
'[...foo]': 'number'
})

@@ -417,53 +417,226 @@ }).to.throw(TypeError, expectedErrors[i])

describe('leading', function () {
describe('required', function () {
var validArgs = [
[fn],
[null, fn],
[undefined, fn],
[undefined, undefined, undefined, fn],
[10, fn],
[10, 10, fn],
[10, 10, 10, fn]
]
var expectedResults = [
{ foo: [], bar: fn },
{ foo: [], bar: fn },
{ foo: [], bar: fn },
{ foo: [], bar: fn },
{ foo: [10], bar: fn },
{ foo: [10, 10], bar: fn },
{ foo: [10, 10, 10], bar: fn }
]
validArgs.forEach(function (args, i) {
// generated test
it('should return args if validations pass: ' + i, function (done) {
var out = assertArgs(args, {
'[...foo]': 'number',
'bar': 'function'
})
expect(out).to.deep.equal(expectedResults[i])
done()
})
})
var invalidArgs = [
[null, null, 'no'],
['no', fn],
[{}, 10, 'no'],
[{}, 10, 10, 'no'],
[{}, 10, 'no', fn]
]
var expectedErrors = [
'"bar" must be a function',
'"qux" must be an object',
'"bar" must be a function',
'"bar" must be a function',
'"...foo" must be numbers'
]
invalidArgs.forEach(function (args, i) {
// generated test
it('should throw: ' + i, function (done) {
expect(function () {
assertArgs(args, {
'[qux]': 'object',
'[...foo]': 'number',
'bar': 'function'
})
}).to.throw(TypeError, expectedErrors[i])
done()
})
})
})
describe('optional', function () {
var fn = function () {}
var validArgs = [
[],
[null],
[null, null],
[fn],
[null, fn],
[undefined, fn],
[undefined, undefined, undefined, fn],
[undefined, undefined, undefined, null],
[10, fn],
[10, 10, fn],
[10, 10, 10, fn]
]
var expectedResults = [
{ foo: [], bar: undefined },
{ foo: [], bar: undefined },
{ foo: [], bar: undefined },
{ foo: [], bar: fn },
{ foo: [], bar: fn },
{ foo: [], bar: fn },
{ foo: [], bar: fn },
{ foo: [], bar: undefined },
{ foo: [10], bar: fn },
{ foo: [10, 10], bar: fn },
{ foo: [10, 10, 10], bar: fn }
]
validArgs.forEach(function (args, i) {
// generated test
it('should return args if validations pass: ' + i, function (done) {
var out = assertArgs(args, {
'[...foo]': 'number',
'[bar]': 'function'
})
expect(out).to.deep.equal(expectedResults[i])
done()
})
})
var invalidArgs = [
[null, null, 'no'],
['no', fn],
[{}, 10, 'no'],
[{}, 10, 10, 'no'],
[{}, 10, 'no', fn]
]
var expectedErrors = [
'"...foo" must be numbers',
'"qux" must be an object',
'"...foo" must be numbers',
'"...foo" must be numbers',
'"...foo" must be numbers'
]
invalidArgs.forEach(function (args, i) {
// generated test
it('should throw: ' + i, function (done) {
expect(function () {
assertArgs(args, {
'[qux]': 'object',
'[...foo]': 'number',
'[bar]': 'function'
})
}).to.throw(TypeError, expectedErrors[i])
done()
})
})
})
})
describe('trailing', function () {
var fn = function () {}
var validArgs = [
[fn],
[fn, null],
[fn, undefined],
[fn, undefined, undefined, undefined],
[fn, 10],
[fn, 10, 10],
[fn, 10, 10, 10]
]
var expectedResults = [
{ foo: fn, bar: [] },
{ foo: fn, bar: [] },
{ foo: fn, bar: [] },
{ foo: fn, bar: [] },
{ foo: fn, bar: [10] },
{ foo: fn, bar: [10, 10] },
{ foo: fn, bar: [10, 10, 10] }
]
validArgs.forEach(function (args, i) {
// generated test
it('should return args if validations pass: ' + i, function (done) {
var out = assertArgs(args, {
'foo': 'function',
'[...bar]': 'number'
describe('required', function () {
var fn = function () {}
var validArgs = [
[fn],
[fn, null],
[fn, undefined],
[fn, undefined, undefined, undefined],
[fn, 10],
[fn, 10, 10],
[fn, 10, 10, 10]
]
var expectedResults = [
{ foo: fn, bar: [] },
{ foo: fn, bar: [] },
{ foo: fn, bar: [] },
{ foo: fn, bar: [] },
{ foo: fn, bar: [10] },
{ foo: fn, bar: [10, 10] },
{ foo: fn, bar: [10, 10, 10] }
]
validArgs.forEach(function (args, i) {
// generated test
it('should return args if validations pass: ' + i, function (done) {
var out = assertArgs(args, {
'foo': 'function',
'[...bar]': 'number'
})
expect(out).to.deep.equal(expectedResults[i])
done()
})
expect(out).to.deep.equal(expectedResults[i])
done()
})
var invalidArgs = [
[null],
[undefined],
[null, 10],
[undefined, 10],
['yes', 'no'],
['yes', null, 'no'],
['yes', undefined, 'no']
]
var expectedErrors = [
'"foo" must be a string',
'"foo" must be a string',
'"foo" must be a string',
'"foo" must be a string',
'"...bar" must be numbers',
'"...bar" must be numbers',
'"...bar" must be numbers'
]
invalidArgs.forEach(function (args, i) {
// generated test
it('should throw: ' + i, function (done) {
expect(function () {
assertArgs(args, {
'foo': 'string',
'[...bar]': 'number'
})
}).to.throw(TypeError, expectedErrors[i])
done()
})
})
})
describe('followed by optional', function () {
describe('optional', function () {
var validArgs = [
[null],
[undefined],
[null, 10],
[undefined, 10],
[10],
[10, 10],
[10, 10, 10],
[10, 10, 10, 10]
[10, 10, 10, 10],
[fn, 10, 10, 10, 10]
]
var expectedResults = [
{ foo: [10], bar: undefined },
{ foo: [10, 10], bar: undefined },
{ foo: [10, 10, 10], bar: undefined },
{ foo: [10, 10, 10, 10], bar: undefined }
{ bar: [], foo: null },
{ bar: [], foo: undefined },
{ bar: [10], foo: null },
{ bar: [10], foo: undefined },
{ bar: [10], foo: undefined },
{ bar: [10, 10], foo: undefined },
{ bar: [10, 10, 10], foo: undefined },
{ bar: [10, 10, 10, 10], foo: undefined },
{ bar: [10, 10, 10, 10], foo: fn }
]
validArgs.slice(0, 1).forEach(function (args, i) {
validArgs.forEach(function (args, i) {
// generated test
it('should return args if validations pass: ' + i, function (done) {
var out = assertArgs(args, {
'[...foo]': 'number',
'[bar]': 'function'
'[foo]': 'function',
'[...bar]': 'number'
})

@@ -474,21 +647,63 @@ expect(out).to.deep.equal(expectedResults[i])

})
var invalidArgs = [
[fn],
['yes', 'no'],
['yes', null, 'no'],
['yes', undefined, 'no']
]
var expectedErrors = [
'"foo" must be a string',
'"...bar" must be numbers',
'"...bar" must be numbers',
'"...bar" must be numbers'
]
invalidArgs.forEach(function (args, i) {
// generated test
it('should throw: ' + i, function (done) {
expect(function () {
assertArgs(args, {
'[foo]': 'string',
'[...bar]': 'number'
})
}).to.throw(TypeError, expectedErrors[i])
done()
})
})
})
})
})
describe('required', function () {
describe('only', function () {
var validArgs = [
[10],
[10, 10],
[10, 10, 10]
]
var expectedResults = [
{ foo: [10] },
{ foo: [10, 10] },
{ foo: [10, 10, 10] }
]
validArgs.forEach(function (args, i) {
// generated test
it('should return args if validations pass: ' + i, function (done) {
var out = assertArgs(args, {
'...foo': 'number'
})
expect(out).to.deep.equal(expectedResults[i])
done()
})
})
var invalidArgs = [
[null],
[undefined],
[null, 10],
[undefined, 10],
['yes', 'no'],
['yes', null, 'no'],
['yes', undefined, 'no']
[],
['no'],
[null, 'no']
]
var expectedErrors = [
'"foo" must be a string',
'"foo" must be a string',
'"foo" must be a string',
'"foo" must be a string',
'"...bar" must be numbers',
'"...bar" must be numbers',
'"...bar" must be numbers'
'"...foo" is required',
'"...foo" must be numbers',
'"...foo" must be numbers'
]

@@ -500,4 +715,3 @@ invalidArgs.forEach(function (args, i) {

assertArgs(args, {
'foo': 'string',
'[...bar]': 'number'
'...foo': 'number'
})

@@ -509,32 +723,60 @@ }).to.throw(TypeError, expectedErrors[i])

})
})
describe('required', function () {
describe('leading', function () {
var fn = function () {}
var validArgs = [
[10, fn],
[10, 10, fn],
[10, 10, 10, fn],
[10, 10, 10, 10, fn]
]
var expectedResults = [
{ foo: [10], bar: fn },
{ foo: [10, 10], bar: fn },
{ foo: [10, 10, 10], bar: fn },
{ foo: [10, 10, 10, 10], bar: fn }
]
validArgs.forEach(function (args, i) {
// generated test
it('should return args if validations pass: ' + i, function (done) {
var out = assertArgs(args, {
'...foo': 'number',
'bar': 'function'
describe('required', function () {
var validArgs = [
[10, fn],
[10, 10, fn],
[10, 10, 10, fn],
[10, 10, 10, 10, fn]
]
var expectedResults = [
{ foo: [10], bar: fn },
{ foo: [10, 10], bar: fn },
{ foo: [10, 10, 10], bar: fn },
{ foo: [10, 10, 10, 10], bar: fn }
]
validArgs.forEach(function (args, i) {
// generated test
it('should return args if validations pass: ' + i, function (done) {
var out = assertArgs(args, {
'...foo': 'number',
'bar': 'function'
})
expect(out).to.deep.equal(expectedResults[i])
done()
})
expect(out).to.deep.equal(expectedResults[i])
done()
})
var invalidArgs = [
[{}, fn],
[{}, 10, 'no'],
['str', 10, fn],
[{}, 10, 10, 'no'],
[{}, 10, 'no', fn]
]
var expectedErrors = [
'"...foo" is required',
'"bar" must be a function',
'"qux" must be an object',
'"bar" must be a function',
'"...foo" must be numbers'
]
invalidArgs.forEach(function (args, i) {
// generated test
it('should throw: ' + i, function (done) {
expect(function () {
assertArgs(args, {
'[qux]': 'object',
'...foo': 'number',
'bar': 'function'
})
}).to.throw(TypeError, expectedErrors[i])
done()
})
})
})
describe('followed by optional', function () {
describe('optional', function () {
var validArgs = [

@@ -544,3 +786,4 @@ [10],

[10, 10, 10],
[10, 10, 10, 10]
[10, 10, 10, 10],
[10, 10, 10, 10, fn]
]

@@ -551,5 +794,6 @@ var expectedResults = [

{ foo: [10, 10, 10], bar: undefined },
{ foo: [10, 10, 10, 10], bar: undefined }
{ foo: [10, 10, 10, 10], bar: undefined },
{ foo: [10, 10, 10, 10], bar: fn }
]
validArgs.slice(0, 1).forEach(function (args, i) {
validArgs.forEach(function (args, i) {
// generated test

@@ -566,30 +810,2 @@ it('should return args if validations pass: ' + i, function (done) {

})
var invalidArgs = [
[{}, fn],
[{}, 10, 'no'],
['str', 10, fn],
[{}, 10, 10, 'no'],
[{}, 10, 'no', fn]
]
var expectedErrors = [
'"...foo" is required',
'"bar" must be a function',
'"qux" must be an object',
'"bar" must be a function',
'"...foo" must be numbers'
]
invalidArgs.forEach(function (args, i) {
// generated test
it('should throw: ' + i, function (done) {
expect(function () {
assertArgs(args, {
'[qux]': 'object',
'...foo': 'number',
'bar': 'function'
})
}).to.throw(TypeError, expectedErrors[i])
done()
})
})
})

@@ -630,14 +846,14 @@

var expectedErrors = [
'"...foo" is required',
'"bar" must be a function',
'"bar" must be a function',
'"...foo" must be numbers'
'"foo" must be a number',
'"...bar" must be functions',
'"...bar" must be functions',
'"...bar" must be functions'
]
invalidArgs.forEach(function (args, i) {
// generated test
it('should throw: ' + i, function (done) {
it('should throw: ' + i + ' ' + args, function (done) {
expect(function () {
assertArgs(args, {
'...foo': 'number',
'bar': 'function'
'foo': 'number',
'...bar': 'function'
})

@@ -644,0 +860,0 @@ }).to.throw(TypeError, expectedErrors[i])

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc