Test validation should inherit to submodels

This commit is contained in:
David Cheung 2016-04-26 16:59:39 -04:00
parent 4cd84dcd8a
commit 7ae3047398
1 changed files with 47 additions and 1 deletions

View File

@ -36,7 +36,6 @@ describe('Model / PersistedModel', function() {
var joe = new User({ email: 'joe@joe.com' }); var joe = new User({ email: 'joe@joe.com' });
var joe2 = new User({ email: 'joe@joe.com' }); var joe2 = new User({ email: 'joe@joe.com' });
joe.save(function() { joe.save(function() {
joe2.save(function(err) { joe2.save(function(err) {
assert(err, 'should get a validation error'); assert(err, 'should get a validation error');
@ -452,6 +451,53 @@ describe.onServer('Remote Methods', function() {
assert.equal(user.a, 'foo'); assert.equal(user.a, 'foo');
assert.equal(user.b, 'bar'); assert.equal(user.b, 'bar');
}); });
it('Extended should inherit validations', function(done) {
var datasource = loopback.createDataSource({
connector: loopback.Memory,
});
//setup parent
var Parent = PersistedModel.extend('Parent', { 'name': String });
Parent.attachTo(datasource);
Parent.validatesLengthOf(
'name', { min: 5, message: { min: 'too short' }}
);
//setup child
var Child = Parent.extend('Child');
Child.attachTo(datasource);
var aChild = new Child({ name: 'foo1' });
aChild.save(function(err, res) {
assert(err.message.match('too short'), 'Should have error message');
done();
});
});
it('Child should be able to extend validations', function(done) {
var datasource = loopback.createDataSource({
connector: loopback.Memory,
});
var Parent = PersistedModel.extend('Parent', { 'name': String });
Parent.attachTo(datasource);
Parent.validatesLengthOf(
'name', { min: 5, message: { min: 'too short' }}
);
var Child = Parent.extend('Child');
Child.attachTo(datasource);
//child validation
Child.validatesFormatOf(
'name', { with: 'right', message: { with: 'Not right' }}
);
var aChild = new Child({ name: 'foo1' });
var aChild2 = new Child({ name: 'right' });
aChild.save(function(err, res) {
assert(err.details.messages.name[0] == 'too short',
'Have error message');
assert(err.details.messages.name[1].with == 'Not right',
'Have error message');
done();
});
});
}); });
describe('Model.extend() events', function() { describe('Model.extend() events', function() {