Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including expects with all npm packages installed. Try it out:

var expects = require("expects")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

expects v0.1.0

Argument/Object structure verification for Javascript/Node.js

Argument verification for node.js

To run the tests:

cd expects/

Quick rundown:

Expects verifies a json object, recursively checking that specified properties exists and/or conforms to certain patterns. It does this by comparing to json objects, first being the object to check, the second being a representation of the expected data. This representation should be structured like the expected data, but having verifiers in place of the values. A verifier can be a function or a regexp.

E.g. if the json object expected looks like this (assuming all values are required): { someValue : , someOther : { checkThis : } }

the pattern would look like this, using the built-in verifiers requiredInt and requiredString: { someValue : expects.requiredInt, someOther : { type : "object", presence : "req", pattern : { checkThis : expects.requiredString } } }

Expects will throw an error on unsatisfied requirements, stating the first failed value encountered.

List of verifiers:

expects.requiredString - String, required expects.optionalString - String, optional

expects.requiredNumber - Integer, required, expects.optionalNumber - Integer, optional

expects.requiredParseableInt - Integer-parseable string, required expects.optionalParseableInt - Integer-parseable string, optional

expects.requiredURL - Full URL with protocol (only http accepted), domain|IP, port (optional), path (optional) and query (optional), required expects.optionalURL - Full URL with protocol (only http accepted), domain|IP, port (optional), path (optional) and query (optional), optional

expects.requiredPort = Integer in the range ]1:65535], required expects.optionalPort = Integer in the range ]1:65535], optional

expects.requiredIPV4 = String representing a valid IPv4, required expects.optionalIPV4 = String representing a valid IPv4, optional


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free