Skip to content

Commit 343ea32

Browse files
committed
create the validation package
1 parent 155cded commit 343ea32

File tree

8 files changed

+2193
-0
lines changed

8 files changed

+2193
-0
lines changed

src/validation/errors.go

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
package validation
2+
3+
import (
4+
"errors"
5+
)
6+
7+
// Error represents a validation error
8+
type Error struct {
9+
error
10+
}
11+
12+
var (
13+
////////////////////
14+
// Server Errors //
15+
//////////////////
16+
17+
// ErrorAlreadyRunning will be sent when InitiateValidator() is called but the validator is already running
18+
// Code: 10
19+
ErrorAlreadyRunning = Error{errors.New("validator has already been initiated on this session")}
20+
21+
// ErrorValidatorNotInitiated will be sent when a validation func is called but the validator has not been initiated
22+
// Code: 11
23+
ErrorValidatorNotInitiated = Error{errors.New("validator func called but the validator has not been initiated")}
24+
25+
////////////////////
26+
/// User Errors ///
27+
//////////////////
28+
29+
// ErrorStringCanNotBeConvertedToInt will be sent if the request needs to be converted to an int but can't
30+
// Code: 9001
31+
ErrorStringCanNotBeConvertedToInt = Error{errors.New("the provided string can not be converted to an integer")}
32+
33+
// ErrorCharacterNameEmpty will be sent if the request contains an empty character name
34+
// Code: 10001
35+
ErrorCharacterNameEmpty = Error{errors.New("the provided character name is an empty string")}
36+
37+
// ErrorCharacterNameTooSmall will be sent if the request contains a character name of length < MinRunesAllowedInACharacterName
38+
// Code: 10002
39+
ErrorCharacterNameTooSmall = Error{errors.New("the provided character name is too small")}
40+
41+
// ErrorCharacterNameInvalid will be sent if the request contains an invalid character name
42+
// Code: 10003
43+
ErrorCharacterNameInvalid = Error{errors.New("the provided character name is invalid")}
44+
45+
// ErrorCharacterNameIsOnlyWhiteSpace will be sent if the request contains a name that consists of only whitespaces
46+
// Code: 10004
47+
ErrorCharacterNameIsOnlyWhiteSpace = Error{errors.New("the provided character name consists only of whitespaces")}
48+
49+
// ErrorCharacterNameTooBig will be sent if the request contains a character name of length > MaxRunesAllowedInACharacterName
50+
// Code: 10005
51+
ErrorCharacterNameTooBig = Error{errors.New("the provided character name is too big")}
52+
53+
// ErrorCharacterWordTooBig will be sent if the request contains a word with length > MaxRunesAllowedInACharacterNameWord in the character name
54+
// Code: 10006
55+
ErrorCharacterWordTooBig = Error{errors.New("the provided character name has a word too big")}
56+
57+
// ErrorCharacterWordTooSmall will be sent if the request contains a word with length < MinRunesAllowedInACharacterNameWord in the character name
58+
// Code: 10007
59+
ErrorCharacterWordTooSmall = Error{errors.New("the provided character name has a word too small")}
60+
61+
// ErrorInvalidNewsID will be sent if the request contains an invalid news ID
62+
// Code: 11001
63+
ErrorInvalidNewsID = Error{errors.New("the provided news id is invalid")}
64+
65+
// ErrorWorldDoesNotExist will be sent if the request contains a world that does not exist
66+
// Code: 11002
67+
ErrorWorldDoesNotExist = Error{errors.New("the provided world does not exist")}
68+
69+
// ErrorVocationDoesNotExist will be sent if the request contains a vocation that does not exist
70+
// Code: 11003
71+
ErrorVocationDoesNotExist = Error{errors.New("the provided vocation does not exist")}
72+
73+
// ErrorHighscoreCategoryDoesNotExist will be sent if the request contains a highscore catregory that does not exist
74+
// Code: 11004
75+
ErrorHighscoreCategoryDoesNotExist = Error{errors.New("the provided highscore category does not exist")}
76+
77+
// ErrorHouseDoesNotExist will be sent if the request contains a house that does not exist
78+
// Code: 11005
79+
ErrorHouseDoesNotExist = Error{errors.New("the provided house does not exist")}
80+
81+
// ErrorTownDoesNotExist will be sent if the request contains a town that does not exist
82+
// Code: 11006
83+
ErrorTownDoesNotExist = Error{errors.New("the provided town does not exist")}
84+
85+
// ErrorCreatureNameEmpty will be sent if the request contains an empty creature name
86+
// Code: 12001
87+
ErrorCreatureNameEmpty = Error{errors.New("the provided creature name is an empty string")}
88+
89+
// ErrorCreatureNameTooSmall will be sent if the request contains a creature name of length < smallestCreatureName
90+
// Code: 12002
91+
ErrorCreatureNameTooSmall = Error{errors.New("the provided creature name is too smal")}
92+
93+
// ErrorCreatureNameInvalid will be sent if the request contains an invalid creature name
94+
// Code: 12003
95+
ErrorCreatureNameInvalid = Error{errors.New("the provided creature name is invalid")}
96+
97+
// ErrorCreatureNameIsOnlyWhiteSpace will be sent if the request contains a name that consists of only whitespaces
98+
// Code: 12004
99+
ErrorCreatureNameIsOnlyWhiteSpace = Error{errors.New("the provided creature name consists only of whitespaces")}
100+
101+
// ErrorCreatureNameTooBig will be sent if the request contains a creature name of length > biggestCreatureNameRuneCount
102+
// Code: 12005
103+
ErrorCreatureNameTooBig = Error{errors.New("the provided creature name is too big")}
104+
105+
// ErrorCreatureWordTooBig will be sent if the request contains a word with length > biggestCreatureWordRuneCount in the creature name
106+
// Code: 12006
107+
ErrorCreatureWordTooBig = Error{errors.New("the provided creature name has a word too big")}
108+
109+
// ErrorCreatureWordTooSmall will be sent if the request contains a word with length < smallestCreatureWordRuneCount in the creature name
110+
// Code: 12007
111+
ErrorCreatureWordTooSmall = Error{errors.New("the provided creature name has a word too small")}
112+
113+
// ErrorSpellNameEmpty will be sent if the request contains an empty spell name
114+
// Code: 13001
115+
ErrorSpellNameEmpty = Error{errors.New("the provided spell name is an empty string")}
116+
117+
// ErrorSpellNameTooSmall will be sent if the request contains a spell name of length < smallestSpellNameOrFormulaRuneCount
118+
// Code: 13002
119+
ErrorSpellNameTooSmall = Error{errors.New("the provided spell name is too smal")}
120+
121+
// ErrorSpellNameInvalid will be sent if the request contains an invalid spell name
122+
// Code: 13003
123+
ErrorSpellNameInvalid = Error{errors.New("the provided spell name is invalid")}
124+
125+
// ErrorSpellNameIsOnlyWhiteSpace will be sent if the request contains a name that consists of only whitespaces
126+
// Code: 13004
127+
ErrorSpellNameIsOnlyWhiteSpace = Error{errors.New("the provided spell name consists only of whitespaces")}
128+
129+
// ErrorSpellNameTooBig will be sent if the request contains a spell name of length > biggestSpellNameOrFormulaRuneCount
130+
// Code: 13005
131+
ErrorSpellNameTooBig = Error{errors.New("the provided spell name is too big")}
132+
133+
// ErrorSpellWordTooBig will be sent if the request contains a word with length > biggestSpellWordRuneCount in the spell name
134+
// Code: 13006
135+
ErrorSpellWordTooBig = Error{errors.New("the provided spell name has a word too big")}
136+
137+
// ErrorSpellWordTooSmall will be sent if the request contains a word with length < smallestSpellWordRuneCount in the creature name
138+
// Code: 13007
139+
ErrorSpellWordTooSmall = Error{errors.New("the provided spell name has a word too small")}
140+
141+
// ErrorGuildNameEmpty will be sent if the request contains an empty guild name
142+
// Code: 14001
143+
ErrorGuildNameEmpty = Error{errors.New("the provided guild name is an empty string")}
144+
145+
// ErrorGuildNameTooSmall will be sent if the request contains a Guild name of length < MinRunesAllowedInAGuildName
146+
// Code: 14002
147+
ErrorGuildNameTooSmall = Error{errors.New("the provided guild name is too small")}
148+
149+
// ErrorGuildNameInvalid will be sent if the request contains an invalid guild name
150+
// Code: 14003
151+
ErrorGuildNameInvalid = Error{errors.New("the provided guild name is invalid")}
152+
153+
// ErrorGuildNameIsOnlyWhiteSpace will be sent if the request contains a name that consists of only whitespaces
154+
// Code: 14004
155+
ErrorGuildNameIsOnlyWhiteSpace = Error{errors.New("the provided guild name consists only of whitespaces")}
156+
157+
// ErrorGuildNameTooBig will be sent if the request contains a guild name of length > MaxRunesAllowedInAGuildName
158+
// Code: 14005
159+
ErrorGuildNameTooBig = Error{errors.New("the provided guild name is too big")}
160+
161+
// ErrorGuildWordTooBig will be sent if the request contains a word with length > MaxRunesAllowedInAGuildNameWord in the guild name
162+
// Code: 14006
163+
ErrorGuildWordTooBig = Error{errors.New("the provided guild name has a word too big")}
164+
165+
// ErrorGuildWordTooSmall will be sent if the request contains a word with length < MinRunesAllowedInAGuildNameWord in the guild name
166+
// Code: 14007
167+
ErrorGuildWordTooSmall = Error{errors.New("the provided guild name has a word too smal")}
168+
169+
///////////////////
170+
// Tibia Errors //
171+
/////////////////
172+
173+
// ErrorCharacterNotFound will be sent if the requested character does not exist
174+
// Code: 20001
175+
ErrorCharacterNotFound = Error{errors.New("could not find character")}
176+
177+
// ErrorCreatureNotFound will be sent if the requested creature does not exist
178+
// Code: 20002
179+
ErrorCreatureNotFound = Error{errors.New("could not find creature")}
180+
181+
// ErrorSpellNotFound will be sent if the requested spell does not exist
182+
// Code: 20003
183+
ErrorSpellNotFound = Error{errors.New("could not find spell")}
184+
185+
// ErrorGuildNotFound will be sent if the requested guild does not exist
186+
// Code: 20004
187+
ErrorGuildNotFound = Error{errors.New("could not find guild")}
188+
)
189+
190+
// Code will return the code of the error
191+
func (e Error) Code() int {
192+
switch e {
193+
case ErrorAlreadyRunning:
194+
return 10
195+
case ErrorValidatorNotInitiated:
196+
return 11
197+
case ErrorStringCanNotBeConvertedToInt:
198+
return 9001
199+
case ErrorCharacterNameEmpty:
200+
return 10001
201+
case ErrorCharacterNameTooSmall:
202+
return 10002
203+
case ErrorCharacterNameInvalid:
204+
return 10003
205+
case ErrorCharacterNameIsOnlyWhiteSpace:
206+
return 10004
207+
case ErrorCharacterNameTooBig:
208+
return 10005
209+
case ErrorCharacterWordTooBig:
210+
return 10006
211+
case ErrorCharacterWordTooSmall:
212+
return 10007
213+
case ErrorInvalidNewsID:
214+
return 11001
215+
case ErrorWorldDoesNotExist:
216+
return 11002
217+
case ErrorVocationDoesNotExist:
218+
return 11003
219+
case ErrorHighscoreCategoryDoesNotExist:
220+
return 11004
221+
case ErrorHouseDoesNotExist:
222+
return 11005
223+
case ErrorTownDoesNotExist:
224+
return 11006
225+
case ErrorCreatureNameEmpty:
226+
return 12001
227+
case ErrorCreatureNameTooSmall:
228+
return 12002
229+
case ErrorCreatureNameInvalid:
230+
return 12003
231+
case ErrorCreatureNameIsOnlyWhiteSpace:
232+
return 12004
233+
case ErrorCreatureNameTooBig:
234+
return 12005
235+
case ErrorCreatureWordTooBig:
236+
return 12006
237+
case ErrorCreatureWordTooSmall:
238+
return 12007
239+
case ErrorSpellNameEmpty:
240+
return 13001
241+
case ErrorSpellNameTooSmall:
242+
return 13002
243+
case ErrorSpellNameInvalid:
244+
return 13003
245+
case ErrorSpellNameIsOnlyWhiteSpace:
246+
return 13004
247+
case ErrorSpellNameTooBig:
248+
return 13005
249+
case ErrorSpellWordTooBig:
250+
return 13006
251+
case ErrorSpellWordTooSmall:
252+
return 13007
253+
case ErrorGuildNameEmpty:
254+
return 14001
255+
case ErrorGuildNameTooSmall:
256+
return 14002
257+
case ErrorGuildNameInvalid:
258+
return 14003
259+
case ErrorGuildNameIsOnlyWhiteSpace:
260+
return 14004
261+
case ErrorGuildNameTooBig:
262+
return 14005
263+
case ErrorGuildWordTooBig:
264+
return 14006
265+
case ErrorGuildWordTooSmall:
266+
return 14007
267+
case ErrorCharacterNotFound:
268+
return 20001
269+
case ErrorCreatureNotFound:
270+
return 20002
271+
case ErrorSpellNotFound:
272+
return 20003
273+
case ErrorGuildNotFound:
274+
return 20004
275+
default:
276+
return 0
277+
}
278+
}

