1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tools_1 = require("./tools");
|
4 | class Validator {
|
5 | constructor() {
|
6 | this.validator = require('validator');
|
7 | }
|
8 | createResponse(value, type) {
|
9 | if (value === true) {
|
10 | return tools_1.response(true, null, true);
|
11 | }
|
12 | else {
|
13 | return tools_1.response(false, `is not a valid "${type}"`, value);
|
14 | }
|
15 | }
|
16 | validateArray(items) {
|
17 | try {
|
18 | let success = true;
|
19 | const answer = {};
|
20 | for (let i = 0; i < items.length; i++) {
|
21 | const item = items[i];
|
22 | if (item.format.indexOf('|') > 0) {
|
23 | const ex = item.format.split('|');
|
24 | if (ex[0].toLowerCase() === 'array') {
|
25 | if (Array.isArray(item.value)) {
|
26 | for (let k = 0; k < item.value.length; k++) {
|
27 | const formats = JSON.parse(ex[1]);
|
28 | if ((typeof formats) === 'object') {
|
29 | const keys = Object.keys(formats);
|
30 | for (let j = 0; j < keys.length; j++) {
|
31 | const fName = 'is' + formats[keys[j]].charAt(0).toUpperCase() + formats[keys[j]].substr(1, formats[keys[j]].length);
|
32 | const res = this[fName](item.value[k][keys[j]]);
|
33 | if (!res.success) {
|
34 | success = false;
|
35 | const _key = `${item.key}[${j}].${keys[j]}`;
|
36 | answer[_key] = tools_1.response(false, `${_key} does not match expected format "${_key}: ${formats[keys[j]]}"`);
|
37 | }
|
38 | }
|
39 | }
|
40 | else {
|
41 | success = false;
|
42 | answer[item.key] = tools_1.response(false, `${item.key} does not match expected format "Array[${formats}]"`);
|
43 | }
|
44 | }
|
45 | }
|
46 | else {
|
47 | success = false;
|
48 | answer[item.key] = tools_1.response(false, `${item.key} does not match expected format "${item.format}"`);
|
49 | }
|
50 | }
|
51 | else {
|
52 | const formats = JSON.parse(ex[1]);
|
53 | if ((typeof formats) === 'object') {
|
54 | const keys = Object.keys(formats);
|
55 | for (let j = 0; j < keys.length; j++) {
|
56 | const fName = 'is' + formats[keys[j]].charAt(0).toUpperCase() + formats[keys[j]].substr(1, formats[keys[j]].length);
|
57 | const res = this[fName](item.value[keys[j]]);
|
58 | if (!res.success) {
|
59 | success = false;
|
60 | const _key = `${item.key}.${keys[j]}`;
|
61 | answer[_key] = tools_1.response(false, `${_key} does not match expected format "${_key}:${formats[keys[j]]}"`);
|
62 | }
|
63 | }
|
64 | }
|
65 | else {
|
66 | success = false;
|
67 | answer[item.key] = tools_1.response(false, `${item.key} does not match expected format "${formats}"`);
|
68 | }
|
69 | }
|
70 | }
|
71 | else {
|
72 | const fName = 'is' + item.format.charAt(0).toUpperCase() + item.format.substr(1, item.format.length);
|
73 | if (!this[fName]) {
|
74 | return tools_1.response(false, `${fName} is not a known validator.`);
|
75 | }
|
76 | const res = this[fName](item.value);
|
77 | if (!res.success) {
|
78 | success = false;
|
79 | answer[item.key] = tools_1.response(false, `${item.key} does not match expected format "${item.format}"`);
|
80 | }
|
81 | }
|
82 | }
|
83 | if (success) {
|
84 | return tools_1.response(true, null, true);
|
85 | }
|
86 | return tools_1.response(false, 'one or more fields do not match expected formats', answer);
|
87 | }
|
88 | catch (error) {
|
89 | console.log('107 tools.validation.validateArray()', error);
|
90 | return tools_1.response(false, error);
|
91 | }
|
92 | }
|
93 | _verifyAddress(address) {
|
94 | const validator = this;
|
95 | try {
|
96 | const err = [];
|
97 | if (!address.address1) {
|
98 | err.push('address1');
|
99 | }
|
100 | if (!address.zip || !validator.isNumber(address.zip.toString()).success) {
|
101 | err.push('zip code');
|
102 | }
|
103 | if (!address.city) {
|
104 | err.push('city');
|
105 | }
|
106 | if (!address.state) {
|
107 | err.push('state');
|
108 | }
|
109 | if (err.length < 1) {
|
110 | return tools_1.response(true, null, true);
|
111 | }
|
112 | return tools_1.response(false, `is not a valid 'address', field is not a correct format`, err);
|
113 | }
|
114 | catch (error) {
|
115 | console.log(error);
|
116 | return tools_1.response(false, error);
|
117 | }
|
118 | }
|
119 | isArray(value) {
|
120 | return this.createResponse(Array.isArray(value), 'array');
|
121 | }
|
122 | isAddress(address) {
|
123 | return this._verifyAddress(address);
|
124 | }
|
125 | isEmail(value) {
|
126 | return this.createResponse(this.validator.isEmail(value.toString()), 'email');
|
127 | }
|
128 | isString(value) {
|
129 | return this.createResponse(!!(typeof value === 'string'), 'string');
|
130 | }
|
131 | isSSN(value) {
|
132 | return this.createResponse(this.validator.matches(value.toString(), /^[0-9]{3}\-?[0-9]{2}\-?[0-9]{4}$/), 'SSN');
|
133 | }
|
134 | isLast4Digits(value) {
|
135 | return this.createResponse(this.validator.matches(value.toString(), /^\d{4}$/), 'last4Digits');
|
136 | }
|
137 | isIDnumber(value, state) {
|
138 | const regExSt = {
|
139 | 'NY': /^\d{9}$/,
|
140 | 'NJ': /^[A-Z]{1}\d{14}$/,
|
141 | 'FL': /^[A-Z]{1}\d{13}$/
|
142 | };
|
143 | if (Object.keys(regExSt).indexOf(state.toUpperCase()) < 0) {
|
144 | return tools_1.response(false, `"${state}"" verify rule not found`);
|
145 | }
|
146 | return this.createResponse(this.validator.matches(value.toString(), regExSt[state.toUpperCase()]), 'IDnumber');
|
147 | }
|
148 | isNumber(value) {
|
149 | return this.createResponse(this.validator.isNumeric(value.toString()), 'number');
|
150 | }
|
151 | isFloat(value) {
|
152 | return this.createResponse(this.validator.isFloat(value.toString()), 'float');
|
153 | }
|
154 | isCurrency(value) {
|
155 | return this.createResponse(this.validator.isCurrency(value.toString()), 'currency');
|
156 | }
|
157 | isBirthDate(value) {
|
158 | return this.createResponse(this.validator.matches(value.toString(), /^(0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])[\/\-]\d{4}$/gm), 'birthDate');
|
159 | }
|
160 | isISODate(value) {
|
161 | return this.createResponse(this.validator.isISO8601(value.toString()), 'ISO date');
|
162 | }
|
163 | isDate(value) {
|
164 | return this.createResponse(this.validator.matches(value.toString(), /^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/), 'date');
|
165 | }
|
166 | isDateTime(value) {
|
167 | return this.createResponse(this.validator.matches(value.toString(), /^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])T(\d{1,2}):(\d{2})(?::(\d{2}))?\.(\d{3})Z$/), 'dateTime');
|
168 | }
|
169 | isTime(value) {
|
170 | return this.createResponse(this.validator.matches(value.toString(), /^(\d{1,2}):(\d{2})$/), 'Time');
|
171 | }
|
172 | is12HourTime(value) {
|
173 | return this.createResponse(this.validator.matches(value.toString().toUpperCase(), /^(\d{1,2}):(\d{2})?(AM|PM)$/), 'Time');
|
174 | }
|
175 | isISOTime(value) {
|
176 | return this.createResponse(this.validator.matches(value.toString(), /^(\d{1,2}):(\d{2})(?::(\d{2}))?\.(\d{3})Z$/), 'ISO time');
|
177 | }
|
178 | isUUID(value) {
|
179 | return this.createResponse(this.validator.isUUID(value.toString()), 'UUID');
|
180 | }
|
181 | isPhoneNumber(value) {
|
182 | return this.createResponse(this.validator.matches(value.toString(), /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/i), 'PhoneNumber');
|
183 | }
|
184 | }
|
185 | exports.Validator = Validator;
|
186 |
|
\ | No newline at end of file |