Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

have #3

Closed
stdarg opened this issue Mar 6, 2014 · 7 comments
Closed

have #3

stdarg opened this issue Mar 6, 2014 · 7 comments

Comments

@stdarg
Copy link

stdarg commented Mar 6, 2014

a nice arg checking module

@hemanth
Copy link
Owner

hemanth commented Mar 8, 2014

Nice suggestion, noted down. It's a good competition for nopt

@stdarg
Copy link
Author

stdarg commented Mar 8, 2014

Not exactly. nopt is about command line arguments where have is to validate function arguments. A lot of bugs come down to passing the wrong type of argument and have is useful for validation.

@hemanth
Copy link
Owner

hemanth commented Mar 8, 2014

Yup.

@hemanth
Copy link
Owner

hemanth commented Mar 19, 2014

@stdarg I was pawing at it:

have.assert(function(cond, message) {
  if (!cond) {
    console.log('WARN: assertion failed: ' + message);
  }
});

In the above method is there way to say which method it was validating the arguments for?

Say like 'WARN: assertion failed for ' + method + ' : ' + message ?

@stdarg
Copy link
Author

stdarg commented Mar 20, 2014

It's implicit. The general use case of have is to check arguments for a function, e.g.:

/**
 * Config provides a simple read-only API to a configuration object.
 * @param {String} pathToConfigFile The configuration file
 * @param {String} [region] Optional indicator for the current region, e.g. 'en'
 */
function Config(pathToConfigFileIn, region) {
    have(arguments, {pathToConfigFile: 'str', region: 'opt str' });
    [...]
}

In the above example, have would throw if pathToConfigFile is not a string or if region is defined and is not a string. Also, rather than throw, have can do some user defined behavior. The thrown error would have a call stack. If you generated your own have response, you can get the call stack with the following statement:

var stack = new Error().stack;

Though in the above case, we check the arguments object, it could be used to check any object. Another good use would be to validate documents returned from MongoDB or the JSON body of an HTTP request.

I find it a super-handy module.

@hemanth
Copy link
Owner

hemanth commented Mar 21, 2014

Was having the discussion with author and he suggested something similar

@hemanth
Copy link
Owner

hemanth commented Apr 11, 2014

Fixed in http://nmotw.in/have thanks!

@hemanth hemanth closed this as completed Apr 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants