UNPKG

2.03 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('stopOnError tests', function() {
10 it('synchronous', function() {
11 var validator = new Validator({
12 email: 'x'
13 }, {
14 email: 'min:1|email'
15 });
16 validator.stopOnError(true);
17 expect(validator.fails()).to.be.true;
18 expect(validator.errors.get('email')).to.have.length(1);
19 });
20
21 // it('asynchronous', function(done) {
22
23 // Validator.registerAsync('username_available', function(val, ruleValue, attribute, passes) {
24 // throw 'Should not have been called.';
25 // });
26 // var validator = new Validator({ email: 'x' }, { email: 'email|username_available' });
27 // validator.stopOnError(true);
28 // validator.fails(function() {
29 // expect(validator.errors.get('email')).to.have.length(1);
30 // done();
31 // });
32
33 // });
34
35 it('only certain fields', function() {
36 var validator = new Validator({
37 email1: 'x',
38 email2: 'x'
39 }, {
40 email1: 'min:5|email',
41 email2: 'min:5|email'
42 });
43 validator.stopOnError(['email2']);
44 expect(validator.fails()).to.be.true;
45 expect(validator.errors.get('email1')).to.have.length(2);
46 expect(validator.errors.get('email2')).to.have.length(1);
47 });
48
49 it('should allow globally setting whether to stop on error', function() {
50 Validator.stopOnError(true);
51 var validator = new Validator({
52 email: 'x'
53 }, {
54 email: 'min:5|email'
55 });
56 expect(validator.fails()).to.be.true;
57 expect(validator.errors.get('email')).to.have.length(1);
58 Validator.stopOnError(false);
59 });
60
61 it('should always stop if field is implicit and cannot be validated', function() {
62 var validator = new Validator({
63 email: ''
64 }, {
65 email: 'required|email'
66 });
67 expect(validator.fails()).to.be.true;
68 expect(validator.errors.get('email')).to.have.length(1);
69 });
70});