有没有一种方法可以检查所有必填字段而无需测试每个字段。
验证规则
const Confirmation = Joi.any().valid(Joi.ref('password')).required().options({ language: { any: { allowOnly: 'must match password' } } });
const Email = Joi.string().email();
const Firstname = Joi.string().regex(/^[a-zA-Z\']+$/).min(2).max(30);
const Lastname = Joi.string().regex(/^[a-zA-Z\']+$/).min(2).max(30);
const Password = Joi.string().min(3).max(30);
const Username = Joi.string().regex(/^[a-zA-Z\-\.]+$/).min(3).max(30);
exports.create = {
payload: {
email: Email.required(),
firstname: Firstname.required(),
lastname: Lastname.required(),
password: Password.required(),
password_confirmation: Confirmation,
username: Username.required()
}
};
测试
'use strict';
const Lab = require('lab');
const lab = exports.lab = Lab.script();
const Code = require('code');
const Server = require('../../index');
lab.experiment('User', function() {
lab.test('create firstname should be required', function (done) {
const options = {
method: 'POST',
url: '/api/users',
payload: {
email: 'me@mydomain.com',
password: 'mysecret'
}
};
Server.inject(options, function(response) {
const result = response.result;
Code.expect(response.statusCode).to.equal(422);
Code.expect(result.message).to.equal('child "firstname" fails because ["firstname" is required]');
done();
});
});
//AND SO ON
lab.test('create firstname should be required', function (done) {
const options = {
method: 'POST',
url: '/api/users',
payload: {
email: 'me@mydomain.com',
password: 'mysecret',
firstname: 'me'
}
};
Server.inject(options, function(response) {
const result = response.result;
Code.expect(response.statusCode).to.equal(422);
Code.expect(result.message).to.equal('child "lastname" fails because ["lastname" is required]');
done();
});
});
});