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

function pdf( path, w, h, options, callback ) {
  validate( arguments, [
    "string",
    "!number",
    "!number",
    PdfOptionsType,
    "function=" ] );
}

Template tag

function pdf( path, w, h, options, callback ) {
  validateContract`
    {string}          ${ path }
    {!number}         ${ w }
    {!number}         ${ h }
    {#PdfOptionsType} ${ options }
    {function=}       ${ callback }
    `;
}

Property decorator

class Page {
  @validateJsdoc(`
    @param {string}          path
    @param {!number}         w
    @param {!number}         h
    @param {#PdfOptionsType} options
    @param {function=}       callback
    @returns {Promise}
  `)
  pdf( path, w, h, options, callback ) {
    return Promise.resolve();
  }
}

Last updated