UNPKG

2.24 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('require validation pass rules', function() {
10 it('should pass with non-empty strings', function() {
11 var validator = new Validator({
12 name: 'David'
13 }, {
14 name: 'required'
15 });
16 expect(validator.passes()).to.be.true;
17 });
18
19 it('should fail with empty strings', function() {
20 var validator = new Validator({
21 email: ''
22 }, {
23 email: 'required'
24 });
25 expect(validator.fails()).to.be.true;
26 });
27
28 it('should fail with strings containing only white space', function() {
29 var validator = new Validator({
30 name: ' '
31 }, {
32 name: 'required'
33 });
34 expect(validator.fails()).to.be.true;
35 });
36
37 it('should fail when a value is equal to undefined', function() {
38 var validator = new Validator({
39 name: undefined
40 }, {
41 name: 'required'
42 });
43 expect(validator.fails()).to.be.true;
44 });
45
46 it('should fail when a value is equal to null', function() {
47 var validator = new Validator({
48 name: null
49 }, {
50 name: 'required'
51 });
52 expect(validator.fails()).to.be.true;
53 });
54
55 it('should pass when a value is numeric', function() {
56 var validator = new Validator({
57 age: 29
58 }, {
59 age: 'required'
60 });
61 expect(validator.passes()).to.be.true;
62 });
63
64 it('should fail when the attribute is not passed in', function() {
65 var validator = new Validator({}, {
66 email: 'required'
67 });
68 expect(validator.fails()).to.be.true;
69 expect(validator.passes()).to.be.false;
70 });
71
72 it('should fail when the array is empty', function() {
73 var validator = new Validator({
74 users: []
75 }, {
76 users: 'required|array'
77 });
78 expect(validator.fails()).to.be.true;
79 expect(validator.passes()).to.be.false;
80 });
81
82 it('should not fail when not an empty array', function() {
83 var validator = new Validator({
84 users: [false]
85 }, {
86 users: 'required|array'
87 });
88 expect(validator.passes()).to.be.true;
89 expect(validator.fails()).to.be.false;
90 });
91});