mrz1836/go-sanitize

Repository files navigation

Lightweight Go library providing robust string sanitization and normalization utilities

CI / CDQuality & SecurityDocs & MetaCommunity
Latest Release
Build Status
CodeQL
Last commit
Go Report Card
Code Coverage
OpenSSF Scorecard
Security policy
OpenSSF Best Practices
Go version
Go docs
AGENTS.md rules
Makefile Supported
Dependabot
Contributors
Sponsor
Donate Bitcoin


go-sanitize requires a supported release of Go.

go get -u .com/mrz1836/go-sanitize

Here is a basic example of how to use go-sanitize in your Go project:

package main

import (
    "fmt"
    ".com/mrz1836/go-sanitize"
)

func main() {
	// Sanitize a string to remove unwanted characters
	input := "Hello, World! @2025"
	sanitized := sanitize.AlphaNumeric(input, false) // true to keep spaces

	// Output: "Sanitized String: HelloWorld2025"
	fmt.Println("Sanitized String:", sanitized) 
}

View the generated documentation

Heads up! go-sanitize is intentionally light on dependencies. The only external package it uses is the excellent testify suite—and that's just for our tests. You can drop this library into your projects without dragging along extra baggage.


  • Alpha and alphanumeric sanitization with optional spaces
  • Bitcoin and Bitcoin Cash address sanitizers
  • Custom regular expression helper for arbitrary patterns
  • Precompiled regex sanitizer for repeated patterns
  • Decimal, domain, email and IP address normalization
  • HTML and XML stripping with script removal
  • URI, URL and XSS sanitization
  • Alpha: Remove non-alphabetic characters, optionally keep spaces
  • AlphaNumeric: Remove non-alphanumeric characters, optionally keep spaces
  • BitcoinAddress: Filter input to valid Bitcoin address characters
  • BitcoinCashAddress: Filter input to valid Bitcoin Cash address characters
  • Custom: Use a custom regex to filter input (legacy)
  • CustomCompiled: Use a precompiled custom regex to filter input (suggested)
  • Decimal: Keep only decimal or float characters
  • Domain: Sanitize domain, optionally preserving case and removing www
  • Email: Normalize an email address
  • FirstToUpper: Capitalize the first letter of a string
  • FormalName: Keep only formal name characters
  • HTML: Strip HTML tags
  • IPAddress: Return sanitized and valid IPv4 or IPv6 address
  • Numeric: Remove all but numeric digits
  • PhoneNumber: Keep digits and plus signs for phone numbers
  • PathName: Sanitize to a path-friendly name
  • Punctuation: Allow letters, numbers and basic punctuation
  • ScientificNotation: Keep characters valid in scientific notation
  • Scripts: Remove scripts, iframe and object tags
  • SingleLine: Replace line breaks and tabs with spaces
  • Time: Keep only valid time characters
  • URI: Keep characters allowed in a URI
  • URL: Keep characters allowed in a URL
  • XML: Strip XML tags
  • XSS: Remove common XSS attack strings
Library Deployment

This project uses goreleaser for streamlined binary and library deployment to . To get started, install it via:

brew install goreleaser

The release process is defined in the .goreleaser.yml configuration file.

To generate a snapshot (non-versioned) release for testing purposes, run:

make release-snap

Before tagging a new version, update the release metadata in the CITATION.cff file:

make citation version=0.2.1

Then create and push a new Git tag using:

make tag version=x.y.z

This process ensures consistent, repeatable releases with properly versioned artifacts and citation metadata.

Makefile Commands

View all makefile commands

make help

List of all current commands:

