Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Remove request validation (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoksr authored Jul 19, 2023
1 parent 33f8e05 commit 2be8d91
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ run:
timeout: '5m'
skip-dirs:
- '_examples'
- '_playground'
allow-parallel-runners: true
modules-download-mode: 'readonly'

linters:
enable:
- 'asciicheck'
- 'bodyclose'
- 'depguard'
- 'dogsled'
- 'errcheck'
- 'errorlint'
Expand Down
2 changes: 1 addition & 1 deletion activity_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type (

// ActivityLogGetOptions represents the query parameters for an activity log get request.
ActivityLogGetOptions struct {
ID string `url:"log" json:"-" validate:"required"` // ID is the unique identifier for the log object.
ID string `url:"log" json:"-"` // ID is the unique identifier for the log object.
}

// ActivityLogListResponse represents a response from the activity log list endpoint.
Expand Down
2 changes: 1 addition & 1 deletion audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type (

// AuditWorkplaceUserGetOptions represents options for the audit workplace user get endpoint.
AuditWorkplaceUserGetOptions struct {
UserID string `url:"-" json:"-" validate:"required"`
UserID string `url:"-" json:"-"`
Settings *bool `url:"settings,omitempty" json:"-"` // If true, the api will return more information if the workplace has e.g. SAML enabled and SCIM enabled.
}

Expand Down
2 changes: 1 addition & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type (

// AuthRevokeOptions revokes an auth token.
AuthRevokeOptions struct {
Tokens []AuthToken `url:"-" json:"tokens" validate:"gt=0"` // A list of tokens to revoke.
Tokens []AuthToken `url:"-" json:"tokens"` // A list of tokens to revoke.
}
)

Expand Down
36 changes: 18 additions & 18 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type (

// ConfigGetOptions represents the options for the config get endpoint.
ConfigGetOptions struct {
Project string `url:"project" json:"-" validate:"required"` // Identifier of the project that the config belongs to.
Config string `url:"config" json:"-" validate:"required"` // Name of the config.
Project string `url:"project" json:"-"` // Identifier of the project that the config belongs to.
Config string `url:"config" json:"-"` // Name of the config.
}

// ConfigListResponse represents a response from the config list endpoint.
Expand All @@ -42,7 +42,7 @@ type (
// ConfigListOptions represents the query parameters for a config list request.
ConfigListOptions struct {
ListOptions `url:",inline" json:"-"`
Project string `url:"project" json:"-" validate:"required"` // Identifier of the project that the config belongs to.
Project string `url:"project" json:"-"` // Identifier of the project that the config belongs to.
}

// ConfigCreateResponse represents a response from the config create endpoint.
Expand All @@ -57,9 +57,9 @@ type (

// ConfigCreateOptions represents the body parameters for a config create request.
ConfigCreateOptions struct {
Project string `url:"-" json:"project" validate:"required"` // Identifier of the project that the config belongs to.
Environment string `url:"-" json:"environment" validate:"required"` // Identifier of the environment that the config belongs to.
Name string `url:"-" json:"name" validate:"required"` // Name of the new branch configuration.
Project string `url:"-" json:"project"` // Identifier of the project that the config belongs to.
Environment string `url:"-" json:"environment"` // Identifier of the environment that the config belongs to.
Name string `url:"-" json:"name"` // Name of the new branch configuration.
}

// ConfigUpdateResponse represents a doppler config update request.
Expand All @@ -74,9 +74,9 @@ type (

// ConfigUpdateOptions represents the body parameters for a config update request.
ConfigUpdateOptions struct {
Project string `url:"-" json:"project" validate:"required"` // Identifier of the project that the config belongs to.
Config string `url:"-" json:"config" validate:"required"` // Name of the config.
NewName string `url:"-" json:"name" validate:"required"` // New name of the config.
Project string `url:"-" json:"project"` // Identifier of the project that the config belongs to.
Config string `url:"-" json:"config"` // Name of the config.
NewName string `url:"-" json:"name"` // New name of the config.
}

// ConfigDeleteResponse represents a response from the config delete endpoint.
Expand All @@ -90,8 +90,8 @@ type (

// ConfigDeleteOptions represents the body parameters for a config delete request.
ConfigDeleteOptions struct {
Project string `url:"-" json:"project" validate:"required"` // Identifier of the project that the config belongs to.
Config string `url:"-" json:"config" validate:"required"` // Name of the config.
Project string `url:"-" json:"project"` // Identifier of the project that the config belongs to.
Config string `url:"-" json:"config"` // Name of the config.
}

// ConfigLockResponse represents a response from the config lock endpoint.
Expand All @@ -106,8 +106,8 @@ type (

// ConfigLockOptions represents the body parameters for a config lock request.
ConfigLockOptions struct {
Project string `url:"-" json:"project" validate:"required"` // Identifier of the project that the config belongs to.
Config string `url:"-" json:"config" validate:"required"` // Name of the config.
Project string `url:"-" json:"project"` // Identifier of the project that the config belongs to.
Config string `url:"-" json:"config"` // Name of the config.
}

// ConfigUnlockResponse represents a response from the config unlock endpoint.
Expand All @@ -122,8 +122,8 @@ type (

// ConfigUnlockOptions represents the body parameters for a config unlock request.
ConfigUnlockOptions struct {
Project string `url:"-" json:"project" validate:"required"` // Identifier of the project that the config belongs to.
Config string `url:"-" json:"config" validate:"required"` // Name of the config.
Project string `url:"-" json:"project"` // Identifier of the project that the config belongs to.
Config string `url:"-" json:"config"` // Name of the config.
}

// ConfigCloneResponse represents a response from the config clone endpoint.
Expand All @@ -138,8 +138,8 @@ type (

// ConfigCloneOptions represents the body parameters for a config clone request.
ConfigCloneOptions struct {
Project string `url:"-" json:"project" validate:"required"` // Identifier of the project that the config belongs to.
Config string `url:"-" json:"config" validate:"required"` // Name of the config.
NewConfig string `url:"-" json:"name" validate:"required"` // Name of the new config.
Project string `url:"-" json:"project"` // Identifier of the project that the config belongs to.
Config string `url:"-" json:"config"` // Name of the config.
NewConfig string `url:"-" json:"name"` // Name of the new config.
}
)
16 changes: 8 additions & 8 deletions config_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type (

// ConfigLogGetOptions represents the options for the config log get endpoint.
ConfigLogGetOptions struct {
Project string `url:"project" json:"-" validate:"required"` // Identifier of the project that the config belongs to.
Config string `url:"config" json:"-" validate:"required"` // Name of the config.
ID string `url:"log" json:"-" validate:"required"` // Unique identifier of the config log.
Project string `url:"project" json:"-"` // Identifier of the project that the config belongs to.
Config string `url:"config" json:"-"` // Name of the config.
ID string `url:"log" json:"-"` // Unique identifier of the config log.
}

// ConfigLogListResponse represents a response from the config log list endpoint.
Expand All @@ -51,8 +51,8 @@ type (
// ConfigLogListOptions represents the query parameters for a config log list request.
ConfigLogListOptions struct {
ListOptions `url:",inline" json:"-"`
Project string `url:"project" json:"-" validate:"required"` // Identifier of the project that the config belongs to.
Config string `url:"config" json:"-" validate:"required"` // Name of the config.
Project string `url:"project" json:"-"` // Identifier of the project that the config belongs to.
Config string `url:"config" json:"-"` // Name of the config.
}

// ConfigLogRollbackResponse represents a response from the config log rollback endpoint.
Expand All @@ -67,8 +67,8 @@ type (

// ConfigLogRollbackOptions represents the options for the config log rollback endpoint.
ConfigLogRollbackOptions struct {
Project string `url:"project" json:"-" validate:"required"` // Identifier of the project that the config belongs to.
Config string `url:"config" json:"-" validate:"required"` // Name of the config.
ID string `url:"log" json:"-" validate:"required"` // Unique identifier of the config log.
Project string `url:"project" json:"-"` // Identifier of the project that the config belongs to.
Config string `url:"config" json:"-"` // Name of the config.
ID string `url:"log" json:"-"` // Unique identifier of the config log.
}
)
29 changes: 1 addition & 28 deletions doppler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ import (

"github.com/pkg/errors"

"github.com/go-playground/validator/v10"

"github.com/nikoksr/doppler-go/logging"
"github.com/nikoksr/doppler-go/pointer"
)

const (
// SDKVersion is the version of the SDK
SDKVersion = "0.2.0"
SDKVersion = "0.2.1"

// APIURL is the base URL for the API
APIURL string = "https://api.doppler.com"
Expand All @@ -49,15 +47,6 @@ const (
// Key is the API key used to authenticate with the API
var Key string

// EnableValidation enables validation of the payload. This is enabled by default. If you want to disable validation,
// set this to false.
var EnableValidation = true

// Internal standard logger and validator.
var (
stdValidator = validator.New()
)

// BackendConfig is the configuration for the backend.
type BackendConfig struct {
// Client is the HTTP client to use for requests. If nil, a default client will be used.
Expand Down Expand Up @@ -303,25 +292,9 @@ func (req *Request) getBody() (*bytes.Buffer, error) {
return encodedBody, nil
}

// isPayloadValid uses the go-playground validator to validate the payload. It is expected that the payload is a struct
// and validation tags are used to define the validation rules. Using this function to make the code more readable.
// Validation is skipped if EnableValidation is false or the payload is nil.
func isPayloadValid(payload any) error {
if !EnableValidation || payload == nil {
return nil
}

return stdValidator.Struct(payload)
}

// prepareRequest creates a new HTTP request from the given Request. The returned request is ready to be sent to the
// API.
func (b *backendImplementation) prepareRequest(ctx context.Context, req *Request) (*http.Request, error) {
// Validate the request's payload before we do anything else.
if err := isPayloadValid(req.Payload); err != nil {
return nil, errors.Wrap(err, "validate request payload")
}

// Normalize URL
if !strings.HasPrefix(req.Path, "/") {
req.Path = "/" + req.Path
Expand Down
25 changes: 0 additions & 25 deletions doppler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,31 +522,6 @@ func Test_normalizeURL(t *testing.T) {
}
}

func Test_isPayloadValid(t *testing.T) {
t.Parallel()

// First, test a nil payload.
if err := isPayloadValid(nil); err != nil {
t.Fatalf("isPayloadValid(nil) returned an error: %v", err)
}

// Test a valid payload. All three fields of SecretGetOptions are required. The validator should not return an error.
getOptions := &SecretGetOptions{
Project: "fake-project",
Config: "fake-config",
Name: "fake-name",
}
if err := isPayloadValid(getOptions); err != nil {
t.Fatalf("isPayloadValid() returned an error: %v", err)
}

// Now, remove the project field. The validator should return an error.
getOptions.Project = ""
if err := isPayloadValid(getOptions); err == nil {
t.Fatal("isPayloadValid() should return an error")
}
}

func Test_Call(t *testing.T) {
t.Parallel()

Expand Down
16 changes: 8 additions & 8 deletions dynamic_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type (

// DynamicSecretIssueLeaseOptions represents the options for the dynamic secret issue lease endpoint.
DynamicSecretIssueLeaseOptions struct {
Project string `url:"-" json:"project" validate:"required"` // The project where the dynamic secret is located
Config string `url:"-" json:"config" validate:"required"` // The config where the dynamic secret is located
Name string `url:"-" json:"dynamic_secret" validate:"required"` // The dynamic secret to issue a lease for
TTLSeconds int32 `url:"-" json:"ttl_seconds" validate:"required"` // The number of seconds the lease should last
Project string `url:"-" json:"project"` // The project where the dynamic secret is located
Config string `url:"-" json:"config"` // The config where the dynamic secret is located
Name string `url:"-" json:"dynamic_secret"` // The dynamic secret to issue a lease for
TTLSeconds int32 `url:"-" json:"ttl_seconds"` // The number of seconds the lease should last
}

// DynamicSecretRevokeLeaseResponse represents a response from the dynamic secret revoke lease endpoint.
Expand All @@ -29,9 +29,9 @@ type (

// DynamicSecretRevokeLeaseOptions represents the options for the dynamic secret revoke lease endpoint.
DynamicSecretRevokeLeaseOptions struct {
Project string `url:"-" json:"project" validate:"required"` // The project where the dynamic secret is located
Config string `url:"-" json:"config" validate:"required"` // The config where the dynamic secret is located
Name string `url:"-" json:"dynamic_secret" validate:"required"` // The dynamic secret to revoke a lease for
Slug string `url:"-" json:"slug" validate:"required"` // The lease to revoke
Project string `url:"-" json:"project"` // The project where the dynamic secret is located
Config string `url:"-" json:"config"` // The config where the dynamic secret is located
Name string `url:"-" json:"dynamic_secret"` // The dynamic secret to revoke a lease for
Slug string `url:"-" json:"slug"` // The lease to revoke
}
)
24 changes: 12 additions & 12 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type (

// EnvironmentGetOptions represents the options for the environment get endpoint.
EnvironmentGetOptions struct {
Project string `url:"project" json:"-" validate:"required"` // Identifier of the project the environment belongs to.
Slug string `url:"environment" json:"-" validate:"required"` // A unique identifier for the environment.
Project string `url:"project" json:"-"` // Identifier of the project the environment belongs to.
Slug string `url:"environment" json:"-"` // A unique identifier for the environment.
}

// EnvironmentListResponse represents a response from the environment list endpoint.
Expand All @@ -39,7 +39,7 @@ type (

// EnvironmentListOptions represents the query parameters for a environment list request.
EnvironmentListOptions struct {
Project string `url:"project" json:"-" validate:"required"` // Identifier of the project the environment belongs to.
Project string `url:"project" json:"-"` // Identifier of the project the environment belongs to.
}

// EnvironmentCreateResponse represents a response from the environment create endpoint.
Expand All @@ -54,9 +54,9 @@ type (

// EnvironmentCreateOptions represents the body parameters for a environment create request.
EnvironmentCreateOptions struct {
Project string `url:"project" json:"-" validate:"required"` // Identifier of the project the environment belongs to.
Name string `url:"-" json:"name,omitempty" validate:"required"` // Name of the environment.
Slug string `url:"-" json:"slug,omitempty" validate:"required"` // A unique identifier for the environment.
Project string `url:"project" json:"-"` // Identifier of the project the environment belongs to.
Name string `url:"-" json:"name,omitempty"` // Name of the environment.
Slug string `url:"-" json:"slug,omitempty"` // A unique identifier for the environment.
}

// EnvironmentRenameResponse represents a doppler environment rename request.
Expand All @@ -71,10 +71,10 @@ type (

// EnvironmentRenameOptions represents the body parameters for a environment rename request.
EnvironmentRenameOptions struct {
Project string `url:"project" json:"-" validate:"required"` // Identifier of the project the environment belongs to.
Slug string `url:"environment" json:"-" validate:"required"` // A unique identifier for the environment.
NewName *string `url:"-" json:"name,omitempty"` // New name of the environment.
NewSlug *string `url:"-" json:"slug,omitempty"` // New slug of the environment.
Project string `url:"project" json:"-"` // Identifier of the project the environment belongs to.
Slug string `url:"environment" json:"-"` // A unique identifier for the environment.
NewName *string `url:"-" json:"name,omitempty"` // New name of the environment.
NewSlug *string `url:"-" json:"slug,omitempty"` // New slug of the environment.
}

// EnvironmentDeleteResponse represents a response from the environment delete endpoint.
Expand All @@ -88,7 +88,7 @@ type (

// EnvironmentDeleteOptions represents the body parameters for a environment delete request.
EnvironmentDeleteOptions struct {
Project string `url:"project" json:"-" validate:"required"` // Identifier of the project the environment belongs to.
Slug string `url:"environment" json:"-" validate:"required"` // A unique identifier for the environment.
Project string `url:"project" json:"-"` // Identifier of the project the environment belongs to.
Slug string `url:"environment" json:"-"` // A unique identifier for the environment.
}
)
14 changes: 7 additions & 7 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type (

// ProjectGetOptions represents the options for the project get endpoint.
ProjectGetOptions struct {
Name string `url:"project" json:"-" validate:"required"` // Name is the name of the project.
Name string `url:"project" json:"-"` // Name is the name of the project.
}

// ProjectListResponse represents a response from the project list endpoint.
Expand Down Expand Up @@ -52,8 +52,8 @@ type (

// ProjectCreateOptions represents the body parameters for a project create request.
ProjectCreateOptions struct {
Name string `url:"-" json:"name" validate:"required"` // Name of the project.
Description *string `url:"-" json:"description,omitempty"` // Description of the project.
Name string `url:"-" json:"name"` // Name of the project.
Description *string `url:"-" json:"description,omitempty"` // Description of the project.
}

// ProjectUpdateResponse represents a doppler project update request.
Expand All @@ -68,9 +68,9 @@ type (

// ProjectUpdateOptions represents the body parameters for a project update request.
ProjectUpdateOptions struct {
Name string `url:"-" json:"project" validate:"required"` // Name of the project.
NewName string `url:"-" json:"name,omitempty" validate:"required"` // New name of the project.
NewDescription *string `url:"-" json:"description,omitempty"` // New description of the project.
Name string `url:"-" json:"project"` // Name of the project.
NewName string `url:"-" json:"name,omitempty"` // New name of the project.
NewDescription *string `url:"-" json:"description,omitempty"` // New description of the project.
}

// ProjectDeleteResponse represents a response from the project delete endpoint.
Expand All @@ -84,6 +84,6 @@ type (

// ProjectDeleteOptions represents the body parameters for a project delete request.
ProjectDeleteOptions struct {
Name string `url:"-" json:"project" validate:"required"` // Name of the project.
Name string `url:"-" json:"project"` // Name of the project.
}
)
Loading

0 comments on commit 2be8d91

Please sign in to comment.