1 | if (typeof require !== 'undefined') {
|
2 | var Validator = require('../src/validator.js');
|
3 | var expect = require('chai').expect;
|
4 | } else {
|
5 | var Validator = window.Validator;
|
6 | var expect = window.chai.expect;
|
7 | }
|
8 |
|
9 | describe('max validation rule', function() {
|
10 | it('should fail with the name "David". Maximum size is 3 letters.', function() {
|
11 | var validator = new Validator({
|
12 | name: 'David'
|
13 | }, {
|
14 | name: 'max:3'
|
15 | });
|
16 | expect(validator.passes()).to.be.false;
|
17 | });
|
18 |
|
19 | it('should pass with the name "David". Maximum size is 5 letters.', function() {
|
20 | var validator = new Validator({
|
21 | name: 'Da'
|
22 | }, {
|
23 | name: 'max:5'
|
24 | });
|
25 | expect(validator.passes()).to.be.true;
|
26 | });
|
27 |
|
28 | it('should fail with the age "18". Max is 12.', function() {
|
29 | var validator = new Validator({
|
30 | age: 18
|
31 | }, {
|
32 | age: 'max:12'
|
33 | });
|
34 | expect(validator.fails()).to.be.true;
|
35 | });
|
36 |
|
37 | it('should pass with the age "12". Max is 12.', function() {
|
38 | var validator = new Validator({
|
39 | age: 12
|
40 | }, {
|
41 | age: 'max:12'
|
42 | });
|
43 | expect(validator.passes()).to.be.true;
|
44 | });
|
45 |
|
46 | it('should fail with boolean true value', function() {
|
47 | var validator = new Validator({
|
48 | val: true
|
49 | }, {
|
50 | val: 'max:5'
|
51 | });
|
52 | expect(validator.fails()).to.be.true;
|
53 | });
|
54 |
|
55 | it('should fail with boolean false value', function() {
|
56 | var validator = new Validator({
|
57 | val: false
|
58 | }, {
|
59 | val: 'max:5'
|
60 | });
|
61 | expect(validator.fails()).to.be.true;
|
62 | });
|
63 |
|
64 | it('should pass when the age is 0', function() {
|
65 | var validator = new Validator({
|
66 | age: 0
|
67 | }, {
|
68 | age: 'max:2'
|
69 | });
|
70 | expect(validator.passes()).to.be.true;
|
71 | expect(validator.fails()).to.be.false;
|
72 | });
|
73 |
|
74 | it('should pass when the field is an empty string', function() {
|
75 | var validator = new Validator({
|
76 | email: ''
|
77 | }, {
|
78 | email: 'max:2'
|
79 | });
|
80 | expect(validator.passes()).to.be.true;
|
81 | expect(validator.fails()).to.be.false;
|
82 | });
|
83 |
|
84 | it('should pass when the field does not exist', function() {
|
85 | var validator = new Validator({}, {
|
86 | email: 'max:2'
|
87 | });
|
88 | expect(validator.passes()).to.be.true;
|
89 | expect(validator.fails()).to.be.false;
|
90 | });
|
91 |
|
92 | it('should fail when given string-integer value', function() {
|
93 | var validator = new Validator({
|
94 | val: '18'
|
95 | }, {
|
96 | val: 'integer|max:16'
|
97 | });
|
98 | expect(validator.passes()).to.be.false;
|
99 | });
|
100 |
|
101 | it('should fail when given string-float value', function() {
|
102 | var validator = new Validator({
|
103 | val: '17.56'
|
104 | }, {
|
105 | val: 'numeric|max:17.5'
|
106 | });
|
107 | expect(validator.passes()).to.be.false;
|
108 | });
|
109 | });
|