Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Validate required fields.
- *
- * @param {Object} requiredFields
- * @param {Object} data
- *
- * @throws {MissingFieldError}
- * @throws {InvalidFieldTypeError}
- *
- * @returns {Boolean}
- */
- exports.validateRequiredFields = function( requiredFields, data ) {
- for ( var field in requiredFields ) {
- if ( !data.hasOwnProperty(field) ) {
- throw new MissingFieldError('No ' + field + ' provided');
- return false;
- }
- if ( typeof data[field] !== requiredFields[field] ) {
- throw new InvalidFieldTypeError(field + ' provided is not correct type [' + requiredFields[field] + ']');
- return false;
- }
- }
- return true;
- }
- function MissingFieldError(message) {
- this.name = "MissingFieldError";
- this.message = (message || "");
- }
- MissingFieldError.prototype = Error.prototype;
- function InvalidFieldTypeError(message) {
- this.name = "InvalidFieldTypeError";
- this.message = (message || "");
- }
- InvalidFieldTypeError.prototype = Error.prototype;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement