- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Security
Validate User Input for Go Applications
Build a small Go HTTP API that validates JSON input using reusable rules, aggregates field errors, and returns consistent HTTP error responses (400 for malformed JSON, 422 for semantic validation failures). You will implement validation utilities, apply them to two endpoints, and confirm behavior with focused unit tests and lightweight integration tests.
Lab Info
Table of Contents
-
Challenge
Reusable validation error model
Input validation is most effective when clients get actionable feedback. In Go, ad-hoc maps and string errors often lead to inconsistent responses and hard-to-test behavior.
In this step you’ll create a typed validation error model that:
- Aggregates multiple field failures into a single error
- Is easy for handlers to detect and translate to HTTP responses
- Is easy for unit tests to assert on
This sets the foundation for predictable APIs and reduces duplicated error-building logic across endpoints. info> This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us.
-
Challenge
Reusable validation rules
Validation rules behave better when inputs are normalized first. Trimming whitespace and canonicalizing values like emails prevents false negatives and reduces downstream surprises.
You’ll also implement small, reusable validators that are:
- Composable and request-agnostic
- Easy to unit test
- Consistent in how they report failures
These building blocks will be reused across multiple endpoints, keeping your API behavior uniform.
-
Challenge
Validate registration endpoint
Good APIs distinguish between:
- Malformed syntax (invalid JSON): the server can’t parse the request (HTTP 400)
- Valid syntax but invalid meaning (failed validation): the server understood the JSON but it breaks business/semantic rules (HTTP 422)
In this step you’ll validate a registration payload, aggregate all field errors, and wire validation into the HTTP handler with correct status codes and a predictable JSON response shape.
-
Challenge
Optional field validation
Many real APIs support partial updates where fields are optional. The tricky part is correctly handling:
- Missing fields (no change)
- Present-but-empty fields (often invalid)
- Conditional validation (rules that apply only when a value is provided)
You’ll add profile update validation and a handler that reuses the same error conventions established earlier.
-
Challenge
Consistent error responses
Consistency is a feature. Centralizing error response formatting prevents endpoints from drifting over time.
Finally, you’ll add an integration test that validates the complete request path (router → handler → validation → store) to catch wiring mistakes that unit tests may miss.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.