index.js 3.6 KB

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