1 | if (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 |
|
9 | describe('same validation rule', function() {
|
10 | it('should fail when the 2 attributes are different', function() {
|
11 | var validator = new Validator({
|
12 | pw: 'abc123',
|
13 | pw2: 'abc1234'
|
14 | }, {
|
15 | pw2: 'same:pw'
|
16 | });
|
17 | expect(validator.fails()).to.be.true;
|
18 | expect(validator.passes()).to.be.false;
|
19 | expect(validator.errors.first('pw2')).to.equal('The pw2 and pw fields must match.');
|
20 | });
|
21 |
|
22 | it('should fail when the the comparing attribute doesnt exist', function() {
|
23 | var validator = new Validator({
|
24 | pw2: 'abc1234'
|
25 | }, {
|
26 | pw2: 'same:pw'
|
27 | });
|
28 | expect(validator.fails()).to.be.true;
|
29 | expect(validator.passes()).to.be.false;
|
30 | expect(validator.errors.first('pw2')).to.equal('The pw2 and pw fields must match.');
|
31 | });
|
32 |
|
33 | it('should pass when the 2 attributes are equal', function() {
|
34 | var validator = new Validator({
|
35 | pw: 'abc123',
|
36 | pw2: 'abc123'
|
37 | }, {
|
38 | pw2: 'same:pw'
|
39 | });
|
40 | expect(validator.passes()).to.be.true;
|
41 | expect(validator.fails()).to.be.false;
|
42 | });
|
43 |
|
44 | it('should pass if one of the 2 attributes is a nested path', function() {
|
45 | var validator = new Validator({
|
46 | payload: {
|
47 | pw: 'abc123',
|
48 | username: 'test',
|
49 | },
|
50 | username: 'test',
|
51 | }, {
|
52 | username: 'same:payload.username'
|
53 | });
|
54 | expect(validator.passes()).to.be.true;
|
55 | expect(validator.fails()).to.be.false;
|
56 | });
|
57 |
|
58 | it('should fail if one of the 2 attributes is an invalid nested path', function() {
|
59 | var validator = new Validator({
|
60 | payload: {
|
61 | pw: 'abc123',
|
62 | username: 'test123',
|
63 | },
|
64 | username: 'test',
|
65 | }, {
|
66 | username: 'same:payload.username'
|
67 | });
|
68 | expect(validator.fails()).to.be.true;
|
69 | expect(validator.passes()).to.be.false;
|
70 | expect(validator.errors.first('username')).to.equal('The username and payload.username fields must match.');
|
71 | });
|
72 | });
|