3 function isArguments (thingy) {
4 return thingy != null && typeof thingy === 'object' && thingy.hasOwnProperty('callee')
8 '*': {label: 'any', check: function () { return true }},
9 A: {label: 'array', check: function (thingy) { return Array.isArray(thingy) || isArguments(thingy) }},
10 S: {label: 'string', check: function (thingy) { return typeof thingy === 'string' }},
11 N: {label: 'number', check: function (thingy) { return typeof thingy === 'number' }},
12 F: {label: 'function', check: function (thingy) { return typeof thingy === 'function' }},
13 O: {label: 'object', check: function (thingy) { return typeof thingy === 'object' && thingy != null && !types.A.check(thingy) && !types.E.check(thingy) }},
14 B: {label: 'boolean', check: function (thingy) { return typeof thingy === 'boolean' }},
15 E: {label: 'error', check: function (thingy) { return thingy instanceof Error }},
16 Z: {label: 'null', check: function (thingy) { return thingy == null }}
19 var validate = module.exports = function (rawSchemas, args) {
20 if (arguments.length !== 2) throw wrongNumberOfArgs(['SA'], arguments.length)
21 if (!rawSchemas) throw missingRequiredArg(0, 'rawSchemas')
22 if (!args) throw missingRequiredArg(1, 'args')
23 if (!types.S.check(rawSchemas)) throw invalidType(0, ['string'], rawSchemas)
24 if (!types.A.check(args)) throw invalidType(1, ['array'], args)
25 var schemas = rawSchemas.split('|')
27 function addSchema (schema) {
28 var group = arity[schema.length] = arity[schema.length] || []
29 if (group.indexOf(schema) === -1) group.push(schema)
31 schemas.forEach(function (schema) {
32 for (var ii = 0; ii < schema.length; ++ii) {
34 if (!types[type]) throw unknownType(ii, type)
36 if (/E.*E/.test(schema)) throw moreThanOneError(schema)
38 if (/E/.test(schema)) {
39 addSchema(schema.replace(/E.*$/, 'E'))
40 addSchema(schema.replace(/E/, 'Z'))
41 if (schema.length === 1) addSchema('')
44 var matching = arity[args.length]
46 throw wrongNumberOfArgs(Object.keys(arity), args.length)
48 for (var ii = 0; ii < args.length; ++ii) {
49 var newMatching = matching.filter(function (schema) {
51 var typeCheck = types[type].check
52 return typeCheck(args[ii])
54 if (!newMatching.length) {
55 var labels = matching.map(function (schema) {
56 return types[schema[ii]].label
57 }).filter(function (schema) { return schema != null })
58 throw invalidType(ii, labels, args[ii])
60 matching = newMatching
64 function missingRequiredArg (num) {
65 return newException('EMISSINGARG', 'Missing required argument #' + (num + 1))
68 function unknownType (num, type) {
69 return newException('EUNKNOWNTYPE', 'Unknown type ' + type + ' in argument #' + (num + 1))
72 function invalidType (num, expectedTypes, value) {
74 Object.keys(types).forEach(function (typeCode) {
75 if (types[typeCode].check(value)) valueType = types[typeCode].label
77 return newException('EINVALIDTYPE', 'Argument #' + (num + 1) + ': Expected ' +
78 englishList(expectedTypes) + ' but got ' + valueType)
81 function englishList (list) {
82 return list.join(', ').replace(/, ([^,]+)$/, ' or $1')
85 function wrongNumberOfArgs (expected, got) {
86 var english = englishList(expected)
87 var args = expected.every(function (ex) { return ex.length === 1 })
90 return newException('EWRONGARGCOUNT', 'Expected ' + english + ' ' + args + ' but got ' + got)
93 function moreThanOneError (schema) {
94 return newException('ETOOMANYERRORTYPES',
95 'Only one error type per argument signature is allowed, more than one found in "' + schema + '"')
98 function newException (code, msg) {
99 var e = new Error(msg)
101 Error.captureStackTrace(e, validate)