Finished up with improvements to error handling.
This commit is contained in:
parent
a6228a9fd9
commit
23ceb74883
|
|
@ -49,7 +49,8 @@ const channelSchema = new mongoose.Schema({
|
||||||
description: {
|
description: {
|
||||||
type: mongoose.SchemaTypes.String,
|
type: mongoose.SchemaTypes.String,
|
||||||
required: true,
|
required: true,
|
||||||
maxLength: 1000,
|
//Calculate max length by the validator max length and the size of an escaped character
|
||||||
|
maxLength: 1000 * 6,
|
||||||
default: 0
|
default: 0
|
||||||
},
|
},
|
||||||
thumbnail: {
|
thumbnail: {
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,8 @@ const typeEnum = ["image", "video"];
|
||||||
const emoteSchema = new mongoose.Schema({
|
const emoteSchema = new mongoose.Schema({
|
||||||
name:{
|
name:{
|
||||||
type: mongoose.SchemaTypes.String,
|
type: mongoose.SchemaTypes.String,
|
||||||
required: true
|
required: true,
|
||||||
|
maxLength: 14,
|
||||||
},
|
},
|
||||||
link:{
|
link:{
|
||||||
type: mongoose.SchemaTypes.String,
|
type: mongoose.SchemaTypes.String,
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||||
|
|
||||||
//NPM Imports
|
//NPM Imports
|
||||||
const {checkSchema} = require('express-validator');
|
const { checkSchema } = require('express-validator');
|
||||||
|
|
||||||
//local imports
|
//local imports
|
||||||
const {isRank} = require('./permissionsValidator');
|
const {isRank} = require('./permissionsValidator');
|
||||||
|
|
|
||||||
|
|
@ -15,30 +15,24 @@ You should have received a copy of the GNU Affero General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||||
|
|
||||||
//NPM Imports
|
//NPM Imports
|
||||||
const { check, body, checkSchema, checkExact} = require('express-validator');
|
const { checkSchema, checkExact } = require('express-validator');
|
||||||
|
|
||||||
//local imports
|
//local imports
|
||||||
const accountValidator = require('./accountValidator');
|
const accountValidator = require('./accountValidator');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
description: (field = 'description') => body(field).escape().trim().isLength({min: 1, max: 1000}),
|
|
||||||
|
|
||||||
thumbnail: (field = 'thumbnail') => accountValidator.img(field),
|
|
||||||
|
|
||||||
rank: (field = 'rank') => accountValidator.rank(field),
|
|
||||||
|
|
||||||
settingsMap: () => checkExact(checkSchema({
|
settingsMap: () => checkExact(checkSchema({
|
||||||
'settingsMap.hidden': {
|
'settingsMap.hidden': {
|
||||||
optional: true,
|
optional: true,
|
||||||
isBoolean: true,
|
isBoolean: true,
|
||||||
}
|
} }))
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.name = function(field = 'name'){
|
module.exports.name = function(field = 'name'){
|
||||||
return checkSchema({
|
return checkSchema({
|
||||||
[field]: {
|
[field]: {
|
||||||
esacpe: true,
|
escape: true,
|
||||||
isAlphanumeric: {
|
isAlphanumeric: {
|
||||||
errorMessage: "Channel names must be alphanumeric."
|
errorMessage: "Channel names must be alphanumeric."
|
||||||
},
|
},
|
||||||
|
|
@ -47,9 +41,46 @@ module.exports.name = function(field = 'name'){
|
||||||
min: 1,
|
min: 1,
|
||||||
max: 50
|
max: 50
|
||||||
},
|
},
|
||||||
errorMessage: "Channel numes must be between 1 and 50 characters."
|
errorMessage: "Channel names must be between 1 and 50 characters."
|
||||||
},
|
},
|
||||||
trim: true
|
trim: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.description = function(field = 'description'){
|
||||||
|
return checkSchema({
|
||||||
|
[field]:{
|
||||||
|
escape: true,
|
||||||
|
trim: true,
|
||||||
|
isLength: {
|
||||||
|
options: {
|
||||||
|
min: 1,
|
||||||
|
max: 1000
|
||||||
|
},
|
||||||
|
errorMessage: "Description must be between 1 and 1000 characters."
|
||||||
|
},
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.thumbnail = function(field = 'thumbnail'){
|
||||||
|
return accountValidator.img(field);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports.rank = function(field = 'rank'){
|
||||||
|
return accountValidator.rank(field);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.settingsMap = function(){
|
||||||
|
return checkExact(
|
||||||
|
checkSchema({
|
||||||
|
'settingsMap.hidden': {
|
||||||
|
optional: true,
|
||||||
|
isBoolean: true,
|
||||||
|
errorMessage: "Bad channel settings map."
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -15,35 +15,71 @@ You should have received a copy of the GNU Affero General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||||
|
|
||||||
//NPM Imports
|
//NPM Imports
|
||||||
const { check } = require('express-validator');
|
const { checkSchema } = require('express-validator');
|
||||||
const validator = require('validator');//We need validators for express-less code too!
|
const validator = require('validator');//We need validators for express-less code too!
|
||||||
|
const { errorMiddleware } = require('../utils/loggerUtils');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: (field = 'name') => check(field).escape().trim().isAlphanumeric().isLength({min: 1, max: 14}),
|
|
||||||
link: (field = 'link') => check(field).trim().isURL(),
|
|
||||||
manualName: (input) => {
|
|
||||||
//Trim and sanatize input
|
|
||||||
const clean = validator.trim(validator.escape(input));
|
|
||||||
|
|
||||||
//if cleaned input is a proper emote name
|
}
|
||||||
if(validator.isLength(clean, {min: 1, max: 14}) && validator.isAlphanumeric(clean)){
|
|
||||||
//return cleaned input
|
module.exports.name = function(field = 'name'){
|
||||||
return clean;
|
return checkSchema({
|
||||||
|
[field]: {
|
||||||
|
escape: true,
|
||||||
|
trim: true,
|
||||||
|
isAlphanumeric: {
|
||||||
|
errorMessage: "Emote names must be alphanumeric."
|
||||||
|
},
|
||||||
|
isLength: {
|
||||||
|
options: {
|
||||||
|
min: 1,
|
||||||
|
max: 14
|
||||||
|
},
|
||||||
|
errorMessage: "Emote name must be between 1 and 14 characters."
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
//otherwise return false
|
module.exports.link = function(field = 'link'){
|
||||||
return false;
|
return checkSchema({
|
||||||
},
|
[field]: {
|
||||||
manualLink: (input) => {
|
isURL: {
|
||||||
//Trim the input
|
options: {
|
||||||
const clean = validator.trim(input)
|
require_tld: false,
|
||||||
|
require_host: false
|
||||||
//If we have a URL return the trimmed input
|
},
|
||||||
if(validator.isURL(clean)){
|
errorMessage: "Invalid URL."
|
||||||
return clean;
|
},
|
||||||
|
trim: true
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
//otherwise return false
|
module.exports.manualName = function(input){
|
||||||
return false;
|
//Trim and sanatize input
|
||||||
|
const clean = validator.trim(validator.escape(input));
|
||||||
|
|
||||||
|
//if cleaned input is a proper emote name
|
||||||
|
if(validator.isLength(clean, {min: 1, max: 14}) && validator.isAlphanumeric(clean)){
|
||||||
|
//return cleaned input
|
||||||
|
return clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//otherwise return false
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.manualLink = function(input){
|
||||||
|
//Trim the input
|
||||||
|
const clean = validator.trim(input)
|
||||||
|
|
||||||
|
//If we have a URL return the trimmed input
|
||||||
|
if(validator.isURL(clean)){
|
||||||
|
return clean;
|
||||||
|
}
|
||||||
|
|
||||||
|
//otherwise return false
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -15,7 +15,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||||
|
|
||||||
//NPM Imports
|
//NPM Imports
|
||||||
const { check, body, checkSchema, checkExact} = require('express-validator');
|
const { checkSchema } = require('express-validator');
|
||||||
|
|
||||||
//local imports
|
//local imports
|
||||||
const permissionModel = require("../schemas/permissionSchema");
|
const permissionModel = require("../schemas/permissionSchema");
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,23 @@ You should have received a copy of the GNU Affero General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||||
|
|
||||||
//NPM Imports
|
//NPM Imports
|
||||||
const { check } = require('express-validator');
|
const { check, checkSchema } = require('express-validator');
|
||||||
|
|
||||||
module.exports = {
|
module.exports.command = function(field = 'command'){
|
||||||
command: (field = 'command') => check(field).escape().trim().isAlphanumeric().isLength({min: 1, max: 30}),
|
return checkSchema({
|
||||||
|
[field]: {
|
||||||
|
escape: true,
|
||||||
|
trim: true,
|
||||||
|
isAlphanumeric: {
|
||||||
|
errorMessage: "Toke commands must be alphanumeric."
|
||||||
|
},
|
||||||
|
isLength: {
|
||||||
|
options: {
|
||||||
|
min: 1,
|
||||||
|
max: 30
|
||||||
|
},
|
||||||
|
errorMessage: "Toke commands must be between 1 and 30 characters."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -105,7 +105,11 @@ class canopyUXUtils{
|
||||||
try{
|
try{
|
||||||
const errors = body.errors;
|
const errors = body.errors;
|
||||||
errors.forEach((err)=>{
|
errors.forEach((err)=>{
|
||||||
new canopyUXUtils.popup(`<h3>${err.type} Error:</h3><p><br>${err.msg}</p>`);
|
//Capitalize the first letter of the type
|
||||||
|
const type = `${err.type[0].toUpperCase()}${err.type.slice(1)}`
|
||||||
|
|
||||||
|
//Display our error
|
||||||
|
new canopyUXUtils.popup(`<h3>${type} Error:</h3><p><br>${err.msg}</p>`);
|
||||||
});
|
});
|
||||||
}catch(err){
|
}catch(err){
|
||||||
console.error("Display Error Body:");
|
console.error("Display Error Body:");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue