byContract
Search…
Welcome
byContract is a small argument validation library based on JSDOC syntax. The library is available as a UMD-compatible module. Besides, it exposes byContract function globally when window object available, meaning you can still use it in non-modular programming.
It is covered with >100 unit-tests and ready for production. The project can be found on GitHub where you can also find our issue tracker.

Highlights

  • Validation syntax based on JSDoc expressions
  • Entry and exit point contract validation
  • Explanatory exceptions in the style of aproba
  • Recursive structure (object) validation
  • Interface validation
  • Template tag flavor
  • Property decorators flavor
  • Can be disabled or completely cut off for production

Flavors

Main

1
function pdf( path, w, h, options, callback ) {
2
validate( arguments, [
3
"string",
4
"!number",
5
"!number",
6
PdfOptionsType,
7
"function=" ] );
8
}
Copied!

Template tag

1
function pdf( path, w, h, options, callback ) {
2
validateContract`
3
{string} ${ path }
4
{!number} ${ w }
5
{!number} ${ h }
6
{#PdfOptionsType} ${ options }
7
{function=} ${ callback }
8
`;
9
}
Copied!

Property decorator

1
class Page {
2
@validateJsdoc(`
3
@param {string} path
4
@param {!number} w
5
@param {!number} h
6
@param {#PdfOptionsType} options
7
@param {function=} callback
8
@returns {Promise}
9
`)
10
pdf( path, w, h, options, callback ) {
11
return Promise.resolve();
12
}
13
}
Copied!
Last modified 3yr ago