spee.ch/public/assets/js/validationFunctions.js

182 lines
5.9 KiB
JavaScript
Raw Normal View History

// validation function which checks the proposed file's type, size, and name
function validateFile(file) {
if (!file) {
throw new Error('no file provided');
}
if (/'/.test(file.name)) {
throw new Error('apostrophes are not allowed in the file name');
}
// validate size and type
switch (file.type) {
case 'image/jpeg':
case 'image/jpg':
case 'image/png':
case 'image/gif':
2017-07-31 19:24:17 +02:00
if (file.size > 50000000){
throw new Error('Sorry, images are limited to 50 megabytes.');
}
break;
case 'video/mp4':
if (file.size > 50000000){
throw new Error('Sorry, videos are limited to 50 megabytes.');
}
break;
default:
throw new Error(file.type + ' is not a supported file type. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.')
}
}
2017-09-19 21:54:23 +02:00
// validation function that checks to make sure the claim name is valid
function validateClaimName (name) {
// ensure a name was entered
if (name.length < 1) {
throw new NameError("You must enter a name for your claim");
}
// validate the characters in the 'name' field
const invalidCharacters = /[^A-Za-z0-9,-]/g.exec(name);
if (invalidCharacters) {
throw new NameError('"' + invalidCharacters + '" characters are not allowed in the title.');
}
2017-09-19 17:47:24 +02:00
}
function validateChannelName (name) {
name = name.substring(name.indexOf('@') + 1);
console.log(name);
// ensure a name was entered
if (name.length < 1) {
throw new ChannelError("You must enter a name for your channel");
}
// validate the characters in the 'name' field
const invalidCharacters = /[^A-Za-z0-9,-,@]/g.exec(name);
if (invalidCharacters) {
throw new ChannelError('"' + invalidCharacters + '" characters are not allowed in the channel name.');
}
}
function validatePassword (password) {
if (password.length < 1) {
throw new ChannelError("You must enter a password for you channel");
}
}
2017-09-19 21:54:23 +02:00
function cleanseClaimName(name) {
name = name.replace(/\s+/g, '-'); // replace spaces with dashes
name = name.replace(/[^A-Za-z0-9-]/g, ''); // remove all characters that are not A-Z, a-z, 0-9, or '-'
return name;
}
// validation functions to check claim & channel name eligibility as the inputs change
function isNameAvailable (name, apiUrl) {
2017-09-19 17:47:24 +02:00
return new Promise(function(resolve, reject) {
// make sure the claim name is still available
var xhttp;
xhttp = new XMLHttpRequest();
2017-09-19 21:54:23 +02:00
xhttp.open('GET', apiUrl + name, true);
2017-09-19 17:47:24 +02:00
xhttp.responseType = 'json';
xhttp.onreadystatechange = function() {
if (this.readyState == 4 ) {
if ( this.status == 200) {
if (this.response == true) {
resolve();
} else {
reject( new NameError("That name has already been claimed by another user. Please choose a different name."));
}
} else {
reject("request to check claim name failed with status:" + this.status);
};
}
};
xhttp.send();
});
}
2017-09-19 17:47:24 +02:00
2017-09-19 21:54:23 +02:00
function showError(errorDisplay, errorMsg) {
errorDisplay.hidden = false;
errorDisplay.innerText = errorMsg;
}
2017-09-19 21:54:23 +02:00
function hideError(errorDisplay) {
errorDisplay.hidden = true;
errorDisplay.innerText = '';
}
2017-09-19 21:54:23 +02:00
function showSuccess (successElement) {
successElement.hidden = false;
successElement.innerHTML = "&#x2714";
}
2017-09-19 17:47:24 +02:00
2017-09-19 21:54:23 +02:00
function hideSuccess (successElement) {
successElement.hidden = true;
successElement.innerHTML = "";
}
2017-09-19 17:47:24 +02:00
function checkAvailability(name, successDisplayElement, errorDisplayElement, validateName, isNameAvailable, apiUrl) {
2017-09-19 17:47:24 +02:00
try {
2017-09-19 21:54:23 +02:00
// check to make sure the characters are valid
validateName(name);
2017-09-19 17:47:24 +02:00
// check to make sure it is available
2017-09-19 21:54:23 +02:00
isNameAvailable(name, apiUrl)
2017-09-19 17:47:24 +02:00
.then(function() {
2017-09-19 21:54:23 +02:00
hideError(errorDisplayElement);
showSuccess(successDisplayElement)
2017-09-19 17:47:24 +02:00
})
.catch(function(error) {
2017-09-19 21:54:23 +02:00
hideSuccess(successDisplayElement);
showError(errorDisplayElement, error.message);
2017-09-19 17:47:24 +02:00
});
} catch (error) {
2017-09-19 21:54:23 +02:00
hideSuccess(successDisplayElement);
showError(errorDisplayElement, error.message);
2017-09-19 17:47:24 +02:00
}
}
2017-09-19 21:54:23 +02:00
function checkClaimName(name){
2017-09-20 00:39:54 +02:00
const successDisplayElement = document.getElementById('input-success-claim-name');
const errorDisplayElement = document.getElementById('input-error-claim-name');
checkAvailability(name, successDisplayElement, errorDisplayElement, validateClaimName, isNameAvailable, '/api/isClaimAvailable/');
2017-09-19 21:54:23 +02:00
}
function checkChannelName(name){
2017-09-20 00:39:54 +02:00
const successDisplayElement = document.getElementById('input-success-channel-name');
const errorDisplayElement = document.getElementById('input-error-channel-name');
name = `@${name}`;
checkAvailability(name, successDisplayElement, errorDisplayElement, validateChannelName, isNameAvailable, '/api/isChannelAvailable/');
2017-09-19 21:54:23 +02:00
}
// validation function which checks all aspects of the publish submission
2017-09-20 00:39:54 +02:00
function validateSubmission(stagedFiles, claimName, channelName){
return new Promise(function (resolve, reject) {
2017-09-20 00:39:54 +02:00
// 1. make sure only 1 file was selected
if (!stagedFiles) {
reject(new FileError("Please select a file"));
} else if (stagedFiles.length > 1) {
reject(new FileError("Only one file is allowed at a time"));
}
2017-09-20 00:39:54 +02:00
// 2. validate the file's name, type, and size
try {
validateFile(stagedFiles[0]);
} catch (error) {
reject(error);
}
2017-09-20 00:39:54 +02:00
// 3. validate that a channel was chosen
if (channelName === 'new') {
reject(new ChannelError("Please select a valid channel"));
};
// 4. validate the claim name
try {
2017-09-20 00:39:54 +02:00
validateClaimName(claimName); // validate the formatting
} catch (error) {
reject(error);
}
2017-09-20 00:39:54 +02:00
console.log(claimName);
isNameAvailable(claimName, '/api/isClaimAvailable/') // validate the availability
.then(function() {
resolve();
})
.catch(function(error) {
reject(error);
});
});
}