UNPKG

1.97 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('implicit rule tests', function() {
10
11 it('should fail implicit rule even when undefined', function() {
12 Validator.registerImplicit('null_or_number', function(val) {
13 return (val && val.match(/^\d*$/)) || val === null;
14 },':attribute must be a number or empty');
15
16 var validator = new Validator({/* empty */},{value:'null_or_number'});
17 expect(validator.passes()).to.be.false;
18 });
19
20 it('should pass implicit rule even when null', function() {
21 Validator.registerImplicit('null_or_number', function(val) {
22 return (val && val.match(/^\d*$/)) || val === null;
23 },':attribute must be a number or empty');
24
25 var validator = new Validator({value:null},{value:'null_or_number'});
26 expect(validator.passes()).to.be.true;
27 });
28
29 it('should fail async implicit rule even when undefined', function(done) {
30 Validator.registerAsyncImplicit('async_null',
31 function(value, attribute, req, passes) {
32 setTimeout(function() {
33 if (value === null) {
34 passes(true);
35 }
36 else{
37 passes(false);
38 }
39 }, 50);
40 }, ':attribute already taken');
41
42 var validator = new Validator({ /* empty */}, { value: 'async_null' });
43 validator.fails(done);
44 });
45
46 it('should pass async implicit rule even when null', function(done) {
47 Validator.registerAsyncImplicit('async_null',
48 function(value, attribute, req, passes) {
49 setTimeout(function() {
50 if (value === null) {
51 passes(true);
52 }
53 else{
54 passes(false);
55 }
56 }, 50);
57 }, ':attribute already taken');
58
59 var validator = new Validator({ value: null }, { value: 'async_null' });
60 validator.passes(done);
61 });
62});