changed up validators

This commit is contained in:
Sendou
2020-03-25 16:59:30 +02:00
parent 9d302fd13e
commit e48d482282
3 changed files with 61 additions and 38 deletions

View File

@@ -86,7 +86,7 @@ const typeDef = gql`
discord_id: String!
weapon: String!
main_abilities: [Ability!]!
sub_abilities: [Ability]!
sub_abilities: [[Ability]!]!
kills: Int!
assists: Int!
deaths: Int!
@@ -99,7 +99,7 @@ const typeDef = gql`
discord_id: String!
weapon: String!
main_abilities: [Ability!]!
sub_abilities: [Ability]!
sub_abilities: [[Ability]!]!
kills: Int!
assists: Int!
deaths: Int!
@@ -132,12 +132,24 @@ const resolvers = {
throw new AuthenticationError("Invalid token provided")
}
validateDetailedTournamentInput(args.tournament)
args.matches.forEach(match =>
match.map_details.forEach(map => validateDetailedMapInput(map))
const tournamentInputProblems = await validateDetailedTournamentInput(
args.tournament
)
const maptInputProblems = args.matches.map(match =>
match.map_details.map(map => validateDetailedMapInput(map))
)
console.log("args", args)
console.log("tournamentInputProblems", tournamentInputProblems)
console.log("maptInputProblems", maptInputProblems.flat(3))
const problems = [
...tournamentInputProblems,
...maptInputProblems.flat(3),
]
if (problems.length > 0) {
throw new UserInputError(problems.join(","))
}
return true
//put tourney in db

View File

@@ -4,7 +4,7 @@ const Player = {
discord_id: String,
weapon: String,
main_abilities: [String],
sub_abilities: [String],
sub_abilities: [[String]],
kills: Number,
assists: Number,
deaths: Number,

View File

@@ -1,4 +1,3 @@
const { UserInputError } = require("apollo-server-express")
const DetailedTournament = require("../mongoose-models/detailedtournament")
const weapons = require("../utils/weapons")
const gear = require("../utils/gear")
@@ -9,109 +8,121 @@ function isNum(maybeNumber) {
}
async function validateDetailedTournamentInput(input) {
const existingTournament = await DetailedTournament.find({ name: input.name })
const problems = []
const existingTournament = await DetailedTournament.findOne({
name: input.name,
})
if (existingTournament) {
throw new UserInputError("Tournament with that name already exists")
problems.push("Tournament with that name already exists")
}
//date not validated
if (input.top_3_team_names.length !== 3) {
throw new UserInputError("Length of top_3_team_names was not 3")
problems.push("Length of top_3_team_names was not 3")
}
if (input.top_3_discord_ids.length !== 3) {
throw new UserInputError("Length of top_3_discord_ids was not 3")
problems.push("Length of top_3_discord_ids was not 3")
}
input.top_3_discord_ids.forEach(discord_id_arr => {
if (discord_id_arr.length !== 4) {
throw new UserInputError(
problems.push(
"Length of top_3_discord_ids contained an array that had invalid length"
)
}
discord_id_arr.forEach(discord_id => {
if (!isNum(discord_id))
throw new UserInputError(`Invalid Discord ID: ${discord_id}`)
if (!isNum(discord_id)) problems.push(`Invalid Discord ID: ${discord_id}`)
})
})
return problems
}
async function validateDetailedMapInput(input) {
function validateDetailedMapInput(input) {
const problems = []
if (!stages.includes(input.stage)) {
throw new UserInputError(`Invalid stage name: ${input.stage}`)
problems.push(`Invalid stage name: ${input.stage}`)
}
if (!["SZ", "TC", "RM", "CB", "TW"].includes(input.mode)) {
throw new UserInputError(`Invalid mode name: ${input.mode}`)
problems.push(`Invalid mode name: ${input.mode}`)
}
if (input.duration < 0 || input.duration > 500) {
throw new UserInputError(`Invalid duration: ${input.duration}`)
problems.push(`Invalid duration: ${input.duration}`)
}
if (input.winners.score <= input.losers.score) {
throw new UserInputError(
"Losing team has greater or equal score as the winner team"
)
problems.push("Losing team has greater or equal score as the winner team")
}
input.winners.players.forEach(player => validateDetailedPlayerInput(player))
input.losers.players.forEach(player => validateDetailedPlayerInput(player))
input.winners.players.forEach(player =>
validateDetailedPlayerInput(player, problems)
)
input.losers.players.forEach(player =>
validateDetailedPlayerInput(player, problems)
)
return problems
}
async function validateDetailedPlayerInput(input) {
function validateDetailedPlayerInput(input, problems) {
if (!isNum(input.discord_id)) {
throw new UserInputError(`Invalid Discord ID: ${input.discord_id}`)
problems.push(`Invalid Discord ID: ${input.discord_id}`)
}
if (!weapons.includes(input.weapon)) {
throw new UserInputError(`Invalid weapon: ${input.weapon}`)
problems.push(`Invalid weapon: ${input.weapon}`)
}
if (input.main_abilities.length !== 3) {
throw new UserInputError(
problems.push(
`Invalid main abilities length: ${input.main_abilities.length}`
)
}
if (input.sub_abilities.length !== 9) {
throw new UserInputError(
`Invalid main abilities length: ${input.sub_abilities.length}`
if (input.sub_abilities.flat().length !== 9) {
console.log("input.sub", input.sub_abilities)
problems.push(
`Invalid sub abilities length: ${input.sub_abilities.flat().length}`
)
}
if (input.kills < 0 || input.kills > 50) {
throw new UserInputError(`Invalid kill count: ${input.kills}`)
problems.push(`Invalid kill count: ${input.kills}`)
}
if (input.assists < 0 || input.assists > 50) {
throw new UserInputError(`Invalid assist count: ${input.assists}`)
problems.push(`Invalid assist count: ${input.assists}`)
}
if (input.deaths < 0 || input.deaths > 50) {
throw new UserInputError(`Invalid death count: ${input.deaths}`)
problems.push(`Invalid death count: ${input.deaths}`)
}
if (input.specials < 0 || input.specials > 50) {
throw new UserInputError(`Invalid special count: ${input.specials}`)
problems.push(`Invalid special count: ${input.specials}`)
}
if (input.paint < 0 || input.paint > 5000) {
throw new UserInputError(`Invalid paint count: ${input.paint}`)
problems.push(`Invalid paint count: ${input.paint}`)
}
if (input.gear.length !== 3) {
throw new UserInputError(`Invalid gear length: ${input.gear.length}`)
problems.push(`Invalid gear length: ${input.gear.length}`)
}
input.gear.forEach(gearPiece => {
if (!gear.includes(gearPiece)) {
throw new UserInputError(`Invalid gear: ${gearPiece}`)
problems.push(`Invalid gear: ${gearPiece}`)
}
})
return problems
}
module.exports = {