help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
go-rest-api
/
internal
/
validation
RSK World
go-rest-api
Go REST API - Enterprise-grade REST API with JWT Authentication + PostgreSQL + Redis Caching + Docker + Comprehensive Testing + Educational Design
validation
  • validation.go1.4 KB
  • validation_test.go1.7 KB
validation_test.go
internal/validation/validation_test.go
Raw Download
Find: Go to:
/*
* Author: RSK World
* Email: help@rskworld.in / support@rskworld.in
* Website: https://rskworld.in/contact.php
* Year: 2026
*/

package validation

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

type TestStruct struct {
	Name  string `validate:"required,min=2,max=10"`
	Email string `validate:"required,email"`
	Age   int    `validate:"gte=18"`
}

func TestValidateStruct(t *testing.T) {
	tests := []struct {
		name      string
		input     TestStruct
		expectErr bool
	}{
		{
			name: "Valid struct",
			input: TestStruct{
				Name:  "John Doe",
				Email: "john@example.com",
				Age:   25,
			},
			expectErr: false,
		},
		{
			name: "Missing required field",
			input: TestStruct{
				Email: "john@example.com",
				Age:   25,
			},
			expectErr: true,
		},
		{
			name: "Invalid email",
			input: TestStruct{
				Name:  "John Doe",
				Email: "invalid-email",
				Age:   25,
			},
			expectErr: true,
		},
		{
			name: "Name too short",
			input: TestStruct{
				Name:  "J",
				Email: "john@example.com",
				Age:   25,
			},
			expectErr: true,
		},
		{
			name: "Age too low",
			input: TestStruct{
				Name:  "John Doe",
				Email: "john@example.com",
				Age:   16,
			},
			expectErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := ValidateStruct(tt.input)
			if tt.expectErr {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)
			}
		})
	}
}

func TestFormatValidationError(t *testing.T) {
	// This function is tested indirectly through ValidateStruct
	// but we can test the error formatting logic
	err := ValidateStruct(TestStruct{Name: ""})
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "required")
}
92 lines•1.7 KB
go

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer