liveBook · Manning

Sanitization

As explained in Shipping Go: Develop, deliver, discuss, design, and go again

Input sanitation is the processing of incoming values to make them uniform and acceptable before further handling.

Input sanitization makes incoming values uniform before service logic handles them. A translator service, for example, should treat capitalization and surrounding whitespace consistently rather than reject otherwise valid requests. section 3.3

A practical sanitization pass can:

  • Convert text to lowercase. section 3.3
  • Trim surrounding whitespace. section 3.3
  • Reject unsupported words, languages, negative numbers, or invalid parameters after normalization. section 3.3
  • Add tests for each variation and rerun them after the fix. section 3.3

Normalizing translator input

A Go service sanitizes both arguments before checking them: sanitizeInput applies strings.ToLower and strings.TrimSpace. Inputs such as "German", "Hello", and "hello " can then produce the same expected translation as their lowercase, trimmed forms. section 3.3

Listing 3.12 translator.go
package translation
 
import "strings"
 
func Translate(word string, language string) string {
    word = sanitizeInput(word)                          #1
    language = sanitizeInput(language)                  #2
 
    if word != "hello" {
        return ""
    }
 
    switch language {
    case "english":
        return "hello"
    case "finnish":
        return "hei"
    case "german":
        return "hallo"
    default:
        return ""
    }
 
}
 
func sanitizeInput(w string) string {                   #3
    w = strings.ToLower(w)
    return strings.TrimSpace(w)
}

See the complete Go implementation, including sanitization before word validation and language selection. section 3.3

How Shipping Go: Develop, deliver, discuss, design, and go again covers it

How deep it goes
Demonstrates lowercase conversion, whitespace trimming, edge-case tests, and service-level validation before deferring handler coverage.
Examples
Tests capitalization, trailing whitespace, unsupported words, and unsupported languages in a Go translation service.
What it assumes
Assumes basic service and unit-testing knowledge while illustrating sanitization through a small translator example.
What you can do after
Apply it by normalizing service arguments and testing variations before accepting their downstream behavior.
How it connects
Connects input validation with unit testing, service layers, handlers, and quality assurance.

Also explained in

Compare how 3 books explain sanitization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Shipping Go: Develop, deliver, discuss, design, and go again. The section links point to the parts of the book it draws on.