This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
package validator
2017-05-06 17:55:02 +02:00
import (
2017-07-01 01:25:11 +02:00
"fmt"
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
"reflect"
2017-07-01 01:25:11 +02:00
"time"
"unicode"
2017-05-26 12:12:52 +02:00
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/log"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
2017-07-01 01:25:11 +02:00
"github.com/go-playground/validator"
2017-07-01 02:59:39 +02:00
"strconv"
2017-05-06 17:55:02 +02:00
)
2017-07-01 01:25:11 +02:00
var validate * validator . Validate
func init ( ) {
validate = validator . New ( )
validate . RegisterValidation ( "isutf" , IsUTFLetterNumericValidator )
2017-07-01 02:59:39 +02:00
validate . RegisterValidation ( "default" , DefaultValidator )
2017-07-01 01:25:11 +02:00
}
2017-05-07 19:59:38 +02:00
2017-05-26 12:12:52 +02:00
// ValidateForm : Check if a form is valid according to its tags
2017-05-21 18:13:28 +02:00
func ValidateForm ( form interface { } , mes * msg . Messages ) {
2017-07-01 01:25:11 +02:00
err := validate . Struct ( form )
2017-06-29 13:15:23 +02:00
if err != nil {
2017-07-01 01:25:11 +02:00
for _ , fieldError := range err . ( validator . ValidationErrors ) {
switch fieldError . Tag ( ) {
case "required" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_field_needed" , fieldError . Field ( ) , fieldError . Param ( ) )
case "eq" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_equal" , fieldError . Field ( ) , fieldError . Param ( ) )
case "ne" , "nefield" , "necsfield" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_not_equal" , fieldError . Field ( ) , fieldError . Param ( ) )
case "eqfield" , "eqcsfield" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_same_value" , fieldError . Field ( ) , fieldError . Param ( ) )
case "gtfield" , "gtcsfield" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_greater_number" , fieldError . Field ( ) , fieldError . Param ( ) )
case "ltfield" , "ltcsfield" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_less_number" , fieldError . Field ( ) , fieldError . Param ( ) )
case "gtefield" , "gtecsfield" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_min_field" , fieldError . Field ( ) , fieldError . Param ( ) )
case "ltefield" , "ltecsfield" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_max_field" , fieldError . Field ( ) , fieldError . Param ( ) )
case "alpha" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_alpha" , fieldError . Field ( ) )
case "alphanum" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_alphanum" , fieldError . Field ( ) )
case "numeric" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_numeric_valid" , fieldError . Field ( ) )
case "number" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_number_valid" , fieldError . Field ( ) )
case "hexadecimal" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_hexadecimal_valid" , fieldError . Field ( ) )
case "hexcolor" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_hex_valid" , fieldError . Field ( ) )
case "rgb" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_rgb_valid" , fieldError . Field ( ) )
case "rgba" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_rgba_valid" , fieldError . Field ( ) )
case "hsl" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_hsl_valid" , fieldError . Field ( ) )
case "hsla" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_hsla_valid" , fieldError . Field ( ) )
case "url" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_url_valid" , fieldError . Field ( ) )
case "uri" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_uri_valid" , fieldError . Field ( ) )
case "base64" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_base64_valid" , fieldError . Field ( ) )
case "contains" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_contains" , fieldError . Field ( ) , fieldError . Param ( ) )
case "containsany" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_contains_any" , fieldError . Field ( ) , fieldError . Param ( ) )
case "excludes" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_excludes" , fieldError . Field ( ) , fieldError . Param ( ) )
case "excludesall" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_excludes_all" , fieldError . Field ( ) , fieldError . Param ( ) )
case "excludesrune" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_excludes_rune" , fieldError . Field ( ) , fieldError . Param ( ) )
case "iscolor" :
mes . AddErrorTf ( fieldError . Field ( ) , "error_color_valid" , fieldError . Field ( ) )
default :
switch fieldError . Kind ( ) {
case reflect . String :
stringErrors ( fieldError , mes )
case reflect . Slice , reflect . Map , reflect . Array :
case reflect . Struct :
if fieldError . Type ( ) != reflect . TypeOf ( time . Time { } ) {
fmt . Printf ( "tag '%s' cannot be used on a struct type." , fieldError . Tag ( ) )
mes . AddErrorTf ( fieldError . Field ( ) , "error_field" , fieldError . Field ( ) )
}
dateErrors ( fieldError , mes )
default :
numberErrors ( fieldError , mes )
}
}
}
2017-05-07 19:59:38 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
}
2017-07-01 01:25:11 +02:00
// Bind a validated form to a model, tag omit to not bind a field
func Bind ( model interface { } , form interface { } ) {
modelIndirect := reflect . Indirect ( reflect . ValueOf ( model ) )
formElem := reflect . ValueOf ( form ) . Elem ( )
typeOfTForm := formElem . Type ( )
for i := 0 ; i < formElem . NumField ( ) ; i ++ {
tag := typeOfTForm . Field ( i ) . Tag
if tag . Get ( "omit" ) != "true" {
modelField := modelIndirect . FieldByName ( typeOfTForm . Field ( i ) . Name )
if modelField . IsValid ( ) {
formField := formElem . Field ( i )
modelField . Set ( formField )
} else {
log . Warnf ( "modelField : %s - %s" , typeOfTForm . Field ( i ) . Name , modelField )
}
}
}
}
// IsUTFLetterNumeric check if the string contains only unicode letters and numbers. Empty string is valid.
func IsUTFLetterNumeric ( str string ) bool {
if len ( str ) == 0 {
return true
}
for _ , c := range str {
if ! unicode . IsLetter ( c ) && ! unicode . IsNumber ( c ) { //letters && numbers are ok
return false
}
}
return true
}
// IsUTFLetterNumericValidator is an interface to IsUTFLetterNumeric from validator
func IsUTFLetterNumericValidator ( fl validator . FieldLevel ) bool {
value := fl . Field ( ) . String ( ) // value
return IsUTFLetterNumeric ( value )
}
2017-07-01 02:59:39 +02:00
func DefaultValidator ( fl validator . FieldLevel ) bool {
switch fl . Field ( ) . Kind ( ) {
case reflect . String :
if len ( fl . Field ( ) . String ( ) ) == 0 {
fl . Field ( ) . SetString ( fl . Param ( ) )
}
case reflect . Int , reflect . Int8 , reflect . Int16 , reflect . Int32 , reflect . Int64 :
if fl . Field ( ) . Int ( ) == 0 {
number , err := strconv . Atoi ( fl . Param ( ) )
if err != nil {
fmt . Printf ( "Couldn't convert default value for field %s" , fl . FieldName ( ) )
return false
}
fl . Field ( ) . SetInt ( int64 ( number ) )
}
case reflect . Float32 , reflect . Float64 :
if fl . Field ( ) . Float ( ) == 0 {
number , err := strconv . ParseFloat ( fl . Param ( ) , 64 )
if err != nil {
fmt . Printf ( "Couldn't convert default value for field %s" , fl . FieldName ( ) )
return false
}
fl . Field ( ) . SetFloat ( number )
}
}
return true
}