src/validation/highscore.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package validation
2+
3+
import (
4+
"errors"
5+
"strings"
6+
)
7+
8+
var (
9+
// validHighscoreCatregories stores all valid highscore categories
10+
validHighscoreCategories = []string{"achievements", "achievement", "axe", "axefighting", "charm", "charms", "charmpoints", "charmspoints", "club", "clubfighting", "distance", "distancefighting", "fishing", "fist", "fistfighting", "goshnar", "goshnars", "goshnarstaint", "loyalty", "loyaltypoints", "magic", "mlvl", "magiclevel", "shielding", "shield", "sword", "swordfighting", "drome", "dromescore", "experience"}
11+
)
12+
13+
// IsHighscoreCategoryValid reports wheter the provided string represents a valid highscore category
14+
// Check if error == nil to see whether the highscore category is valid or not
15+
func IsHighscoreCategoryValid(hs string) error {
16+
for _, highscore := range validHighscoreCategories {
17+
if strings.EqualFold(hs, highscore) {
18+
return nil
19+
}
20+
}
21+
22+
return ErrorHighscoreCategoryDoesNotExist
23+
}
24+
25+
type HighscoreCategory int
26+
27+
const (
28+
HighScoreAchievements HighscoreCategory = iota + 1
29+
HighScoreAxefighting
30+
HighScoreCharmpoints
31+
HighScoreClubfighting
32+
HighScoreDistancefighting
33+
HighScoreExperience
34+
HighScoreFishing
35+
HighScoreFistfighting
36+
HighScoreGoshnarstaint
37+
HighScoreLoyaltypoints
38+
HighScoreMagiclevel
39+
HighScoreShielding
40+
HighScoreSwordfighting
41+
HighScoreDromescore
42+
)
43+
44+
func (hc HighscoreCategory) String() (string, error) {
45+
seasons := [...]string{"achievements", "axefighting", "charmpoints", "clubfighting", "distancefighting", "experience", "fishing", "fistfighting", "goshnarstaint", "loyaltypoints", "magiclevel", "shielding", "swordfighting", "dromescore"}
46+
if hc < HighScoreAchievements || hc > HighScoreDromescore {
47+
return "", errors.New("invalid HighscoreCategory value")
48+
}
49+
return seasons[hc-1], nil
50+
}
51+
52+
func HighscoreCategoryFromString(input string) HighscoreCategory {
53+
// Sanatize of category value
54+
input = strings.ToLower(input)
55+
switch input {
56+
case "achievements", "achievement":
57+
return HighScoreAchievements
58+
case "axe", "axefighting":
59+
return HighScoreAxefighting
60+
case "charm", "charms", "charmpoints", "charmspoints":
61+
return HighScoreCharmpoints
62+
case "club", "clubfighting":
63+
return HighScoreClubfighting
64+
case "distance", "distancefighting":
65+
return HighScoreDistancefighting
66+
case "fishing":
67+
return HighScoreFishing
68+
case "fist", "fistfighting":
69+
return HighScoreFistfighting
70+
case "goshnar", "goshnars", "goshnarstaint":
71+
return HighScoreGoshnarstaint
72+
case "loyalty", "loyaltypoints":
73+
return HighScoreLoyaltypoints
74+
case "magic", "mlvl", "magiclevel":
75+
return HighScoreMagiclevel
76+
case "shielding", "shield":
77+
return HighScoreShielding
78+
case "sword", "swordfighting":
79+
return HighScoreSwordfighting
80+
case "drome", "dromescore":
81+
return HighScoreDromescore
82+
default:
83+
return HighScoreExperience
84+
}
85+
}

0 commit comments

Comments
 (0)