Comparing version 1.0.1 to 1.0.2
48
index.js
@@ -1,19 +0,19 @@ | ||
"use strict" | ||
'use strict' | ||
var types = { | ||
"*": ["any", function () { return true }], | ||
A: ["array", function (thingy) { return thingy instanceof Array || (typeof thingy === "object" && thingy.hasOwnProperty("callee")) }], | ||
S: ["string", function (thingy) { return typeof thingy === "string" }], | ||
N: ["number", function (thingy) { return typeof thingy === "number" }], | ||
F: ["function", function (thingy) { return typeof thingy === "function" }], | ||
O: ["object", function (thingy) { return typeof thingy === "object" && !types.A[1](thingy) && !types.E[1](thingy) }], | ||
B: ["boolean", function (thingy) { return typeof thingy == "boolean" }], | ||
E: ["error", function (thingy) { return thingy instanceof Error }] | ||
'*': ['any', function () { return true }], | ||
A: ['array', function (thingy) { return (Array.isArray && Array.isArray(thingy)) || (typeof thingy === 'object' && thingy.hasOwnProperty('callee')) }], | ||
S: ['string', function (thingy) { return typeof thingy === 'string' }], | ||
N: ['number', function (thingy) { return typeof thingy === 'number' }], | ||
F: ['function', function (thingy) { return typeof thingy === 'function' }], | ||
O: ['object', function (thingy) { return typeof thingy === 'object' && !types.A[1](thingy) && !types.E[1](thingy) }], | ||
B: ['boolean', function (thingy) { return typeof thingy === 'boolean' }], | ||
E: ['error', function (thingy) { return thingy instanceof Error }] | ||
} | ||
var validate = module.exports = function (schema, args) { | ||
if (!schema) throw missingRequiredArg(0, "schema") | ||
if (!args) throw missingRequiredArg(1, "args") | ||
if (!types.S[1](schema)) throw invalidType(0, "string", schema) | ||
if (!types.A[1](args)) throw invalidType(1, "array", args) | ||
if (!schema) throw missingRequiredArg(0, 'schema') | ||
if (!args) throw missingRequiredArg(1, 'args') | ||
if (!types.S[1](schema)) throw invalidType(0, 'string', schema) | ||
if (!types.A[1](args)) throw invalidType(1, 'array', args) | ||
for (var ii = 0; ii < schema.length; ++ii) { | ||
@@ -24,6 +24,6 @@ var type = schema[ii] | ||
var typeCheck = types[type][1] | ||
if (type === "E" && args[ii] == null) continue | ||
if (type === 'E' && args[ii] == null) continue | ||
if (args[ii] == null) throw missingRequiredArg(ii) | ||
if (!typeCheck(args[ii])) throw invalidType(ii, typeLabel, args[ii]) | ||
if (type === "E") return | ||
if (type === 'E') return | ||
} | ||
@@ -33,19 +33,19 @@ if (schema.length < args.length) throw tooManyArgs(schema.length, args.length) | ||
function missingRequiredArg(num) { | ||
return newException("EMISSINGARG", "Missing required argument #"+(num+1)) | ||
function missingRequiredArg (num) { | ||
return newException('EMISSINGARG', 'Missing required argument #' + (num + 1)) | ||
} | ||
function unknownType(num, type) { | ||
return newException("EUNKNOWNTYPE", "Unknown type "+type+" in argument #"+(num+1)) | ||
function unknownType (num, type) { | ||
return newException('EUNKNOWNTYPE', 'Unknown type ' + type + ' in argument #' + (num + 1)) | ||
} | ||
function invalidType(num, type, value) { | ||
return newException("EINVALIDTYPE", "Argument #"+(num+1)+": Expected "+type+" but got "+typeof value) | ||
function invalidType (num, type, value) { | ||
return newException('EINVALIDTYPE', 'Argument #' + (num + 1) + ': Expected ' + type + ' but got ' + typeof value) | ||
} | ||
function tooManyArgs(expected, got) { | ||
return newException("ETOOMANYARGS", "Too many arguments, expected "+expected+" and got "+got) | ||
function tooManyArgs (expected, got) { | ||
return newException('ETOOMANYARGS', 'Too many arguments, expected ' + expected + ' and got ' + got) | ||
} | ||
function newException(code, msg) { | ||
function newException (code, msg) { | ||
var e = new Error(msg) | ||
@@ -52,0 +52,0 @@ e.code = code |
{ | ||
"name": "aproba", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A rediculously light-weight argument validator", | ||
@@ -11,6 +11,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tap": "^0.7.0" | ||
"standard": "^7.1.0", | ||
"tap": "^5.7.1" | ||
}, | ||
"scripts": { | ||
"test": "tap test/*.js" | ||
"test": "standard && tap test/*.js" | ||
}, | ||
@@ -17,0 +18,0 @@ "repository": { |
@@ -26,3 +26,3 @@ aproba | ||
* | matches any type | ||
A | instanceof Array OR an arguments object | ||
A | Array.isArray OR an arguments object | ||
S | typeof == string | ||
@@ -29,0 +29,0 @@ N | typeof == number |
@@ -1,12 +0,11 @@ | ||
"use strict" | ||
var test = require("tap").test | ||
var validate = require("../index.js") | ||
'use strict' | ||
var test = require('tap').test | ||
var validate = require('../index.js') | ||
function thrown (t, code, msg, todo) { | ||
validate("OSSF", arguments) | ||
validate('OSSF', arguments) | ||
try { | ||
todo() | ||
t.fail(msg) | ||
} | ||
catch (e) { | ||
} catch (e) { | ||
t.is(e.code, code, msg + e.message) | ||
@@ -17,22 +16,21 @@ } | ||
function notThrown (t, msg, todo) { | ||
validate("OSF", arguments) | ||
validate('OSF', arguments) | ||
try { | ||
todo() | ||
t.pass(msg) | ||
} catch (e) { | ||
t.fail(msg + '\n' + e.stack) | ||
} | ||
catch (e) { | ||
t.fail(msg+"\n"+e.stack) | ||
} | ||
} | ||
test("general", function (t) { | ||
test('general', function (t) { | ||
t.plan(69) | ||
var values = { | ||
"A": [], | ||
"S": "test", | ||
"N": 123, | ||
"F": function () {}, | ||
"O": {}, | ||
"B": false, | ||
"E": new Error() | ||
'A': [], | ||
'S': 'test', | ||
'N': 123, | ||
'F': function () {}, | ||
'O': {}, | ||
'B': false, | ||
'E': new Error() | ||
} | ||
@@ -42,8 +40,7 @@ Object.keys(values).forEach(function (type) { | ||
if (type === contraType) { | ||
notThrown(t, type + " matches " + contraType, function () { | ||
notThrown(t, type + ' matches ' + contraType, function () { | ||
validate(type, [values[contraType]]) | ||
}) | ||
} | ||
else { | ||
thrown(t, "EINVALIDTYPE", type + " does not match " + contraType, function () { | ||
} else { | ||
thrown(t, 'EINVALIDTYPE', type + ' does not match ' + contraType, function () { | ||
validate(type, [values[contraType]]) | ||
@@ -53,9 +50,8 @@ }) | ||
}) | ||
if (type === "E") { | ||
notThrown(t, "null is ok for E", function () { | ||
if (type === 'E') { | ||
notThrown(t, 'null is ok for E', function () { | ||
validate(type, [null]) | ||
}) | ||
} | ||
else { | ||
thrown(t, "EMISSINGARG", "null not ok for "+type, function () { | ||
} else { | ||
thrown(t, 'EMISSINGARG', 'null not ok for ' + type, function () { | ||
validate(type, [null]) | ||
@@ -66,24 +62,24 @@ }) | ||
Object.keys(values).forEach(function (contraType) { | ||
notThrown(t, "* matches " + contraType, function () { | ||
validate("*", [values[contraType]]) | ||
notThrown(t, '* matches ' + contraType, function () { | ||
validate('*', [values[contraType]]) | ||
}) | ||
}) | ||
thrown(t, "EMISSINGARG", "not enough args", function () { | ||
validate("SNF", ["abc", 123]) | ||
thrown(t, 'EMISSINGARG', 'not enough args', function () { | ||
validate('SNF', ['abc', 123]) | ||
}) | ||
thrown(t, "ETOOMANYARGS", "too many args", function () { | ||
validate("SNF", ["abc", 123, function () {}, true]) | ||
thrown(t, 'ETOOMANYARGS', 'too many args', function () { | ||
validate('SNF', ['abc', 123, function () {}, true]) | ||
}) | ||
notThrown(t, "E matches null", function () { | ||
validate("E", [null]) | ||
notThrown(t, 'E matches null', function () { | ||
validate('E', [null]) | ||
}) | ||
notThrown(t, "E matches undefined", function () { | ||
validate("E", [undefined]) | ||
notThrown(t, 'E matches undefined', function () { | ||
validate('E', [undefined]) | ||
}) | ||
notThrown(t, "E w/ error requires nothing else", function () { | ||
validate("ESN", [new Error(), "foo"]) | ||
notThrown(t, 'E w/ error requires nothing else', function () { | ||
validate('ESN', [new Error(), 'foo']) | ||
}) | ||
thrown(t, "EMISSINGARG", "E w/o error works as usual", function () { | ||
validate("ESN", [null, "foo"]) | ||
thrown(t, 'EMISSINGARG', 'E w/o error works as usual', function () { | ||
validate('ESN', [null, 'foo']) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7176
2
6
124