# Go Utilities for MIME Type Validation: Exploring the vmime Package

> Discover Go utilities in the vmime package for robust MIME type validation. Explore IsValidMimeType, MimeTypeRe, and ErrInvalidMimeType for your next project.

- Repository: [Aperture Robotics/util](https://github.com/aperturerobotics/util)
- Tags: how-to-guide
- Published: 2026-02-25

---

**The vmime package provides three essential Go utilities for MIME type validation: a compiled regular expression (`MimeTypeRe`), a boolean validation function (`IsValidMimeType`), and a sentinel error (`ErrInvalidMimeType`).**

The `vmime` package in the `github.com/aperturerobotics/util` repository offers lightweight Go utilities for MIME type validation without external dependencies. This self-contained helper enables developers to verify MIME type strings using a strict regular expression pattern, making it ideal for applications that need to validate content types, file uploads, or HTTP headers.

## Core Go Utilities for MIME Type Validation

The `vmime` package exports three symbols that constitute its complete public API. These utilities work together to provide robust MIME type validation in Go applications.

### MimeTypeRe: The Compiled Regular Expression

**`MimeTypeRe`** is a pre-compiled `*regexp.Regexp` that defines the syntactic rules for valid MIME types. The pattern `^[-\w.]+/[-\w.]+$` enforces that MIME types must contain exactly one forward slash separating the type and subtype, with both components consisting of word characters, hyphens, and dots.

According to the source code in [`vmime/vmime.go`](https://github.com/aperturerobotics/util/blob/main/vmime/vmime.go) (line 9), this regex matches strings such as `"text/plain"` or `"application/json"` while rejecting malformed values like `"invalid/mime/type/"` or `"badtype"`.

### IsValidMimeType: The Validation Function

**`IsValidMimeType`** is a convenience wrapper function that returns `true` when the supplied string satisfies `MimeTypeRe`. Implemented in [`vmime/vmime.go`](https://github.com/aperturerobotics/util/blob/main/vmime/vmime.go) (lines 11-14), this function provides the primary API that callers use to test MIME type strings.

The function signature is:

```go
func IsValidMimeType(str string) bool

```

This boolean return pattern makes it ideal for use in conditional validation logic, middleware checks, or input sanitization routines.

### ErrInvalidMimeType: The Sentinel Error

**`ErrInvalidMimeType`** is a package-level sentinel error defined as `errors.New("invalid mime type")` in [`vmime/vmime.go`](https://github.com/aperturerobotics/util/blob/main/vmime/vmime.go) (line 17). This exported error allows callers to return or compare against a standardized error value when validation fails, facilitating consistent error handling across applications that consume the `vmime` package.

## Implementation Details in vmime/vmime.go

The complete implementation of the `vmime` package resides in a single file, [`vmime/vmime.go`](https://github.com/aperturerobotics/util/blob/main/vmime/vmime.go). The source code demonstrates a minimal, zero-dependency approach to MIME type validation:

```go
// vmime/vmime.go
var MimeTypeRe = regexp.MustCompile(`^[-\w.]+/[-\w.]+$`)

func IsValidMimeType(str string) bool {
    return MimeTypeRe.MatchString(str)
}

var ErrInvalidMimeType = errors.New("invalid mime type")

```

This implementation intentionally avoids complex parsing logic, relying instead on the performance and reliability of Go's `regexp` package. The regex pattern strictly enforces the `type/subtype` format without allowing additional parameters or whitespace, making it suitable for security-sensitive validation contexts.

## Code Examples: Validating MIME Types in Go

The following examples demonstrate practical usage of the `vmime` package utilities in real-world Go applications.

### Basic Validation with IsValidMimeType

Use `IsValidMimeType` to quickly verify MIME type strings in validation logic:

```go
package main

import (
	"fmt"

	"github.com/aperturerobotics/util/vmime"
)

func main() {
	tests := []string{
		"text/plain",
		"application/json",
		"invalid/mime/type/",
		"image/png",
		"badtype",
	}

	for _, t := range tests {
		if vmime.IsValidMimeType(t) {
			fmt.Printf("%q is a valid MIME type\n", t)
		} else {
			fmt.Printf("%q is NOT a valid MIME type (%v)\n", t, vmime.ErrInvalidMimeType)
		}
	}
}

```

**Output:**

```

"text/plain" is a valid MIME type
"application/json" is a valid MIME type
"invalid/mime/type/" is NOT a valid MIME type (invalid mime type)
"image/png" is a valid MIME type
"badtype" is NOT a valid MIME type (invalid mime type)

```

### Advanced Usage with MimeTypeRe

For scenarios requiring pattern matching without boolean logic, access the compiled regex directly:

```go
if vmime.MimeTypeRe.MatchString(s) {
    // String matches the MIME type pattern
    fmt.Println("Valid format detected")
}

```

This approach is useful when integrating with validation frameworks that accept `*regexp.Regexp` instances, or when you need to inspect the regex pattern itself for debugging purposes.

## Summary

The `vmime` package in `github.com/aperturerobotics/util` provides focused Go utilities for MIME type validation through three exported symbols:

- **`MimeTypeRe`**: A compiled regex (`^[-\w.]+/[-\w.]+$`) that defines valid MIME type syntax
- **`IsValidMimeType`**: A boolean function that tests strings against `MimeTypeRe`
- **`ErrInvalidMimeType`**: A sentinel error for consistent validation failure handling

These utilities reside in [`vmime/vmime.go`](https://github.com/aperturerobotics/util/blob/main/vmime/vmime.go) and offer a zero-dependency, performance-oriented solution for applications requiring strict MIME type validation.

## Frequently Asked Questions

### What regex pattern does the vmime package use for MIME type validation?

The `vmime` package uses the pattern `^[-\w.]+/[-\w.]+$` compiled into `MimeTypeRe`. This regex requires exactly one forward slash separating the type and subtype components, with both sides limited to word characters (letters, digits, underscores), hyphens, and dots. It explicitly rejects strings with multiple slashes, spaces, or parameter suffixes like `; charset=utf-8`.

### How do I import the vmime package in my Go project?

Add the `github.com/aperturerobotics/util` module to your project and import the `vmime` subpackage:

```go
import "github.com/aperturerobotics/util/vmime"

```

Since `vmime` is a subpackage within the larger `util` repository, you get only the MIME validation code without importing unrelated utilities. The package has no external dependencies beyond Go's standard library.

### Can I use vmime.MimeTypeRe for extracting MIME type components?

While `MimeTypeRe` validates the overall format, it does not contain capturing groups to extract the type and subtype separately. The current implementation in [`vmime/vmime.go`](https://github.com/aperturerobotics/util/blob/main/vmime/vmime.go) uses a simple validation regex without subgroups. If you need to parse components, you should use `strings.Split` after validation or modify the regex to include capture groups like `^([-\w.]+)/([-\w.]+)$`.

### Is the vmime package suitable for production use?

Yes, the `vmime` package is designed for production environments requiring strict, lightweight MIME type validation. The implementation in [`vmime/vmime.go`](https://github.com/aperturerobotics/util/blob/main/vmime/vmime.go) uses compiled regular expressions for optimal performance and exports sentinel errors for proper error handling. However, it intentionally does not support RFC 2045 parameter syntax (e.g., `text/plain; charset=us-ascii`), so applications requiring full MIME parsing should consider more comprehensive libraries like `mime` or `github.com/gabriel-vasile/mimetype`.