Skip to content

Commit 74bde9f

Browse files
Merge pull request #83 from RobinBuschmann/issue-59
fixes #59
2 parents c3f8b3f + 280d422 commit 74bde9f

File tree

2 files changed

+124
-4
lines changed

2 files changed

+124
-4
lines changed

lib/annotations/validation/Length.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {addAttributeOptions} from "../../services/models";
77
export function Length({msg, min, max}: {msg?: string; min?: number; max?: number}): Function {
88

99
let options: [number, number] | {msg: string, args: [number, number]};
10-
const length = [min, max] as [number, number];
10+
const length = [min || 0, max] as [number, number];
1111

1212
if (msg) {
1313
options = {args: length, msg: msg as string};

test/specs/validation.spec.ts

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* tslint:disable:max-classes-per-file */
2+
13
import {expect, use} from 'chai';
24
import * as chaiAsPromised from 'chai-as-promised';
35
import {DefineValidateOptions} from "sequelize";
@@ -8,6 +10,10 @@ import {
810
} from "../models/ShoeWithValidation";
911
import {majorVersion} from "../../lib/utils/versioning";
1012
import {Is} from "../../lib/annotations/validation/Is";
13+
import {Model} from "../../lib/models/Model";
14+
import {Table} from "../../lib/annotations/Table";
15+
import {Column} from "../../lib/annotations/Column";
16+
import {Length} from "../../lib/annotations/validation/Length";
1117

1218
use(chaiAsPromised);
1319

@@ -20,7 +26,7 @@ describe('validation', () => {
2026
describe(`rawAttributes of ${ShoeWithValidation.name}`, () => {
2127

2228
const rawAttributes = ShoeWithValidation['rawAttributes'];
23-
const shoeAttributes: {[key: string]: DefineValidateOptions} = {
29+
const shoeAttributes: { [key: string]: DefineValidateOptions } = {
2430
id: {
2531
isUUID: UUID_VERSION
2632
},
@@ -98,7 +104,7 @@ describe('validation', () => {
98104

99105
describe('validation', () => {
100106

101-
const data: {[key: string]: {valid: any[]; invalid: any[]}} = {
107+
const data: { [key: string]: { valid: any[]; invalid: any[] } } = {
102108
id: {
103109
valid: ['903830b8-4dcc-4f10-a5aa-35afa8445691', null, undefined],
104110
invalid: ['', 'abc', 1],
@@ -190,9 +196,123 @@ describe('validation', () => {
190196
describe('Is', () => {
191197

192198
it('Should throw due to missing name of function', () => {
193-
194199
expect(() => Is(() => null)).to.throw(/Passed validator function must have a name/);
200+
});
201+
202+
});
203+
204+
describe('Length', () => {
205+
206+
it('should not produce an error', () => {
207+
@Table
208+
class User extends Model<User> {
209+
@Length({min: 0, max: 5}) @Column name: string;
210+
}
211+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
212+
sequelizeValidationOnly.addModels([User]);
213+
const user = new User({name: 'elisa'});
214+
215+
if (majorVersion === 3) {
216+
return user.validate().then(err => expect(err).to.be.not.an('object'));
217+
} else if (majorVersion === 4) {
218+
return expect(user.validate()).to.be.not.rejected;
219+
}
220+
});
221+
222+
it('should produce an error due to unfulfilled max', () => {
223+
@Table
224+
class User extends Model<User> {
225+
@Length({min: 0, max: 5}) @Column name: string;
226+
}
227+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
228+
sequelizeValidationOnly.addModels([User]);
229+
const user = new User({name: 'elisa tree'});
230+
231+
if (majorVersion === 3) {
232+
return user.validate().then(err => expect(err).to.be.an('object'));
233+
} else if (majorVersion === 4) {
234+
return expect(user.validate()).to.be.rejected;
235+
}
236+
});
237+
238+
it('should produce an error due to unfulfilled min', () => {
239+
@Table
240+
class User extends Model<User> {
241+
@Length({min: 5, max: 5}) @Column name: string;
242+
}
243+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
244+
sequelizeValidationOnly.addModels([User]);
245+
const user = new User({name: 'elli'});
246+
247+
if (majorVersion === 3) {
248+
return user.validate().then(err => expect(err).to.be.an('object'));
249+
} else if (majorVersion === 4) {
250+
return expect(user.validate()).to.be.rejected;
251+
}
252+
});
253+
254+
it('should not produce an error (max only)', () => {
255+
@Table
256+
class User extends Model<User> {
257+
@Length({max: 5}) @Column name: string;
258+
}
259+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
260+
sequelizeValidationOnly.addModels([User]);
261+
const user = new User({name: 'elisa'});
262+
263+
if (majorVersion === 3) {
264+
return user.validate().then(err => expect(err).to.be.not.an('object'));
265+
} else if (majorVersion === 4) {
266+
return expect(user.validate()).to.be.not.rejected;
267+
}
268+
});
269+
270+
it('should produce an error (max only)', () => {
271+
@Table
272+
class User extends Model<User> {
273+
@Length({max: 5}) @Column name: string;
274+
}
275+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
276+
sequelizeValidationOnly.addModels([User]);
277+
const user = new User({name: 'elisa tree'});
278+
279+
if (majorVersion === 3) {
280+
return user.validate().then(err => expect(err).to.be.an('object'));
281+
} else if (majorVersion === 4) {
282+
return expect(user.validate()).to.be.rejected;
283+
}
284+
});
285+
286+
it('should not produce an error (min only)', () => {
287+
@Table
288+
class User extends Model<User> {
289+
@Length({min: 4}) @Column name: string;
290+
}
291+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
292+
sequelizeValidationOnly.addModels([User]);
293+
const user = new User({name: 'elisa'});
294+
295+
if (majorVersion === 3) {
296+
return user.validate().then(err => expect(err).to.be.not.an('object'));
297+
} else if (majorVersion === 4) {
298+
return expect(user.validate()).to.be.not.rejected;
299+
}
300+
});
195301

302+
it('should produce an error (min only)', () => {
303+
@Table
304+
class User extends Model<User> {
305+
@Length({min: 5}) @Column name: string;
306+
}
307+
const sequelizeValidationOnly = createSequelizeValidationOnly(false);
308+
sequelizeValidationOnly.addModels([User]);
309+
const user = new User({name: 'elli'});
310+
311+
if (majorVersion === 3) {
312+
return user.validate().then(err => expect(err).to.be.an('object'));
313+
} else if (majorVersion === 4) {
314+
return expect(user.validate()).to.be.rejected;
315+
}
196316
});
197317

198318
});

0 commit comments

Comments
 (0)