Finished up with improvements to error handling.

This commit is contained in:
rainbow napkin 2025-04-29 07:05:38 -04:00
parent a6228a9fd9
commit 23ceb74883
8 changed files with 128 additions and 40 deletions

View file

@ -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: {

View file

@ -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,

View file

@ -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');

View file

@ -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."
}
})
);
}

View file

@ -15,13 +15,49 @@ 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) => {
module.exports.name = function(field = 'name'){
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."
},
}
})
}
module.exports.link = function(field = 'link'){
return checkSchema({
[field]: {
isURL: {
options: {
require_tld: false,
require_host: false
},
errorMessage: "Invalid URL."
},
trim: true
}
})
}
module.exports.manualName = function(input){
//Trim and sanatize input //Trim and sanatize input
const clean = validator.trim(validator.escape(input)); const clean = validator.trim(validator.escape(input));
@ -33,8 +69,9 @@ module.exports = {
//otherwise return false //otherwise return false
return false; return false;
}, }
manualLink: (input) => {
module.exports.manualLink = function(input){
//Trim the input //Trim the input
const clean = validator.trim(input) const clean = validator.trim(input)
@ -45,5 +82,4 @@ module.exports = {
//otherwise return false //otherwise return false
return false; return false;
}
} }

View file

@ -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");

View file

@ -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."
}
}
});
} }

View file

@ -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:");