UNPKG

9.27 kBJavaScriptView Raw
1if (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
9describe('Error messages', function() {
10 describe('first()', function() {
11 it('should return an error message that states the email is required', function() {
12 var validator = new Validator({
13 email: ''
14 }, {
15 email: 'required|email'
16 });
17 expect(validator.passes()).to.be.false;
18 expect(validator.errors.first('email')).to.equal('The email field is required.');
19 });
20
21 it('should have a method on the errors object to retrieve the first error message for an attribute', function() {
22 var validator = new Validator({
23 email: ''
24 }, {
25 email: 'required|email'
26 });
27 expect(validator.passes()).to.be.false;
28 expect(validator.errors.first('email')).to.equal('The email field is required.');
29 });
30
31 it('should return false if errors.first() is called and there are no errors', function() {
32 var validator = new Validator({
33 email: 'john@yahoo.com'
34 }, {
35 email: 'required|email'
36 });
37 expect(validator.passes()).to.be.true;
38 expect(validator.errors.first('email')).to.equal(false);
39 });
40
41 it('should return an error message that states the email must be valid', function() {
42 var validator = new Validator({
43 email: 'john@yahoo'
44 }, {
45 email: 'required|email'
46 });
47 expect(validator.passes()).to.be.false;
48 expect(validator.errors.first('email')).to.equal('The email format is invalid.');
49 });
50
51 it('should return null for a key without an error message', function() {
52 var validator = new Validator({
53 name: 'David'
54 }, {
55 name: 'required'
56 });
57 expect(validator.passes()).to.be.true;
58 expect(validator.errors.first('name')).to.be.false;
59 });
60
61 it('should return error messages with attribute names and values for multi-part rules', function() {
62 var validator = new Validator({
63 age: 17,
64 description: 'a',
65 info: '',
66 hours: 3,
67 pin: '123',
68 range: 20,
69 tweet: 'some tweet'
70 }, {
71 age: 'min:18',
72 description: 'required|min:5',
73 info: 'required|min:3',
74 hours: 'size:5',
75 pin: 'size:4',
76 range: 'max:10',
77 tweet: 'max:5'
78 });
79
80 expect(validator.passes()).to.be.false;
81 expect(validator.errors.first('age')).to.equal('The age must be at least 18.'); // min numeric
82 expect(validator.errors.first('description')).to.equal('The description must be at least 5 characters.'); // min string
83 expect(validator.errors.first('info')).to.equal('The info field is required.');
84 expect(validator.errors.first('hours')).to.equal('The hours must be 5.'); // size numeric
85 expect(validator.errors.first('pin')).to.equal('The pin must be 4 characters.'); // size string
86 expect(validator.errors.first('range')).to.equal('The range may not be greater than 10.'); // max numeric
87 expect(validator.errors.first('tweet')).to.equal('The tweet may not be greater than 5 characters.'); // max string
88 });
89
90 it('should return a customized alpha error message', function() {
91 var validator = new Validator({
92 name: '12'
93 }, {
94 name: 'alpha'
95 });
96 expect(validator.passes()).to.be.false;
97 expect(validator.errors.first('name')).to.equal('The name field must contain only alphabetic characters.');
98 });
99
100 it('should fail with non alpha dash characters', function() {
101 var validator = new Validator({
102 name: 'David *'
103 }, {
104 name: 'alpha_dash'
105 });
106 expect(validator.passes()).to.be.false;
107 expect(validator.errors.first('name')).to.equal('The name field may only contain alpha-numeric characters, as well as dashes and underscores.');
108 });
109
110 it('should fail without a matching confirmation field for the field under validation', function() {
111 var validator = new Validator({
112 password: 'abc'
113 }, {
114 password: 'confirmed'
115 });
116 expect(validator.passes()).to.be.false;
117 expect(validator.errors.first('password')).to.equal('The password confirmation does not match.');
118 });
119
120 it('should fail when the 2 attributes are the same', function() {
121 var validator = new Validator({
122 field1: 'abc',
123 field2: 'abc'
124 }, {
125 field2: 'different:field1'
126 });
127 expect(validator.passes()).to.be.false;
128 expect(validator.errors.first('field2')).to.equal('The field2 and field1 must be different.');
129 });
130
131 it('should fail with a url only containing http://', function() {
132 var link = 'http://';
133 var validator = new Validator({
134 link: link
135 }, {
136 link: 'url'
137 });
138 expect(validator.passes()).to.be.false;
139 expect(validator.errors.first('link')).to.equal('The link format is invalid.');
140 });
141
142 it('should fail the custom telephone rule registration with a default error message', function() {
143 Validator.register('telephone', function(val) {
144 return val.match(/^\d{3}-\d{3}-\d{4}$/);
145 });
146
147 var validator = new Validator({
148 phone: '4213-454-9988'
149 }, {
150 phone: 'telephone'
151 });
152 expect(validator.passes()).to.be.false;
153 expect(validator.errors.first('phone')).to.equal('The phone attribute has errors.');
154 });
155
156 it('should fail the custom telephone rule registration with a custom error message', function() {
157 Validator.register('telephone', function(val) {
158 return val.match(/^\d{3}-\d{3}-\d{4}$/);
159 }, 'The :attribute phone number is not in the format XXX-XXX-XXXX.');
160
161 var validator = new Validator({
162 cell: '4213-454-9988'
163 }, {
164 cell: 'telephone'
165 });
166 expect(validator.passes()).to.be.false;
167 expect(validator.errors.first('cell')).to.equal('The cell phone number is not in the format XXX-XXX-XXXX.');
168 });
169 });
170
171 describe('get()', function() {
172 it('should return an array of all email error messages', function() {
173 var validator = new Validator({
174 email: ''
175 }, {
176 email: 'required|email'
177 });
178
179 expect(validator.passes()).to.be.false;
180 expect(validator.errors.get('email')).to.be.instanceOf(Array);
181 expect(validator.errors.get('email').length).to.equal(1);
182 });
183
184 it('should return an empty array if there are no messages for an attribute', function() {
185 var validator = new Validator({
186 email: 'johndoe@gmail.com'
187 }, {
188 email: 'required|email'
189 });
190
191 expect(validator.passes()).to.be.true;
192 expect(validator.errors.get('email')).to.be.instanceOf(Array);
193 expect(validator.errors.get('email').length).to.equal(0);
194 });
195
196 it('should return multiple array items for an attribute', function() {
197
198 var validator = new Validator({
199 email: 'x'
200 }, {
201 email: 'email|min:10'
202 });
203
204 expect(validator.passes()).to.be.false;
205 expect(validator.errors.get('email')).to.be.instanceOf(Array);
206 expect(validator.errors.get('email').length).to.equal(2);
207
208 });
209
210 });
211
212
213 describe('ValidatorErrors.prototype.all()', function() {
214 it('should return an array of all email error messages', function() {
215 var validation = new Validator({
216 name: 'd',
217 email: '',
218 age: 28
219 }, {
220 name: 'required|min:2',
221 email: 'required|email',
222 age: 'min:18'
223 });
224
225 var expected = JSON.stringify({
226 name: ['The name must be at least 2 characters.'],
227 email: ['The email field is required.']
228 });
229
230 expect(validation.passes()).to.be.false;
231 expect(JSON.stringify(validation.errors.all())).to.equal(expected);
232 });
233 });
234
235 describe('ValidatorErrors.prototype.has()', function() {
236 it('should return an array of all email error messages', function() {
237 var validation = new Validator({
238 name: 'd',
239 email: '',
240 age: 28
241 }, {
242 name: 'required|min:2',
243 email: 'required|email',
244 age: 'min:18'
245 });
246
247 expect(validation.passes()).to.be.false;
248 expect(validation.errors.has('name')).to.equal(true);
249 expect(validation.errors.has('age')).to.equal(false);
250 expect(validation.errors.has('fake-property')).to.equal(false);
251 });
252 });
253
254 describe('should output correct error messages for numeric-like rules', function() {
255 it('should give correct error message with numeric rule', function() {
256 var validator = new Validator({
257 val: '1'
258 }, {
259 val: 'numeric|min:2'
260 });
261 expect(validator.fails()).to.be.true;
262 expect(validator.errors.first('val')).to.equal('The val must be at least 2.');
263 });
264
265 it('should give correct error message with integer rule', function() {
266 var validator = new Validator({
267 val: '1'
268 }, {
269 val: 'integer|min:2'
270 });
271 expect(validator.fails()).to.be.true;
272 expect(validator.errors.first('val')).to.equal('The val must be at least 2.');
273 });
274 });
275
276});