bench                 ## Run all benchmarks in the Go application
build-go              ## Build the Go application (locally)
citation              ## Update version in CITATION.cff (use version=X.Y.Z)
clean-mods            ## Remove all the Go mod cache
coverage              ## Show test coverage
diff                  ## Show git diff and fail if uncommitted changes exist
generate              ## Run go generate in the base of the repo
godocs                ## Trigger GoDocs tag sync
govulncheck-install   ## Install govulncheck
help                  ## Display this help message
install-go            ## Install using go install with specific version
install-releaser      ## Install GoReleaser
install               ## Install the application binary
lint                  ## Run the golangci-lint application (install if not found)
release-snap          ## Build snapshot binaries
release-test          ## Run release dry-run (no publish)
release               ## Run production release (requires _token)
run-fuzz-tests        ## Run fuzz tests for all packages
tag-remove            ## Remove local and remote tag (use version=X.Y.Z)
tag-update            ## Force-update tag to current commit (use version=X.Y.Z)
tag                   ## Create and push a new tag (use version=X.Y.Z)
test-ci-no-race       ## CI test suite without race detector
test-ci-short         ## CI unit-only short tests
test-ci               ## CI full test suite with coverage
test-no-lint          ## Run only tests (no lint)
test-short            ## Run tests excluding integration
test-unit             ## Runs tests and outputs coverage
test                  ## Run lint and all tests
uninstall             ## Uninstall the Go binary
update-linter         ## Upgrade golangci-lint (macOS only)
update-releaser       ## Reinstall GoReleaser
update                ## Update dependencies
vet                   ## Run go vet
Workflows
Workflow NameDescription
auto-merge-on-approval.ymlAutomatically merges PRs after approval and all required checks, following strict rules.
check-for-s.ymlRuns gits to detect secrets on a daily schedule.
clean-runner-cache.ymlRemoves Actions caches tied to closed pull requests.
codeql-analysis.ymlAnalyzes code for security vulnerabilities using CodeQL.
delete-merged-branches.ymlDeletes feature branches after their pull requests are merged.
dependabot-auto-merge.ymlAutomatically merges Dependabot PRs that meet all requirements.
pull-request-management.ymlLabels PRs by branch prefix, assigns a default user if none is assigned, and welcomes new contributors with a comment.
release.ymlBuilds and publishes releases via GoReleaser when a semver tag is pushed.
run-tests.ymlRuns linter, Go tests and dependency checks on every push and pull request.
scorecard.ymlRuns OpenSSF Scorecard to assess supply chain security.
stale.ymlWarns about (and optionally closes) inactive issues and PRs on a schedule or manual trigger.
sync-labels.ymlKeeps labels in sync with the declarative manifest at ./labels.yml.
Updating Dependencies

To update all dependencies (Go modules, linters, and related tools), run:

make update

This command ensures all dependencies are brought up to date in a single step, including Go modules and any tools managed by the Makefile. It is the recommended way to keep your development environment and CI in sync with the latest versions.


All unit tests and examples run via Actions and use Go version 1.18.x. View the configuration file.

Run all tests:

make test

Run the Go benchmarks:

make bench

Performance benchmarks for the core functions in this library, executed on an Apple M1 Max (ARM64):

BenchmarkIterationsns/opB/opallocs/op
Alpha15,108,70378.7241
Alpha_WithSpaces13,972,90383.2241
AlphaNumeric10,619,542112.0321
AlphaNumeric_WithSpaces10,005,721118.9321
BitcoinAddress10,766,221112.0481
BitcoinCashAddress7,910,431151.6481
Custom (Legacy)920,3361,277.094417
CustomCompiled1,638,974730.6965
Decimal18,779,28162.74241
Domain4,988,238243.21763
Domain_PreserveCase5,707,197210.41602
Domain_RemoveWww4,991,971240.41763
Email8,781,903137.2482
Email_PreserveCase13,118,78692.15241
FirstToUpper65,587,06317.93161
FormalName15,207,22978.84241
HTML2,557,639469.4643
IPAddress11,802,175101.4483
IPAddress_IPV62,997,530384.01123
Numeric27,050,88844.0161
PhoneNumber18,070,62166.33241
PathName15,465,88578.74241
Punctuation9,166,885130.7481
ScientificNotation19,580,97961.32241
Scripts2,025,324594.6642
SingleLine12,599,41695.94321
Time24,114,90748.93161
URI11,414,026104.7321
URL11,462,407105.1321
XML4,179,268288.5563
XSS3,499,938345.1402

These benchmarks reflect fast, allocation-free lookups for most retrieval functions, ensuring optimal performance in production environments.


Read more about this Go project's code standards.


This project documents expectations for AI assistants using a few dedicated files:

  • AGENTS.md — canonical rules for coding style, workflows, and pull requests used by Codex.
  • CLAUDE.md — quick checklist for the Claude agent.
  • .cursorrules — machine-readable subset of the policies for Cursor and similar tools.
  • sweep.yaml — rules for Sweep, a tool for code review and pull request management.

Edit AGENTS.md first when adjusting these policies, and keep the other files in sync within the same pull request.


MrZ
MrZ

View the contributing guidelines and please follow the code of conduct.

All kinds of contributions are welcome 🙌! The most basic way to show your support is to star 🌟 the project, or to raise issues 💬. You can also support this project by becoming a sponsor on 👏 or by making a bitcoin donation to ensure this journey continues indefinitely! 🚀

Stars


License

About

🛁 Lightweight Go library providing robust string sanitization and normalization utilities

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 6