mirror of
https://github.com/status-im/status-go.git
synced 2025-01-18 02:31:47 +00:00
e9abf1662d
* chore(config)_: extract rpc_provider_persistence + tests * Add rpc_providers table, migration * add RpcProvider type * deprecate old rpc fields in networks, add RpcProviders list * add persistence packages for rpc_providers, networks * Tests
50 lines
1.8 KiB
Go
50 lines
1.8 KiB
Go
package networkhelper
|
|
|
|
import (
|
|
"gopkg.in/go-playground/validator.v9"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
)
|
|
|
|
func GetValidator() *validator.Validate {
|
|
validate := validator.New()
|
|
|
|
// Register struct-level validation for RpcProvider
|
|
validate.RegisterStructValidation(rpcProviderStructLevelValidation, params.RpcProvider{})
|
|
|
|
return validate
|
|
}
|
|
|
|
func rpcProviderStructLevelValidation(sl validator.StructLevel) {
|
|
provider := sl.Current().Interface().(params.RpcProvider)
|
|
|
|
switch provider.AuthType {
|
|
case params.NoAuth:
|
|
if provider.AuthLogin != "" || provider.AuthPassword != "" || provider.AuthToken != "" {
|
|
sl.ReportError(provider.AuthLogin, "AuthLogin", "authLogin", "noauth_fields_empty", "")
|
|
sl.ReportError(provider.AuthPassword, "AuthPassword", "authPassword", "noauth_fields_empty", "")
|
|
sl.ReportError(provider.AuthToken, "AuthToken", "authToken", "noauth_fields_empty", "")
|
|
}
|
|
case params.BasicAuth:
|
|
if provider.AuthLogin == "" {
|
|
sl.ReportError(provider.AuthLogin, "AuthLogin", "authLogin", "required", "")
|
|
}
|
|
if provider.AuthPassword == "" {
|
|
sl.ReportError(provider.AuthPassword, "AuthPassword", "authPassword", "required", "")
|
|
}
|
|
if provider.AuthToken != "" {
|
|
sl.ReportError(provider.AuthToken, "AuthToken", "authToken", "basic_auth_token_empty", "")
|
|
}
|
|
case params.TokenAuth:
|
|
if provider.AuthToken == "" {
|
|
sl.ReportError(provider.AuthToken, "AuthToken", "authToken", "required", "")
|
|
}
|
|
if provider.AuthLogin != "" || provider.AuthPassword != "" {
|
|
sl.ReportError(provider.AuthLogin, "AuthLogin", "authLogin", "tokenauth_fields_empty", "")
|
|
sl.ReportError(provider.AuthPassword, "AuthPassword", "authPassword", "tokenauth_fields_empty", "")
|
|
}
|
|
default:
|
|
sl.ReportError(provider.AuthType, "AuthType", "authType", "invalid_auth_type", "")
|
|
}
|
|
}
|