1 | var should = require("chai").should();
|
2 |
|
3 | var { HelperValidate } = require("../../dist/index")
|
4 | var helperValidate = new HelperValidate(false)
|
5 | var mongo = require("mongojs")
|
6 |
|
7 | let moment = require('moment')
|
8 |
|
9 | describe("Helper Validate", () => {
|
10 |
|
11 | describe("#range()", () => {
|
12 | it("should validate if the data passed is within the given range", () => {
|
13 | helperValidate.range(12, 1, 13).should.be.ok;
|
14 | helperValidate.range(12, 0, 10).should.not.be.ok;
|
15 | })
|
16 | })
|
17 |
|
18 | describe("#length()", () => {
|
19 | it("should validate only for greater than min if max is not passed", () => {
|
20 | helperValidate.length("asdf", 3).should.be.ok;
|
21 | })
|
22 | it("should validate the length of the array/string for the given range", () => {
|
23 | helperValidate.length("asdf", 3, 5).should.be.ok;
|
24 | helperValidate.length([1, 2, 3, 4], 2, 3).should.not.be.ok;
|
25 | })
|
26 | })
|
27 |
|
28 | describe("#isMongoId()", () => {
|
29 | it("should return true if the given data is a valid mongo id", () => {
|
30 | var id = mongo.ObjectId()
|
31 | helperValidate.isMongoId(id).should.be.ok;
|
32 | })
|
33 | it("should return false if the given data is an invalid mongoid", () => {
|
34 | helperValidate.isMongoId("asdf").should.not.be.ok;
|
35 | })
|
36 | })
|
37 |
|
38 | describe("#in()", () => {
|
39 | it("should validate if the data exists in the given array", () => {
|
40 | var arr = [1, 2, 3, 4, 5]
|
41 | helperValidate.in(2, arr).should.be.ok
|
42 | helperValidate.in(6, arr).should.not.be.ok;
|
43 | })
|
44 | })
|
45 |
|
46 | describe("#isName()", () => {
|
47 | it("should allow only alphabets, numberic and (_-.)", () => {
|
48 | var name = "asdfASDF -_.0123"
|
49 | helperValidate.isName(name).should.be.ok
|
50 | })
|
51 | it("should return false if any other special characters are used", () => {
|
52 | var name = "asdfASDF? -_,0123"
|
53 | helperValidate.isName(name).should.not.be.ok
|
54 | })
|
55 | })
|
56 |
|
57 | describe("#isEmail()", () => {
|
58 | it("should validate for email string", () => {
|
59 | helperValidate.isEmail("test.test@in.test.com").should.be.ok
|
60 | helperValidate.isEmail("test@test.org").should.be.ok
|
61 | helperValidate.isEmail("@mail.com").should.not.be.ok
|
62 | helperValidate.isEmail("test@.com").should.not.be.ok;
|
63 | helperValidate.isEmail("test@mail.c").should.not.be.ok;
|
64 | })
|
65 | })
|
66 |
|
67 | describe("#isAlpha()", () => {
|
68 | it("should validate only for alphabets", () => {
|
69 | helperValidate.isAlpha("asdfASD").should.be.ok;
|
70 | helperValidate.isAlpha("asdfASD1324").should.not.be.ok;
|
71 | })
|
72 | })
|
73 |
|
74 | describe("#isNumeric()", () => {
|
75 | it("should validate numbers", () => {
|
76 | var i = 23456
|
77 | helperValidate.isNumeric(i).should.be.ok
|
78 | })
|
79 | it("should validate for floating point numbers", () => {
|
80 | var i = 23.456
|
81 | helperValidate.isNumeric(i).should.be.ok
|
82 | })
|
83 | it("should not validate if the string is not numeric", () => {
|
84 | var str = "asdf"
|
85 | helperValidate.isNumeric(str).should.not.be.ok
|
86 | })
|
87 | })
|
88 |
|
89 | describe("#isAlphaNumeric()", () => {
|
90 | it("should validate only for alpha numeric", () => {
|
91 | helperValidate.isAlphaNumeric("asdfASD").should.be.ok;
|
92 | helperValidate.isAlphaNumeric("asdf").should.be.ok;
|
93 | helperValidate.isAlphaNumeric("asdfASD1234").should.be.ok;
|
94 | helperValidate.isAlphaNumeric("asdfASD1234?").should.not.be.ok;
|
95 | })
|
96 | })
|
97 |
|
98 | describe("#isDate()", () => {
|
99 | it("should validate the string against the given date format", () => {
|
100 | helperValidate.isDate("2017", "YYYY").should.be.ok
|
101 | helperValidate.isDate("2017", "MM-DD").should.not.be.ok
|
102 | })
|
103 | })
|
104 |
|
105 | describe("#isRegex()", () => {
|
106 | it("should validate the string for the given expression", () => {
|
107 | helperValidate.isRegex("asdfASDF", "[asdf]*[ASDF]*").should.be.ok
|
108 | helperValidate.isRegex("asdfASDF1234", "[asdf]*[ASDF]*").should.be.ok
|
109 | helperValidate.isRegex("asdfASDF1234", "^[asdf]*[ASDF]*$").should.not.be.ok
|
110 | })
|
111 | })
|
112 |
|
113 | })
|