Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A ridiculously light-weight function argument validator

License

NotificationsYou must be signed in to change notification settings

iarna/aproba

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 () {}) // okmyfunc(123, 23, function () {}) // type errormyfunc('test', 23) // missing arg errormyfunc('test', 23, function () {}, true) // too many args error

Valid types are:

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

Validation failures throw one of three exception types, distinguished by acode property ofEMISSINGARG,EINVALIDTYPE orETOOMANYARGS.

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

If anerror argument is found and is not null then the remainingarguments are optional. That is, if you sayESO then that's like using anon-magicalE 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 forfs.createWriteStream. The docs for it describe it thusly:

fs.createWriteStream(path[, options])

This would be a signature ofSO|S. That is, a string and and object, orjust a string.

Now, if you read the fullfs docs, you'll see that actually path can ALSObe 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 possiblecombinations and that implies a signature ofSO|SS|OO|OS|S|O. Theawkwardness is a feature: It reminds you of the complexity you're adding toyour API when you do this sort of thing.

Browser support

This has no dependencies and should work in browsers, though you'll havenoisier 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 suchthing as an optional argument.

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

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

About

A ridiculously light-weight function argument validator

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp