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 function addSchema (schema, arity) {
20 var group = arity[schema.length] = arity[schema.length] || []
21 if (group.indexOf(schema) === -1) group.push(schema)
24 var validate = module.exports = function (rawSchemas, args) {
25 if (arguments.length !== 2) throw wrongNumberOfArgs(['SA'], arguments.length)
26 if (!rawSchemas) throw missingRequiredArg(0, 'rawSchemas')
27 if (!args) throw missingRequiredArg(1, 'args')
28 if (!types.S.check(rawSchemas)) throw invalidType(0, ['string'], rawSchemas)
29 if (!types.A.check(args)) throw invalidType(1, ['array'], args)
30 var schemas = rawSchemas.split('|')
33 schemas.forEach(function (schema) {
34 for (var ii = 0; ii < schema.length; ++ii) {
36 if (!types[type]) throw unknownType(ii, type)
38 if (/E.*E/.test(schema)) throw moreThanOneError(schema)
39 addSchema(schema, arity)
40 if (/E/.test(schema)) {
41 addSchema(schema.replace(/E.*$/, 'E'), arity)
42 addSchema(schema.replace(/E/, 'Z'), arity)
43 if (schema.length === 1) addSchema('', arity)
46 var matching = arity[args.length]
48 throw wrongNumberOfArgs(Object.keys(arity), args.length)
50 for (var ii = 0; ii < args.length; ++ii) {
51 var newMatching = matching.filter(function (schema) {
53 var typeCheck = types[type].check
54 return typeCheck(args[ii])
56 if (!newMatching.length) {
57 var labels = matching.map(function (schema) {
58 return types[schema[ii]].label
59 }).filter(function (schema) { return schema != null })
60 throw invalidType(ii, labels, args[ii])
62 matching = newMatching
66 function missingRequiredArg (num) {
67 return newException('EMISSINGARG', 'Missing required argument #' + (num + 1))
70 function unknownType (num, type) {
71 return newException('EUNKNOWNTYPE', 'Unknown type ' + type + ' in argument #' + (num + 1))
74 function invalidType (num, expectedTypes, value) {
76 Object.keys(types).forEach(function (typeCode) {
77 if (types[typeCode].check(value)) valueType = types[typeCode].label
79 return newException('EINVALIDTYPE', 'Argument #' + (num + 1) + ': Expected ' +
80 englishList(expectedTypes) + ' but got ' + valueType)
83 function englishList (list) {
84 return list.join(', ').replace(/, ([^,]+)$/, ' or $1')
87 function wrongNumberOfArgs (expected, got) {
88 var english = englishList(expected)
89 var args = expected.every(function (ex) { return ex.length === 1 })
92 return newException('EWRONGARGCOUNT', 'Expected ' + english + ' ' + args + ' but got ' + got)
95 function moreThanOneError (schema) {
96 return newException('ETOOMANYERRORTYPES',
97 'Only one error type per argument signature is allowed, more than one found in "' + schema + '"')
100 function newException (code, msg) {
101 var e = new Error(msg)
103 if (Error.captureStackTrace) Error.captureStackTrace(e, validate)