Skip to content

Commit 9674b27

Browse files
committed
add isString()
1 parent e15a539 commit 9674b27

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

validate.js

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ module.exports = function(app) {
5454

5555
};
5656

57+
function isString(s) {
58+
if(null == s)return false;
59+
return 'string' == typeof(s)?true:false
60+
}
5761

5862
var v = require('validator');
5963

@@ -148,13 +152,13 @@ Validator.prototype.ensure = function(assertion, tip, shouldBail) {
148152
};
149153

150154
Validator.prototype.isInt = function(tip) {
151-
if (this.goOn&& ("number" != typeof this.value && !v.isInt(this.value))) {
155+
if (this.goOn&& ("number" != typeof this.value && (!isString(this.value) || !v.isInt(this.value)))) {
152156
this.addError(tip || this.key + " is not integer.");
153157
}
154158
return this;
155159
};
156160
Validator.prototype.isFloat = function(tip) {
157-
if (this.goOn && ("number" != typeof this.value && !v.isFloat(this.value))) {
161+
if (this.goOn && ("number" != typeof this.value && (!isString(this.value) ||!v.isFloat(this.value)))) {
158162
this.addError(tip || this.key + " is not float.");
159163
}
160164
return this;
@@ -227,92 +231,92 @@ Validator.prototype.le = function(l, tip) {
227231
return this;
228232
};
229233
Validator.prototype.contains = function(s, tip) {
230-
if (this.goOn && !v.contains(this.value,s)) {
234+
if (this.goOn && (!isString(this.value) ||!v.contains(this.value,s))) {
231235
this.addError(tip || this.key + " is must contain " + s + ".");
232236
}
233237
return this;
234238
};
235239
Validator.prototype.notContains = function(s, tip) {
236-
if (this.goOn && v.contains(this.value,s)) {
240+
if (this.goOn && (!isString(this.value) ||v.contains(this.value,s))) {
237241
this.addError(tip || this.key + " is must not contain " + s + ".");
238242
}
239243
return this;
240244
};
241245
Validator.prototype.isEmail = function(tip,options) {
242-
if (this.goOn && !v.isEmail(this.value,options)) {
246+
if (this.goOn && (!isString(this.value) ||!v.isEmail(this.value,options))) {
243247
this.addError(tip || this.key + " is not email format.");
244248
}
245249
return this;
246250
};
247251
Validator.prototype.isUrl = function(tip,options) {
248-
if (this.goOn && !v.isURL(this.value,options)) {
252+
if (this.goOn && (!isString(this.value) ||!v.isURL(this.value,options))) {
249253
this.addError(tip || this.key + " is not url format.");
250254
}
251255
return this;
252256
};
253257
Validator.prototype.isIp = function(tip,version) {
254-
if (this.goOn && !v.isIP(this.value,version)) {
258+
if (this.goOn && (!isString(this.value) ||!v.isIP(this.value,version))) {
255259
this.addError(tip || this.key + " is not ip format.");
256260
}
257261
return this;
258262
};
259263
Validator.prototype.isAlpha = function(tip,locale) {
260-
if (this.goOn && !v.isAlpha(this.value,locale)) {
264+
if (this.goOn && (!isString(this.value) ||!v.isAlpha(this.value,locale))) {
261265
this.addError(tip || this.key + " is not an alpha string.");
262266
}
263267
return this;
264268
};
265269
Validator.prototype.isNumeric = function(tip) {
266-
if (this.goOn && !v.isNumeric(this.value)) {
270+
if (this.goOn && (!isString(this.value) ||!v.isNumeric(this.value))) {
267271
this.addError(tip || this.key + " is not numeric.");
268272
}
269273
return this;
270274
};
271275

272276
Validator.prototype.isAlphanumeric = function(tip,locale) {
273-
if (this.goOn && !v.isAlphanumeric(this.value,locale)) {
277+
if (this.goOn && (!isString(this.value) ||!v.isAlphanumeric(this.value,locale))) {
274278
this.addError(tip || this.key + " is not an aphanumeric string.");
275279
}
276280
return this;
277281
};
278282
Validator.prototype.isBase64 = function(tip) {
279-
if (this.goOn && !v.isBase64(this.value)) {
283+
if (this.goOn && (!isString(this.value) ||!v.isBase64(this.value))) {
280284
this.addError(tip || this.key + " is not a base64 string.");
281285
}
282286
return this;
283287
};
284288
Validator.prototype.isHexadecimal = function(tip) {
285-
if (this.goOn && !v.isHexadecimal(this.value)) {
289+
if (this.goOn && (!isString(this.value) ||!v.isHexadecimal(this.value))) {
286290
this.addError(tip || this.key + " is not a hexa decimal string.");
287291
}
288292
return this;
289293
};
290294
Validator.prototype.isHexColor = function(tip) {
291-
if (this.goOn && !v.isHexColor(this.value)) {
295+
if (this.goOn && (!isString(this.value) ||!v.isHexColor(this.value))) {
292296
this.addError(tip || this.key + " is not hex color format.");
293297
}
294298
return this;
295299
};
296300
Validator.prototype.isLowercase = function(tip) {
297-
if (this.goOn && !v.isLowercase(this.value)) {
301+
if (this.goOn && (!isString(this.value) ||!v.isLowercase(this.value))) {
298302
this.addError(tip || this.key + " is not a lowwer case string");
299303
}
300304
return this;
301305
};
302306
Validator.prototype.isUppercase = function(tip) {
303-
if (this.goOn && !v.isUppercase(this.value)) {
307+
if (this.goOn && (!isString(this.value) ||!v.isUppercase(this.value))) {
304308
this.addError(tip || this.key + " is not a upper case string.");
305309
}
306310
return this;
307311
};
308312
Validator.prototype.isDivisibleBy = function(n, tip) {
309-
if (this.goOn && !v.isDivisibleBy(this.value, n)) {
313+
if (this.goOn && (!isString(this.value) ||!v.isDivisibleBy(this.value, n))) {
310314
this.addError(tip || this.key + " can not divide by" + n + ".");
311315
}
312316
return this;
313317
};
314318
Validator.prototype.isNull = function(tip) {
315-
if (this.goOn && !v.isNull(this.value)) {
319+
if (this.goOn && (!isString(this.value) ||!v.isNull(this.value))) {
316320
this.addError(tip || this.key + " is not null.");
317321
}
318322
return this;
@@ -333,13 +337,13 @@ Validator.prototype.isByteLength = function(min, max,charset,tip) {
333337
};
334338
Validator.prototype.byteLength = Validator.prototype.isByteLength;
335339
Validator.prototype.isUUID = function(tip,ver) {
336-
if (this.goOn && !v.isUUID(this.value,ver)) {
340+
if (this.goOn && (!isString(this.value) ||!v.isUUID(this.value,ver))) {
337341
this.addError(tip || this.key + " is not a UUID format.");
338342
}
339343
return this;
340344
};
341345
Validator.prototype.isDate = function(tip) {
342-
if (this.goOn && !util.isDate(this.value) && !v.isDate(this.value)) {
346+
if (this.goOn && !util.isDate(this.value) && (!isString(this.value) ||!v.isDate(this.value))) {
343347
this.addError(tip || this.key + " is not a date format.");
344348
}
345349
return this;
@@ -353,111 +357,111 @@ Validator.prototype.isTime = function(tip) {
353357
};
354358

355359
Validator.prototype.isAfter = function(d, tip) {
356-
if (this.goOn && !v.isAfter(this.value, d)) {
360+
if (this.goOn && (!isString(this.value) ||!v.isAfter(this.value, d))) {
357361
this.addError(tip || this.key + " must after " + d + ".");
358362
}
359363
return this;
360364
};
361365
Validator.prototype.isBefore = function(d, tip) {
362-
if (this.goOn && !v.isBefore(this.value, d)) {
366+
if (this.goOn && (!isString(this.value) ||!v.isBefore(this.value, d))) {
363367
this.addError(tip || this.key + " must before " + d + ".");
364368
}
365369
return this;
366370
};
367371
Validator.prototype.isCreditCard = function(tip) {
368-
if (this.goOn && !v.isCreditCard(this.value)) {
372+
if (this.goOn && (!isString(this.value) ||!v.isCreditCard(this.value))) {
369373
this.addError(tip || this.key + " is not credit card format.");
370374
}
371375
return this;
372376
};
373377
Validator.prototype.isISBN = function(tip,version) {
374-
if (this.goOn && !v.isISBN(this.value,version)) {
378+
if (this.goOn && (!isString(this.value) ||!v.isISBN(this.value,version))) {
375379
this.addError(tip || this.key + " is not a ISBN format.");
376380
}
377381
return this;
378382
};
379383
Validator.prototype.isJSON = function(tip) {
380-
if (this.goOn && !v.isJSON(this.value)) {
384+
if (this.goOn && (!isString(this.value) ||!v.isJSON(this.value))) {
381385
this.addError(tip || this.key + " is not a json format.");
382386
}
383387
return this;
384388
};
385389

386390
Validator.prototype.isMultibyte = function(tip) {
387-
if (this.goOn && !v.isMultibyte(this.value)) {
391+
if (this.goOn && (!isString(this.value) ||!v.isMultibyte(this.value))) {
388392
this.addError(tip || this.key + " is not a multibyte string.");
389393
}
390394
return this;
391395
};
392396
Validator.prototype.isAscii = function(tip) {
393-
if (this.goOn && !v.isAscii(this.value)) {
397+
if (this.goOn && (!isString(this.value) ||!v.isAscii(this.value))) {
394398
this.addError(tip || this.key + " is not a ascii string.");
395399
}
396400
return this;
397401
};
398402
Validator.prototype.isFullWidth = function(tip) {
399-
if (this.goOn && !v.isFullWidth(this.value)) {
403+
if (this.goOn && (!isString(this.value) ||!v.isFullWidth(this.value))) {
400404
this.addError(tip || this.key + " is not a full width string.");
401405
}
402406
return this;
403407
};
404408
Validator.prototype.isHalfWidth = function(tip) {
405-
if (this.goOn && !v.isHalfWidth(this.value)) {
409+
if (this.goOn && (!isString(this.value) ||!v.isHalfWidth(this.value))) {
406410
this.addError(tip || this.key + " is not a half width string.");
407411
}
408412
return this;
409413
};
410414
Validator.prototype.isVariableWidth = function(tip) {
411-
if (this.goOn && !v.isVariableWidth(this.value)) {
415+
if (this.goOn && (!isString(this.value) ||!v.isVariableWidth(this.value))) {
412416
this.addError(tip || this.key + " is not a variable width string.");
413417
}
414418
return this;
415419
};
416420
Validator.prototype.isSurrogatePair = function(tip) {
417-
if (this.goOn && !v.isSurrogatePair(this.value)) {
421+
if (this.goOn && (!isString(this.value) ||!v.isSurrogatePair(this.value))) {
418422
this.addError(tip || this.key + " is not a surrogate pair string.");
419423
}
420424
return this;
421425
};
422426
Validator.prototype.isCurrency = function(tip,options) {
423-
if (this.goOn && !v.isCurrency(this.value,options)) {
427+
if (this.goOn && (!isString(this.value) ||!v.isCurrency(this.value,options))) {
424428
this.addError(tip || this.key + " is not a currency format.");
425429
}
426430
return this;
427431
};
428432
Validator.prototype.isDataURI = function(tip) {
429-
if (this.goOn && !v.isDataURI(this.value)) {
433+
if (this.goOn && (!isString(this.value) ||!v.isDataURI(this.value))) {
430434
this.addError(tip || this.key + " is not a data uri format.");
431435
}
432436
return this;
433437
};
434438
Validator.prototype.isMobilePhone = function(tip,locale) {
435-
if (this.goOn && !v.isMobilePhone(this.value,locale)) {
439+
if (this.goOn && (!isString(this.value) ||!v.isMobilePhone(this.value,locale))) {
436440
this.addError(tip || this.key + " is not a mobile phone format.");
437441
}
438442
return this;
439443
};
440444
Validator.prototype.isISO8601 = function(tip) {
441-
if (this.goOn && !v.isISO8601(this.value)) {
445+
if (this.goOn && (!isString(this.value) ||!v.isISO8601(this.value))) {
442446
this.addError(tip || this.key + " is not a ISO8601 string format.");
443447
}
444448
return this;
445449
};
446450
Validator.prototype.isMACAddress = function(tip) {
447-
if (this.goOn && !v.isMACAddress(this.value)) {
451+
if (this.goOn && (!isString(this.value) ||!v.isMACAddress(this.value))) {
448452
this.addError(tip || this.key + " is not a MAC address format.");
449453
}
450454
return this;
451455
};
452456

453457
Validator.prototype.isISIN = function(tip) {
454-
if (this.goOn && !v.isISIN(this.value)) {
458+
if (this.goOn && (!isString(this.value) ||!v.isISIN(this.value))) {
455459
this.addError(tip || this.key + " is not a ISIN format.");
456460
}
457461
return this;
458462
};
459463
Validator.prototype.isFQDN = function(tip,options) {
460-
if (this.goOn && !v.isFQDN(this.value,options)) {
464+
if (this.goOn && (!isString(this.value) ||!v.isFQDN(this.value,options))) {
461465
this.addError(tip || this.key + " is not a fully qualified domain name format.");
462466
}
463467
return this;

0 commit comments

Comments
 (0)