basic series validation

master
Kit Kasune 2 years ago
parent 685eec13b6
commit 43209070cd
  1. 64
      api/ani/v1/routes/series/add.js

@ -56,49 +56,35 @@ module.exports = (app, router) => {
//VALIDATION //VALIDATION
const badReq = msg => {res.status(400).send(msg); return null;}; const badReq = msg => {res.status(400).send(msg); return null;};
const validateStringList = (maxLength, list, listName, regex) => { const validateStringList = (maxLength, list, listName, maxListLength, regex) => {
let bad = false; let good = false;
list.forEach(item => {bad = !bad && typeof item === 'string' && item.length < maxLength && (!regex || item.match(regex))}); if (maxListLength !== -1 && list.length > maxListLength) {return badReq(`Your ${listName} had too many items.`);}
if (bad) {return badReq();} list.forEach(item => {good = !good && typeof item === 'string' && item.length < maxLength && (!regex || item.match(regex));});
if (!good) {return badReq(`Your ${listName} did not contain purely strings, or one of them was too long.`);}
return list; return list;
}; };
let options = req.body; let options = req.body;
if (options.altNames && Array.isArray(options.altNames) && options.altNames.length <= 10) { if (options.altNames) {series.altNames = validateStringList(150, options.altNames, 'altNames'); if (!series.altNames) {return;}}
let bad = false; if (options.tags) {series.tags = validateStringList(25, options.tags, 'tags', 25, /^[\w-]+$/gm);if (!series.tags) {return;}}
options.altNames.forEach(name => bad = !bad && typeof name === 'string' && name.length < 150);
if (bad) {return badReq("Your altNames did not contain purely strings, or one of them was too long.");}
series.altNames = options.altNames;
}
if (options.tags && Array.isArray(options.tags) && options.tags.length <= 10) {
let bad = false;
options.tags.forEach(tag => bad = !bad && typeof tag === 'string' && tag.length < 25 && tag.match(/^[\w-]+$/gm));
if (bad) {return badReq("Your tags did not contain purely strings, or one of them was too long, or contained invalud characters");}
series.tags = options.tags.map(tag => tag.toLowerCase());
}
if (options.nsfw === true && !(options.nsfwReason || ['gore', 'language', 'nudity', 'themes'].includes(options.nsfwReason))) {return badReq("You marked this series as nsfw, but did not provide a reason.");} if (options.nsfw === true && !(options.nsfwReason || ['gore', 'language', 'nudity', 'themes'].includes(options.nsfwReason))) {return badReq("You marked this series as nsfw, but did not provide a reason.");}
else if (options.nsfw) { else if (options.nsfw) {
series.nsfw = options.nsfw; series.nsfw = options.nsfw;
series.nsfwReason = options.nsfwReason; series.nsfwReason = options.nsfwReason;
} }
if (options.genres && Array.isArray(options.genres) && options.genres.length <= 10) { if (options.genres) {series.genres = validateStringList(25, options.genres, 'genres', 10, /^[\w- ]+$/gm); if (!series.genres) {return;}} //TODO genres as DB
let bad = false; if (options.streamAt) {series.streamAt = validateStringList(25, options.streamAt, 'streamAt locations', 10, /^[\w- ]+$/gm);if (!series.streamAt) {return;}}
options.genres.forEach(tag => bad = !bad && typeof tag === 'string' && tag.length < 25 && tag.match(/^[\w- ]+$/gm)); if (options.publishers) {series.publishers = validateStringList(25, options.publishers, 'publishers', 5, /^[\w- ]+$/gm);if (!series.publishers) {return;}}
if (bad) {return badReq("Your genres did not contain purely strings, or one of them was too long.");} //well this is hell. never again will i make a schema so fat // i have no idea what im doing here
series.genres = options.genres; //TODO validate pt. 2, the requiem
} //TODO genres as DB if (options.air) {
if (options.streamAt && Array.isArray(options.streamAt) && options.streamAt.length <= 10) { if (options.air.from) {series.air.from = options.air.from;}
let bad = false; if (options.air.to) {series.air.to = options.air.to;} //it's 9am send help
options.streamAt.forEach(tag => bad = !bad && typeof tag === 'string' && tag.length < 25 && tag.match(/^[\w- ]+$/gm));
if (bad) {return badReq("Your streamAt locations did not contain purely strings, or one of them was too long.");}
series.streamAt = options.streamAt;
}
if (options.publishers && Array.isArray(options.publishers) && options.publishers.length <= 10) {
let bad = false;
options.publishers.forEach(tag => bad = !bad && typeof tag === 'string' && tag.length < 25 && tag.match(/^[\w- ]+$/gm));
if (bad) {return badReq("Your publishers did not contain purely strings, or one of them was too long.");}
series.publishers = options.publishers;
} }
//unvalidated BS at this point fuck it
//get rotated idiot
if (options.art) {series.art = options.art;}
if (options.officialSite) {series.officialSite = options.officialSite;}
if (options.videos) {series.videos = options.videos;} //it probably works. good enough yeah?
return series.save().then(async () => { return series.save().then(async () => {
app.cache.series[series.id] = { app.cache.series[series.id] = {
@ -110,8 +96,18 @@ module.exports = (app, router) => {
genres: series.genres, genres: series.genres,
tags: series.tags tags: series.tags
}; };
app.cache.seriesCount++;
return res.send(`Your series was successfully ${series.meta.submitted ? 'submitted' : "added"}.`); return res.send(`Your series was successfully ${series.meta.submitted ? 'submitted' : "added"}.`);
}).catch((e) => {console.error(e); res.status(500).send("There was an error trying to process your request. It's likely that our database found something wrong with your body fields, and the server didn't realize. Check your request and try again.");}); }).catch((e) => {console.error(e); res.status(500).send("There was an error trying to process your request. It's likely that our database found something wrong with your body fields, and the server didn't realize. Check your request and try again.");});
//TODO remove console error //TODO remove console error
})
.get(app.auth.tokenPass, app.auth.permsPass('series-approve'), async (req, res) => {
const series = await Anime.findOne({id: req.params.id});
if (!series || (series && !series.meta.completed && req.unauthorized)) {return res.status(400).send("A series with that ID doesn't exist!");}
let {name, id, romaji, kanji, air, rating, likes, seasons, characters, altNames, genres, tags, nsfw, nsfwReason, synopsis, numericalId, completed, publishers, studios} = series;
return res.json({
name, id, romaji, kanji, air, rating, likes, seasons, characters, altNames, genres, tags, nsfw, nsfwReason, synopsis, numericalId, completed, publishers, studios,
meta: {completed: series.meta.completed, creator: series.meta.creator, locked: series.meta.locked}
});
}); });
}; };
Loading…
Cancel
Save