Skip to content

Commit 4c7a48d

Browse files
Merge pull request #66 from gloorx/patch-1
Add NotEmpty validation message
2 parents 3b2cfbf + 743e6c6 commit 4c7a48d

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

lib/annotations/validation/NotEmpty.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,29 @@ import {addAttributeOptions} from "../../services/models";
44
/**
55
* Don't allow empty strings
66
*/
7-
export function NotEmpty(target: any, propertyName: string): void {
7+
export function NotEmpty(target: any, propertyName: string): void;
8+
export function NotEmpty(options: {msg: string}): Function;
9+
export function NotEmpty(...args: any[]): void|Function {
810

9-
addAttributeOptions(target, propertyName, {
10-
validate: {
11-
notEmpty: true
12-
}
13-
});
11+
if (args.length === 1) {
12+
13+
const options = args[0];
14+
15+
return (target: any, propertyName: string) =>
16+
addAttributeOptions(target, propertyName, {
17+
validate: {
18+
notEmpty: options,
19+
}
20+
});
21+
} else {
22+
23+
const target = args[0];
24+
const propertyName = args[1];
25+
26+
addAttributeOptions(target, propertyName, {
27+
validate: {
28+
notEmpty: true
29+
}
30+
});
31+
}
1432
}

test/specs/validation.spec.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {Model} from "../../lib/models/Model";
1414
import {Table} from "../../lib/annotations/Table";
1515
import {Column} from "../../lib/annotations/Column";
1616
import {Length} from "../../lib/annotations/validation/Length";
17+
import {NotEmpty} from "../../lib/annotations/validation/NotEmpty";
1718

1819
use(chaiAsPromised);
1920

@@ -317,6 +318,73 @@ describe('validation', () => {
317318

318319
});
319320

321+
describe('NotEmpty', () => {
322+
323+
it('should not produce an error', () => {
324+
@Table
325+
class User extends Model<User> {
326+
@NotEmpty @Column name: string;
327+
}
328+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
329+
sequelizeValidationOnly.addModels([User]);
330+
const user = new User({name: 'elisa'});
331+
332+
if (majorVersion === 3) {
333+
return user.validate().then(err => expect(err).to.be.not.an('object'));
334+
} else if (majorVersion === 4) {
335+
return expect(user.validate()).to.be.not.rejected;
336+
}
337+
});
338+
339+
it('should produce an error', () => {
340+
@Table
341+
class User extends Model<User> {
342+
@NotEmpty @Column name: string;
343+
}
344+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
345+
sequelizeValidationOnly.addModels([User]);
346+
const user = new User({name: ''});
347+
348+
if (majorVersion === 3) {
349+
return user.validate().then(err => expect(err).to.be.an('object'));
350+
} else if (majorVersion === 4) {
351+
return expect(user.validate()).to.be.rejected;
352+
}
353+
});
354+
355+
it('should not produce an error (with msg)', () => {
356+
@Table
357+
class User extends Model<User> {
358+
@NotEmpty({msg: 'NotEmpty'}) @Column name: string;
359+
}
360+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
361+
sequelizeValidationOnly.addModels([User]);
362+
const user = new User({name: 'elisa'});
363+
364+
if (majorVersion === 3) {
365+
return user.validate().then(err => expect(err).to.be.not.an('object'));
366+
} else if (majorVersion === 4) {
367+
return expect(user.validate()).to.be.not.rejected;
368+
}
369+
});
370+
371+
it('should produce an error (with msg)', () => {
372+
@Table
373+
class User extends Model<User> {
374+
@NotEmpty({msg: 'NotEmpty'}) @Column name: string;
375+
}
376+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
377+
sequelizeValidationOnly.addModels([User]);
378+
const user = new User({name: ''});
379+
380+
if (majorVersion === 3) {
381+
return user.validate().then(err => expect(err.errors[0].message).to.eq('NotEmpty'));
382+
} else if (majorVersion === 4) {
383+
return expect(user.validate()).to.be.rejected;
384+
}
385+
});
386+
});
387+
320388
});
321389

322390
describe('only', () => {

0 commit comments

Comments
 (0)