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: {
type: mongoose.SchemaTypes.String,
required: true,
maxLength: 1000,
//Calculate max length by the validator max length and the size of an escaped character
maxLength: 1000 * 6,
default: 0
},
thumbnail: {

View file

@ -26,7 +26,8 @@ const typeEnum = ["image", "video"];
const emoteSchema = new mongoose.Schema({
name:{
type: mongoose.SchemaTypes.String,
required: true
required: true,
maxLength: 14,
},
link:{
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/>.*/
//NPM Imports
const {checkSchema} = require('express-validator');
const { checkSchema } = require('express-validator');
//local imports
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/>.*/
//NPM Imports
const { check, body, checkSchema, checkExact} = require('express-validator');
const { checkSchema, checkExact } = require('express-validator');
//local imports
const accountValidator = require('./accountValidator');
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.hidden': {
optional: true,
isBoolean: true,
}
}))
} }))
}
module.exports.name = function(field = 'name'){
return checkSchema({
[field]: {
esacpe: true,
escape: true,
isAlphanumeric: {
errorMessage: "Channel names must be alphanumeric."
},
@ -47,9 +41,46 @@ module.exports.name = function(field = 'name'){
min: 1,
max: 50
},
errorMessage: "Channel numes must be between 1 and 50 characters."
errorMessage: "Channel names must be between 1 and 50 characters."
},
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,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/>.*/
//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 { errorMiddleware } = require('../utils/loggerUtils');
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
return clean;
}
//otherwise return false
return false;
},
manualLink: (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;
}
}
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
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;
}

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/>.*/
//NPM Imports
const { check, body, checkSchema, checkExact} = require('express-validator');
const { checkSchema } = require('express-validator');
//local imports
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/>.*/
//NPM Imports
const { check } = require('express-validator');
const { check, checkSchema } = require('express-validator');
module.exports = {
command: (field = 'command') => check(field).escape().trim().isAlphanumeric().isLength({min: 1, max: 30}),
module.exports.command = function(field = 'command'){
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{
const errors = body.errors;
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){
console.error("Display Error Body:");