index.js 3.9 KB

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