這是廣受歡迎但已棄用的 npm 套件 chai-param (https://github.com/theblacksmith/chai-param) 的分支。

已修正匯入路徑以支援現代 Node.js 版本。

chai-param-nn

使用 chai 驗證您方法的參數

快速開始

將其安裝在您的專案中

npm install chai-param-nn

引入並載入它

var chai = require('chai'),
    chaiParam = require('chai-param-nn'),
    expect = chai.expect,
    should = chai.should(),
    param = chaiParam.param;

chai.use(chaiParam);

愉快的參數守護:)

function register(user, pass) {
	// using should
	param(user, 'user', 'UsersCtl.register').should.not.be.empty;

	// or using expect
	expect(user).param('user', 'UsersCtl.register')
		.to.have.length.above(5)
		.and.below(16);
}

設定

可以透過設定 chaiParam.config 物件的屬性來設定 chai-param-nn。例如

var chaiParam = require('chai-param-nn');

chaiParam.config.improveMessages = true;

improveMessages

類型: Boolean 預設值: true

控制 chai-param-nn 是否應將 chai 的訊息替換為更具描述性的訊息。因此,舉例來說,您將會得到,而不是

UserCtl.register: expected 'asdf' to have a length above 5 but got 4

您將會得到…

Improved error message

在錯誤訊息上顯示呼叫者

類型: Boolean 預設值: true

此選項僅適用於 node,且需要 improveMessagestrue

chaiParam.config.showCaller = true;

disableColors

類型: Boolean 預設值: false

停用訊息著色。很無聊,但對測試很有用。