You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.7 KiB

4 years ago
  1. aproba
  2. ======
  3. A ridiculously light-weight function argument validator
  4. ```
  5. var validate = require("aproba")
  6. function myfunc(a, b, c) {
  7. // `a` must be a string, `b` a number, `c` a function
  8. validate('SNF', arguments) // [a,b,c] is also valid
  9. }
  10. myfunc('test', 23, function () {}) // ok
  11. myfunc(123, 23, function () {}) // type error
  12. myfunc('test', 23) // missing arg error
  13. myfunc('test', 23, function () {}, true) // too many args error
  14. ```
  15. Valid types are:
  16. | type | description
  17. | :--: | :----------
  18. | * | matches any type
  19. | A | `Array.isArray` OR an `arguments` object
  20. | S | typeof == string
  21. | N | typeof == number
  22. | F | typeof == function
  23. | O | typeof == object and not type A and not type E
  24. | B | typeof == boolean
  25. | E | `instanceof Error` OR `null` **(special: see below)**
  26. | Z | == `null`
  27. Validation failures throw one of three exception types, distinguished by a
  28. `code` property of `EMISSINGARG`, `EINVALIDTYPE` or `ETOOMANYARGS`.
  29. If you pass in an invalid type then it will throw with a code of
  30. `EUNKNOWNTYPE`.
  31. If an **error** argument is found and is not null then the remaining
  32. arguments are optional. That is, if you say `ESO` then that's like using a
  33. non-magical `E` in: `E|ESO|ZSO`.
  34. ### But I have optional arguments?!
  35. You can provide more than one signature by separating them with pipes `|`.
  36. If any signature matches the arguments then they'll be considered valid.
  37. So for example, say you wanted to write a signature for
  38. `fs.createWriteStream`. The docs for it describe it thusly:
  39. ```
  40. fs.createWriteStream(path[, options])
  41. ```
  42. This would be a signature of `SO|S`. That is, a string and and object, or
  43. just a string.
  44. Now, if you read the full `fs` docs, you'll see that actually path can ALSO
  45. be a buffer. And options can be a string, that is:
  46. ```
  47. path <String> | <Buffer>
  48. options <String> | <Object>
  49. ```
  50. To reproduce this you have to fully enumerate all of the possible
  51. combinations and that implies a signature of `SO|SS|OO|OS|S|O`. The
  52. awkwardness is a feature: It reminds you of the complexity you're adding to
  53. your API when you do this sort of thing.
  54. ### Browser support
  55. This has no dependencies and should work in browsers, though you'll have
  56. noisier stack traces.
  57. ### Why this exists
  58. I wanted a very simple argument validator. It needed to do two things:
  59. 1. Be more concise and easier to use than assertions
  60. 2. Not encourage an infinite bikeshed of DSLs
  61. This is why types are specified by a single character and there's no such
  62. thing as an optional argument.
  63. This is not intended to validate user data. This is specifically about
  64. asserting the interface of your functions.
  65. If you need greater validation, I encourage you to write them by hand or
  66. look elsewhere.