Skip to content

Commit 02d53e3

Browse files
committed
Updates deps and README.md, adds methods to lib and new demo
1 parent b132476 commit 02d53e3

39 files changed

+20035
-12729
lines changed

README.md

+17-8
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@ Valido — A helper library for form validation built on top of .@chriso's valid
1515

1616
```javascript
1717

18-
const Valido = require('../lib/Valido.util');
19-
const idNumber = '$not_alpha_numeric_only#(u)123';
20-
21-
if (Valido.IsAlphaNumericOnly(idNumber) === true) {
22-
alert('Valid id');
23-
} else {
24-
alert('Not Valido');
25-
}
18+
const Valido = require("../lib/Valido.util");
19+
const expect = require('expect');
20+
21+
test('String does not meet specified length', () => {
22+
23+
const username = "bass";
24+
25+
const { valid, errors } = Valido.validateProperLength({
26+
value: username,
27+
minLength: 8,
28+
maxLength: 15
29+
});
30+
31+
expect(errors.length).toBeGreaterThan(0);
32+
expect(valid).toEqual(false);
33+
34+
});
2635

2736
```

lib/Valido.util.js

+154-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
const validator = require("validator");
22

3+
/**
4+
* @typedef {Object} TextInputProps
5+
* @property {string} value - The input value
6+
* @property {string} fieldName - The input label or labeled-by, used for validation message
7+
* @property {string[]} errors - A collection of the validation errors
8+
* @property {string} [required] - Indicates whether the input is required
9+
* @property {string} [minLength] - Specifies the minimum character length of an input, if present
10+
* @property {string} [maxLength] - Specifies the maximum character length of an input, if present
11+
* @property {boolean} dirty - Indicates whether the input has been edited by the user
12+
*/
13+
14+
/**
15+
* @typedef {Object} InputValidationResult
16+
* @property {boolean} valid - Indicates whether the input meets the validation tests
17+
* @property {string[]} errors - A collection of the validation errors
18+
*/
19+
320
/**
421
* Validation library for handling checks for
522
* valid Email, Password, Phone, SSN, TIN, Date (MM/dd/yyyy) and Alphanumeric formats,
@@ -125,7 +142,7 @@ class Valido {
125142
if (typeof value !== "string") return false;
126143

127144
const regex = new RegExp(/^po|^po box|^p.o.|^p.o. box|^post office box|^pmb|^private mailbox|^hc|^rr|hc [0-9]+ box [0-9]+|po box [0-9]+|rr [0-9]+ box [0-9]+/gim);
128-
return regex.test(value) == false;
145+
return regex.test(value) === false;
129146
}
130147

131148
/**
@@ -152,6 +169,142 @@ class Valido {
152169
return regex.test(value);
153170
}
154171

172+
173+
/**
174+
* Validates the email address given meets the appropriate format
175+
* @param {string} value
176+
*/
177+
static validateEmail(value) {
178+
let errors = [];
179+
180+
if (value.length > 0) {
181+
if (!Valido.IsEmail(value)) {
182+
errors.push("The value given is not a valid email address");
183+
}
184+
}
185+
186+
return { valid: errors.length === 0, errors };
187+
}
188+
189+
static validatePassword(value, name, props) {
190+
let errors = [];
191+
192+
if (value.length > 0) {
193+
if (
194+
!Valido.IsPassword(
195+
value,
196+
props[name].alphanumeric,
197+
props[name].containsUpperCase,
198+
props[name].minLength,
199+
props[name].maxLength
200+
)
201+
) {
202+
if ('containsUpperCase' in props[name] && Valido.containsUpperCase.test(value) === false) {
203+
errors.push(`Password must contain at least one uppercase characters`);
204+
}
205+
206+
if ('alphanumeric' in props[name] && Valido.containsNumbers.test(value) === false) {
207+
errors.push(`Password must contain at least one number`);
208+
}
209+
}
210+
if ('containsSpecialChar' in props[name] && Valido.containsSpecialChar.test(value) === false) {
211+
errors.push(`Password must contain at least one special character`);
212+
}
213+
}
214+
215+
return { valid: errors.length === 0, errors };
216+
}
217+
218+
/**
219+
* Validates if the input meets the specified min length and returns a collection of the validation errors
220+
* @param {TextInputProps} props
221+
* @returns {InputValidationResult}
222+
*/
223+
static validateMinLength(props) {
224+
let errors = [];
225+
226+
if (props.minLength && props.value.length < props.minLength && props.value.length > 0) {
227+
errors.push(`The field ${props.fieldName} must be at least ${props.minLength} characters long`);
228+
}
229+
230+
return { valid: errors.length === 0, errors };
231+
}
232+
233+
/**
234+
* Validates if the input meets the specified max length and returns a collection of the validation errors
235+
* @param {TextInputProps} props
236+
* @returns {InputValidationResult}
237+
*/
238+
static validateMaxLength(props) {
239+
let errors = [];
240+
241+
if (props.maxLength && props.value.length > props.maxLength) {
242+
errors.push(`The field ${props.fieldName} does not meet the maximum length`);
243+
}
244+
245+
return { valid: errors.length === 0, errors };
246+
}
247+
248+
/**
249+
* Validates if the input meets the specified min length and returns a collection of the validation errors
250+
* @param {TextInputProps} props
251+
* @returns {InputValidationResult}
252+
*/
253+
static validateProperLength(props) {
254+
let errors = [];
255+
256+
if (props.minLength && props.value.length < props.minLength && props.value.length > 0) {
257+
errors.push(`The field ${props.fieldName} must be at least ${props.minLength} characters long`);
258+
}
259+
260+
if (props.maxLength && props.value.length > props.maxLength) {
261+
errors.push(`The field ${props.fieldName} does not meet the maximum length`);
262+
}
263+
264+
return { valid: errors.length === 0, errors };
265+
}
266+
267+
/**
268+
* Validates if the input is required and has a value, and returns a valid flag along with a collection of the validation errors
269+
* @param {TextInputProps} props
270+
* @returns {InputValidationResult}
271+
*/
272+
static validateRequired(props) {
273+
let errors = [];
274+
275+
if (props.required && props.value.length === 0 && (props.dirty)) {
276+
errors.push(`The field ${props.fieldName} is required`);
277+
}
278+
279+
return { valid: errors.length === 0, errors };
280+
}
281+
282+
/**
283+
*
284+
* @param {{[x: string]: TextInputProps }} values
285+
* @returns {Promise<boolean>} result
286+
*/
287+
static isFormValid(values) {
288+
const inputKeys = Object.keys(values);
289+
290+
const result = inputKeys.every(function (propertyName) {
291+
292+
/**
293+
* @type {boolean}
294+
*/
295+
const propertyIsRequired = typeof values[propertyName] === "object" && values[propertyName].required;
296+
297+
// if not required then it evaluates to true, otherwise it checks that there are no errors and the input is not empty
298+
const isValid =
299+
!propertyIsRequired ||
300+
(values[propertyName].errors.length === 0 && values[propertyName].value.length > 0);
301+
302+
return isValid;
303+
});
304+
305+
return Promise.resolve(result);
306+
}
307+
155308
}
156309

157310
module.exports = Valido;

0 commit comments

Comments
 (0)