2.0.0 • Published 6 years ago

aproba v2.0.0

Weekly downloads
16,330,041
License
ISC
Repository
github
Last release
6 years ago

aproba

A ridiculously light-weight function argument validator

var validate = require("aproba")

function myfunc(a, b, c) {
  // `a` must be a string, `b` a number, `c` a function
  validate('SNF', arguments) // [a,b,c] is also valid
}

myfunc('test', 23, function () {}) // ok
myfunc(123, 23, function () {}) // type error
myfunc('test', 23) // missing arg error
myfunc('test', 23, function () {}, true) // too many args error

Valid types are:

typedescription
*matches any type
AArray.isArray OR an arguments object
Stypeof == string
Ntypeof == number
Ftypeof == function
Otypeof == object and not type A and not type E
Btypeof == boolean
Einstanceof Error OR null (special: see below)
Z== null

Validation failures throw one of three exception types, distinguished by a code property of EMISSINGARG, EINVALIDTYPE or ETOOMANYARGS.

If you pass in an invalid type then it will throw with a code of EUNKNOWNTYPE.

If an error argument is found and is not null then the remaining arguments are optional. That is, if you say ESO then that's like using a non-magical E in: E|ESO|ZSO.

But I have optional arguments?!

You can provide more than one signature by separating them with pipes |. If any signature matches the arguments then they'll be considered valid.

So for example, say you wanted to write a signature for fs.createWriteStream. The docs for it describe it thusly:

fs.createWriteStream(path[, options])

This would be a signature of SO|S. That is, a string and and object, or just a string.

Now, if you read the full fs docs, you'll see that actually path can ALSO be a buffer. And options can be a string, that is:

path <String> | <Buffer>
options <String> | <Object>

To reproduce this you have to fully enumerate all of the possible combinations and that implies a signature of SO|SS|OO|OS|S|O. The awkwardness is a feature: It reminds you of the complexity you're adding to your API when you do this sort of thing.

Browser support

This has no dependencies and should work in browsers, though you'll have noisier stack traces.

Why this exists

I wanted a very simple argument validator. It needed to do two things:

  1. Be more concise and easier to use than assertions

  2. Not encourage an infinite bikeshed of DSLs

This is why types are specified by a single character and there's no such thing as an optional argument.

This is not intended to validate user data. This is specifically about asserting the interface of your functions.

If you need greater validation, I encourage you to write them by hand or look elsewhere.

gaugemove-concurrentlycopy-concurrentlyrun-queuevuedragdropuploadimageskilli8n-react-native-fast-imagegew-nodejs-api-client@cashremit/cr-streamline-iconsdbase.idcloud-archive-s3visualcamp-automationbb-chat@frxf/frxf@fundefund/funde_ckeasycommands-betagql_din_mod@jttechnic/interpreter@l1nyanm1ng/react-picture-viewer@saeon/ol-react@saeon/quick-form@olivervorasai/slidercogoportutilsjs4cytoscape@mink-opn/build-tokens@saaspe/componentsexpand-react-bridgenode-gio@everything-registry/sub-chunk-1158@simstudio/htmldiffproject-snowflakezzzxxxyyy321123@saqib1987/quiz_testemr-tabulator-tablesengine-munger@hproinformatica/functions@ericmcornelius/ease@evocateur/libnpmaccess@evocateur/libnpmpublish@hieuquang2212/formes-react-bridge@hawkingnetwork/react-native-tab-vieweslint-config-k3cman-angular@greminder/bootstrap-material-designevanutilsevt-asyncevt-promisefastlion-picture-viewerfhir2@inti-ar/evm-chains@infinitebrahmanuniverse/nolb-apr@ikon-x/ckeditor5-custom-build-for-irrosoft@idas1/ui-component-lib@icetee/react-recaptcha-v3@ifanshx/cycgods@innodata/vue-v3-ya-metrika@elsouza1985/react-org-chart@edoroshenko/codemirror@enirisdev/angular-google-charts@fabwcie/ckeditor5-preview@faizanhaider/iconsax@farvater/open-pedigree@img-arena/img-ui-mui-theme@img-arena/ui-corefacerecfrdrk-js-semaphore@kirrosh/pts@kuinox/testpackagethatnooneshoulduse@kuinox/testpackagethatnooneshoulduseever@krwhitley/neataptic@kristoffertonning/vue-laravel-errorsstyledbuttonxxsvelte-component-libsushi-sdk-ftmgit-branching-workflow@luaeb/assetsgh-monoproject-clistyletoolsgentle-fs@playsavage/savagedb@positionex/position-sdk@plogg/threejamuskalimjameslinenode.js1@poondestroyer/sig@podlubnaja/ui-kitvformlmqverify-img-codejeuxuijesusdemovue-infinite-loading-propsvue-dev-clonevue-v3-yandex-metrikavinpm@rbc-public/react-selectable-fast@react-18-pdf/root@rstacruz/pnpm@rps-engine/core@saeon/logger@safely-project/safely-ts@sackmanson/quill-image-uploader
2.0.0

6 years ago

1.2.0

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

9 years ago

1.0.0

9 years ago