Hone logo
Hone
Problems

Go Request Validation with go-playground/validator

Building robust web applications in Go often involves validating incoming request data before processing it. This ensures data integrity, security, and prevents unexpected application behavior. This challenge will guide you through implementing request validation using the popular go-playground/validator library.

Problem Description

Your task is to create a Go program that validates incoming JSON request payloads against a predefined schema. You will define the structure of your data using Go structs and leverage the go-playground/validator library to enforce validation rules. The program should be able to handle successful validations and return appropriate error messages for invalid data.

Key Requirements:

  • Define a Go struct to represent the expected request payload.
  • Annotate the struct fields with validation tags provided by go-playground/validator.
  • Implement a function that takes a JSON byte slice as input, unmarshals it into the defined struct, and then validates the struct.
  • The validation function should return a boolean indicating success or failure, and a list of detailed error messages if validation fails.
  • Handle cases where the input is not valid JSON.

Expected Behavior:

  • If the input JSON is valid and conforms to the struct's validation rules, the function should return true and an empty list of errors.
  • If the input JSON is valid but violates one or more validation rules, the function should return false and a list of specific error messages.
  • If the input JSON is malformed (not valid JSON), the function should return false and an error message indicating the JSON parsing failure.

Edge Cases to Consider:

  • Empty input JSON.
  • JSON with missing required fields.
  • JSON with fields of incorrect data types.
  • JSON with values that violate specific validation constraints (e.g., minimum/maximum length, range).

Examples

Example 1: Successful Validation

Input JSON:
{
  "name": "Alice",
  "age": 30,
  "email": "alice@example.com",
  "tags": ["golang", "validation"]
}
Output:
IsValid: true
Errors: []

Explanation: The input JSON represents a valid user profile that adheres to all defined validation rules.

Example 2: Validation Failure (Missing Required Field and Invalid Age)

Input JSON:
{
  "name": "Bob",
  "email": "bob@example.com"
}
Output:
IsValid: false
Errors: [
  "Key: 'User.Age' Error:Field validation for 'Age' failed on the 'required' tag",
  "Key: 'User.Age' Error:Field validation for 'Age' failed on the 'gt' tag (minimum: 18)"
]

Explanation: The age field is missing (required) and also below the minimum age of 18.

Example 3: Malformed JSON

Input JSON:
{
  "name": "Charlie",
  "age": 25,
  "email": "charlie@example.com",
  "tags": ["testing" // missing closing bracket
}
Output:
IsValid: false
Errors: [
  "Error parsing JSON: invalid character '\"' after array element: all recent tokens were consumed"
]

Explanation: The input is not valid JSON due to a syntax error (missing closing bracket for the tags array).

Constraints

  • The go-playground/validator library must be used for validation.
  • The validation logic should be contained within a single function.
  • The program should gracefully handle errors during JSON unmarshalling.
  • The output error messages should be clear and informative, indicating which field and which validation rule was violated.

Notes

  • Familiarize yourself with the common validation tags provided by go-playground/validator such as required, email, gt (greater than), gte (greater than or equal to), lt (less than), lte (less than or equal to), min, max, url, etc.
  • Consider how you will structure your Go struct to best represent the expected request data and apply the appropriate validation tags.
  • Think about how to present the validation errors to the user in a user-friendly format.
Loading editor...
go