Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* @fileOverview Class to validate user input for order
* @class Orders validator
* @exports ValidateOrders
*/
class ValidateOrder {
/**
* validate orders input validity
* @param {Object} request
* @param {Object} response
*
* @callback {Function} next
*
* @return {Object} json
*/
static validateOrder(request, response, next) {
const {
recipient,
recipientEmail,
recipientPhoneNumber,
recipientAddress,
items,
} = request.body;
const errors = {};
const itemErrors = [];
const rules = {
validRecipient: /^[a-zA-Z][a-zA-Z\s]+$/,
validAddress: /^[a-zA-Z][a-zA-Z0-9\s?.,:]+$/,
empty: /^(\S+)/,
addressLength: /^[a-zA-Z][a-zA-Z0-9\s?.,:]{10,150}$/,
validEmail: /^[A-Za-z]\w{3,15}@\w{2,15}[.]\w{2,15}$/,
recipientLength: /^[a-zA-Z][a-zA-Z\s]{8,}$/,
validNumber: /^[0-9]{8,15}/,
validId: /^[1-9]{1,}/,
};
const recipientRequired = 'Sorry! the full name field is required';
const recipientLength = 'Sorry your fullname cannot be less than 8 characters and must contain a space';
const validRecipient = 'Please enter a valid full name. Your full name can only contain letters and spaces';
const addressRequired = 'Sorry! the Address field is required';
const invalidAddrsLength = 'Sorry your address cannot be less than 10 characters';
const validAddress = 'Please enter a valid Address';
const emailRequired = 'Sorry! the Email field is required';
const validEmail = 'Please enter a valid email address';
const phoneNumberRequired = 'Sorry! the phone number field is required';
const invalidPhoneNumber = 'Please your phone number can only contain numbers and cannot be greater than 15 or less than 8 characters';
const itemsEmpty = 'Sorry! your order is invalid. You did not pick any items?';
const validItem = 'Sorry the items are invalid. Enter valid items';
const validItemObject = 'Sorry the items are invalid. Valid items must be objects.';
errors.recipientRequired = (!recipient || !rules.empty.test(recipient))?recipientRequired: null;
errors.recipientLength = (!rules.recipientLength.test(recipient)) ? recipientLength : null ;
errors.validRecipient = (!rules.validRecipient.test(recipient)) ? validRecipient : null;
errors.addressRequired = (!recipientAddress || !rules.empty.test(recipientAddress)) ? addressRequired: null;
errors.addressLength = (!rules.addressLength.test(recipientAddress)) ? invalidAddrsLength : null;
errors.validAddress = (!rules.validAddress.test(recipientAddress)) ? validAddress : null;
errors.emailRequired = (!recipientEmail || !rules.empty.test(recipientEmail)) ? emailRequired : null;
errors.validEmail = (!rules.validEmail.test(recipientEmail)) ? validEmail : null;
errors.phoneRequired = (!recipientPhoneNumber || !rules.empty.test(recipientPhoneNumber)) ? phoneNumberRequired : null;
errors.validNumber = (!rules.validNumber.test(recipientPhoneNumber)) ? invalidPhoneNumber : null;
errors.itemsEmpty = validateItem(items, rules);
errors.validItems = validateItemObject(items);
function validateItem(items, rules) {
if(!items || rules.empty.test(items)) {
return itemsEmpty;
}
}
function validateItemObject(items) {
if (typeof items !== 'object') {
return validItem;
}
if (typeof items[0] !== 'object') {
return validItemObject;
}
}
items.forEach((item) => {
if (!Number.isInteger(item.quantity) || item.quantity < 1) {
itemErrors.push('The quantity of the an item must be a number greater than zero');
}
});
if (itemErrors.length > 0) {
errors.itemErrors = itemErrors;
}
if (Object.keys(errors).length > 0) {
return response.status(406).json({
statusCode: 406,
success: false,
error: errors,
});
}
return next();
}
static validateOrderStatus(request, response, next) {
const errors = {};
const validId = /^[1-9]{1,}/;
let { orderStatus } = request.body;
const { orderId } = request.params;
orderStatus = orderStatus.toString();
if (!validId.test(orderId)) {
errors.validId = 'Please the orderId must be a number greater than zero';
}
if (orderStatus) {
if (orderStatus !== 'Canceled' && orderStatus !== 'Declined' && orderStatus !== 'Accepted' && orderStatus !== 'Completed') {
errors.validStatus = 'Please enter a valid status. The status can only be Canceled or Declined or Accepted or Completed';
}
} else {
errors.emptyStatus = 'Sorry the status is required';
}
if (Object.keys(errors).length > 0) {
return response.status(406).json({
statusCode: 406,
success: false,
error: errors,
});
}
return next();
}
static validateOrderId(request, response, next) {
const { orderId } = request.params;
const validId = /^[1-9]{1,}/;
if (!validId.test(orderId)) {
return response.status(406).json({
statusCode: 406,
success: false,
error: 'Please the orderId must be a number greater than zero',
});
}
return next();
}
}
export default ValidateOrder;
@davidshare

This comment has been minimized.

Copy link

commented Sep 14, 2018

That is how I had already refactored it
just that now my eslint is giving me errors of 100 max line limit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.