npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chai-joi

v2.0.3

Published

Chai Extensions for Joi Validation

Downloads

772

Readme

chai-joi

Extends Chai with Joi specific assertions.

npm version Build Status Dependencies Status DevDependencies Status

TL;DL

Assuming that result is the result of a Joi validation:

  • Assert-style
    • chai.assert.isValidation(result)
  • BDD-style
    • result.should
      • [.not].be.a.validation
      • [.not].validate
      • [.not].have.an.error
      • [.not].have.a.value
      • [.not].have.errmsgs
      • [.not].have.errmsgs.that.include(str)
      • [.not].have.errmsg(str)
  • TDD-style
    • expect(result).to
      • [.not].be.a.validation
      • [.not].validate
      • [.not].have.an.error
      • [.not].have.a.value
      • [.not].have.errmsgs
      • [.not].have.errmsgs.that.include(str)
      • [.not].have.errmsg(str)

(See test/chai-joi.js for uses.)

Example:

var joi    = require('joi'),
    chai   = require('chai'),
    expect = chai.expect;

chai.should();

chai.use( require('chai-joi') );

var data={
  str:'foo',
  num: 100
};

var schema=joi.object({
  str:joi.string().allow(''),
  num:joi.number().min(1).max(10)
});

var result=joi.validate(data,schema);

result.should.not.validate;
result.should.have.errmsg('"value" must be less than or equal to 10');

data.num=1;
result=joi.validate(data,schema);

expect(result).to.validate;
expect(result).to.not.have.errmsgs; // same thing, really.

assert

Extension to chai.assert to test for a valid Joi validation result.


assert.isValidation(target)

Asserts that the target object is result of a call to Joi.validate()

Parameters

target: object, Asserts that the target object is result of a call to Joi.validate()

Returns: boolean

property

Chainable properties.


property.validation()

Assert that target is a Joi validation

Example:

expect(target).to.[not].be.a.validation
target.should.[not].be.a.validation

property.validate()

Assert that target validates correctly

Example:

expect(target).should.[not].validate
target.should.[not].validate

property.error()

Assert that target contains one or more errors (unsuccessful validation). Mutates current chainable object to be target.error.

Example:

expect(target).to.[not].have.an.error
target.should.[not].have.an.error

property.value()

Assert that target contains a value. Mutates current chainable object to be target.value.

Example:

expect(target).to.[not].have.a.value
target.should.[not].have.a.value

property.errmsgs()

Assert that target contains one or more error messages (unsuccessful validation). Mutates current chainable object to be list {String[]} of error messages.

Example:

expect(target).to.[not].have.errmsgs
target.should.[not].have.errmsgs
expect(target).to.[not].have.errmsgs.that.include(errmsg)
target.should.have.errmsgs.that.include(errmsg)

method

Chainable methods.


method.errmsg(msg)

Assert that target contains specified error message (unsuccessful validation).

Parameters

msg: String, errmsg to match

Example:

target.should.[not].have.errmsg(msg)