UNPKG

1.54 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('alpha_num validation rule', function() {
10 it('should fail with non-alphanumeric characters', function() {
11 var validator = new Validator({
12 age: '$'
13 }, {
14 age: 'alpha_num'
15 });
16 expect(validator.fails()).to.be.true;
17 expect(validator.passes()).to.be.false;
18 expect(validator.errors.first('age')).to.equal('The age field must be alphanumeric.');
19 });
20
21 it('should pass with only alphanumeric characters', function() {
22 var validator = new Validator({
23 age: 'abc123'
24 }, {
25 age: 'alpha_num'
26 });
27 expect(validator.passes()).to.be.true;
28 expect(validator.fails()).to.be.false;
29 });
30
31 it('should pass with only numeric characters', function() {
32 var validator = new Validator({
33 age: 123
34 }, {
35 age: 'alpha_num'
36 });
37 expect(validator.passes()).to.be.true;
38 expect(validator.fails()).to.be.false;
39 });
40
41 it('should pass when the field is blank / optional', function() {
42 var validator = new Validator({
43 name: ''
44 }, {
45 name: 'alpha_num'
46 });
47 expect(validator.passes()).to.be.true;
48 });
49
50 it('should pass when the field does not exist', function() {
51 var validator = new Validator({}, {
52 name: 'alpha_num'
53 });
54 expect(validator.passes()).to.be.true;
55 expect(validator.fails()).to.be.false;
56 });
57});