UNPKG

7.42 kBJavaScriptView Raw
1var Valida = require('../');
2var expect = require('chai').expect;
3
4describe('sanitizers', function () {
5 describe('toInt', function () {
6 var schema = {
7 age: [
8 { sanitizer: Valida.Sanitizer.toInt }
9 ],
10 };
11
12 describe('given is a string data of a valid integer number', function () {
13 it('should set the field with the converted int value', function (done) {
14 var data = { age: '50' };
15
16 Valida.process(data, schema, function(err, ctx) {
17 if (err) return done(err);
18 expect(data.age).to.eql(50);
19 done();
20 });
21 });
22 });
23
24 describe('given is a string data of a invalid integer number', function () {
25 it('should set the field with the result of the failed convertion', function (done) {
26 var data = { age: 'x50' };
27
28 Valida.process(data, schema, function(err, ctx) {
29 if (err) return done(err);
30 expect(data.age).to.be.NaN;
31 done();
32 });
33 });
34 });
35 });
36
37 describe('toFloat', function () {
38 var schema = {
39 price: [
40 { sanitizer: Valida.Sanitizer.toFloat }
41 ],
42 };
43
44 describe('given is a string data of a valid float number', function () {
45 it('should set the field with the converted float value', function (done) {
46 var data = { price: '50.10' };
47
48 Valida.process(data, schema, function(err, ctx) {
49 if (err) return done(err);
50 expect(data.price).to.eql(50.10);
51 done();
52 });
53 });
54 });
55
56 describe('given is a string data of a invalid float number', function () {
57 it('should set the field with the result of the failed convertion', function (done) {
58 var data = { price: 'x50' };
59
60 Valida.process(data, schema, function(err, ctx) {
61 if (err) return done(err);
62 expect(data.price).to.be.NaN;
63 done();
64 });
65 });
66 });
67 });
68
69 describe('toDate', function () {
70 var schema = {
71 createdAt: [
72 { sanitizer: Valida.Sanitizer.toDate }
73 ],
74 };
75
76 describe('given is a string data of a valid date', function () {
77 it('should set the field with the converted date value', function (done) {
78 var date = new Date();
79 var data = { createdAt: date.toISOString() };
80
81 Valida.process(data, schema, function(err, ctx) {
82 if (err) return done(err);
83 expect(data.createdAt).to.eql(date);
84 done();
85 });
86 });
87 });
88
89 describe('given is a string data of a invalid date', function () {
90 it('should set the field with the result of the failed convertion', function (done) {
91 var data = { createdAt: '----' };
92
93 Valida.process(data, schema, function(err, ctx) {
94 if (err) return done(err);
95 expect(data.createdAt).to.be.NaN;
96 done();
97 });
98 });
99 });
100 });
101
102 describe('trim', function () {
103 var schema = {
104 name: [
105 { sanitizer: Valida.Sanitizer.trim }
106 ],
107 };
108
109 describe('given is a string data with whitespaces around it', function () {
110 it('should set the field with no whitespaces arount id', function (done) {
111 var data = { name: ' Jack ' };
112
113 Valida.process(data, schema, function(err, ctx) {
114 if (err) return done(err);
115 expect(data.name).to.eql('Jack');
116 done();
117 });
118 });
119 });
120 });
121
122 describe('string', function () {
123 var schema = {
124 age: [
125 { sanitizer: Valida.Sanitizer.string }
126 ],
127 };
128
129 describe('given is a interger data', function () {
130 it('should set the field with the converted string value', function (done) {
131 var data = { age: 100 };
132
133 Valida.process(data, schema, function(err, ctx) {
134 if (err) return done(err);
135 expect(data.age).to.eql('100');
136 done();
137 });
138 });
139 });
140 });
141
142 describe('lowerCase', function () {
143 var schema = {
144 text: [
145 { sanitizer: Valida.Sanitizer.lowerCase }
146 ],
147 };
148
149 describe('given is a string data with letters in all diferent cases', function () {
150 it('should set the field with lower case', function (done) {
151 var data = { text: 'My Name is Jack. OK?!' };
152
153 Valida.process(data, schema, function(err, ctx) {
154 if (err) return done(err);
155 expect(data.text).to.eql('my name is jack. ok?!');
156 done();
157 });
158 });
159 });
160 });
161
162 describe('titleCase', function () {
163 var schema = {
164 text: [
165 { sanitizer: Valida.Sanitizer.titleCase }
166 ],
167 };
168
169 describe('given is a string data with letters in all diferent cases', function () {
170 it('should set the field with title case', function (done) {
171 var data = { text: 'my name is Jack. OK?!' };
172
173 Valida.process(data, schema, function(err, ctx) {
174 if (err) return done(err);
175 expect(data.text).to.eql('My Name Is Jack. OK?!');
176 done();
177 });
178 });
179 });
180 });
181
182 describe('upperCaseFirst', function () {
183 var schema = {
184 text: [
185 { sanitizer: Valida.Sanitizer.upperCaseFirst }
186 ],
187 };
188
189 describe('given is a string data with letters in all diferent cases', function () {
190 it('should set the field with title case', function (done) {
191 var data = { text: 'my name is Jack. OK?!' };
192
193 Valida.process(data, schema, function(err, ctx) {
194 if (err) return done(err);
195 expect(data.text).to.eql('My name is Jack. OK?!');
196 done();
197 });
198 });
199 });
200 });
201
202 describe('upperCase', function () {
203 var schema = {
204 text: [
205 { sanitizer: Valida.Sanitizer.upperCase }
206 ],
207 };
208
209 describe('given is a string data with letters in all diferent cases', function () {
210 it('should set the field with title case', function (done) {
211 var data = { text: 'my name is Jack. OK?!' };
212
213 Valida.process(data, schema, function(err, ctx) {
214 if (err) return done(err);
215 expect(data.text).to.eql('MY NAME IS JACK. OK?!');
216 done();
217 });
218 });
219 });
220 });
221
222 describe('toBool', function () {
223 var schema = {
224 published: [
225 { sanitizer: Valida.Sanitizer.toBool }
226 ],
227 };
228
229 describe('given is a string "true"', function () {
230 it('should set the field with "true" bool value', function (done) {
231 var data = { published: 'true' };
232
233 Valida.process(data, schema, function(err, ctx) {
234 if (err) return done(err);
235 expect(data.published).to.eql(true);
236 done();
237 });
238 });
239 });
240
241 describe('given is a string "false"', function () {
242 it('should set the field with "false" bool value', function (done) {
243 var data = { published: 'false' };
244
245 Valida.process(data, schema, function(err, ctx) {
246 if (err) return done(err);
247 expect(data.published).to.eql(false);
248 done();
249 });
250 });
251 });
252
253 describe('given is a string with an invalid bool value', function () {
254 it('should set the field with "false" bool value', function (done) {
255 var data = { published: 'NOPE' };
256
257 Valida.process(data, schema, function(err, ctx) {
258 if (err) return done(err);
259 expect(data.published).to.eql(false);
260 done();
261 });
262 });
263 });
264 });
265});