The StringSchema provides comprehensive validation for string values with support for length constraints, pattern matching, format validation, and more.
import "github.com/nyxstack/schema"
// Basic string schema
nameSchema := schema.String()
// With validation constraints
emailSchema := schema.String().
Email().
Required("Email is required")Marks the string as required (cannot be nil or omitted).
schema.String().Required()
schema.String().Required("Username is required")
schema.String().Required(i18n.S("username is required"))Marks the string as optional (can be nil or omitted).
schema.String().Optional()Allows the string value to be explicitly null.
schema.String().Nullable()Sets a default value when the input is nil.
schema.String().Default("guest")Sets the minimum length for the string.
schema.String().MinLength(3)
schema.String().MinLength(8, "Password must be at least 8 characters")
schema.String().MinLength(8, i18n.F("password must be at least %d characters", 8))Sets the maximum length for the string.
schema.String().MaxLength(100)
schema.String().MaxLength(20, "Username too long")Requires an exact string length.
schema.String().Length(10, "Must be exactly 10 characters")Validates the string against a regular expression.
// Alphanumeric only
schema.String().Pattern("^[a-zA-Z0-9]+$", "Must be alphanumeric")
// Phone number
schema.String().Pattern("^\\+?[1-9]\\d{1,14}$", "Invalid phone number")Validates email format.
schema.String().Email()
schema.String().Email("Invalid email address")Validates URL format.
schema.String().URL()
schema.String().URL("Invalid URL")Validates UUID format.
schema.String().UUID()
schema.String().UUID("Invalid UUID")Validates ISO 8601 date-time format.
schema.String().DateTime()Validates date format (YYYY-MM-DD).
schema.String().Date()Validates time format (HH:MM:SS).
schema.String().Time()Applies a format validator.
Available Formats:
StringFormatEmail- Email address validationStringFormatURI- URI validationStringFormatURL- URL validationStringFormatDateTime- ISO 8601 date-time (e.g., "2025-11-16T10:30:00Z")StringFormatDate- ISO 8601 date (e.g., "2025-11-16")StringFormatTime- ISO 8601 time (e.g., "10:30:00")StringFormatUUID- UUID validation (v1-v5)StringFormatHostname- Hostname validationStringFormatIPv4- IPv4 address validationStringFormatIPv6- IPv6 address validationStringFormatPassword- Password format (metadata only)StringFormatBinary- Binary data formatStringFormatByte- Base64 encoded byte data
schema.String().Format(schema.StringFormatIPv4)
schema.String().Format(schema.StringFormatIPv6)
schema.String().Format(schema.StringFormatHostname)
schema.String().Format(schema.StringFormatBinary)
schema.String().Format(schema.StringFormatByte)Restricts the string to a set of allowed values.
schema.String().Enum([]string{"active", "inactive", "pending"})
schema.String().Enum([]string{"red", "green", "blue"}, "Invalid color")Requires the string to match an exact value.
schema.String().Const("accepted")
schema.String().Const("yes", "Must agree to terms")Sets a title for documentation and JSON Schema generation.
schema.String().Title("User Email")Sets a description for documentation and JSON Schema generation.
schema.String().Description("The user's primary email address")nameSchema := schema.String().
MinLength(2).
MaxLength(50).
Required("Name is required")
ctx := schema.DefaultValidationContext()
result := nameSchema.Parse("John Doe", ctx)
if result.Valid {
fmt.Printf("Valid name: %s\n", result.Value)
}emailSchema := schema.String().
Email().
Required("Email is required")
result := emailSchema.Parse("user@example.com", ctx)usernameSchema := schema.String().
Pattern("^[a-zA-Z0-9_]{3,20}$", "Username must be 3-20 alphanumeric characters").
Required()
result := usernameSchema.Parse("john_doe123", ctx)statusSchema := schema.String().
Enum([]string{"draft", "published", "archived"}).
Default("draft")
result := statusSchema.Parse(nil, ctx) // Uses default: "draft"passwordSchema := schema.String().
MinLength(8, i18n.F("password must be at least %d characters", 8)).
Pattern(".*[A-Z].*", i18n.S("password must contain uppercase")).
Pattern(".*[0-9].*", i18n.S("password must contain number")).
Pattern(".*[!@#$%^&*].*", i18n.S("password must contain special char")).
Required(i18n.S("password is required"))
result := passwordSchema.Parse("SecurePass123!", ctx)roleSchema := schema.String().
Enum([]string{"admin", "user", "guest"}).
Default("guest").
Optional()
result := roleSchema.Parse(nil, ctx) // Returns "guest"All error messages support i18n through the github.com/nyxstack/i18n package:
schema.String().
MinLength(3, i18n.F("username must be at least %d characters", 3)).
Required(i18n.S("username is required"))schema := schema.String().
Title("User Email").
Description("Primary email address").
Email().
Required()
jsonSchema := schema.JSON()
// Outputs:
// {
// "type": "string",
// "format": "email",
// "title": "User Email",
// "description": "Primary email address"
// }result := schema.Parse(data, ctx)
if !result.Valid {
for _, err := range result.Errors {
fmt.Printf("Path: %v\n", err.Path)
fmt.Printf("Message: %s\n", err.Message)
fmt.Printf("Code: %s\n", err.Code)
}
}- Object Schema - For validating objects with string properties
- Array Schema - For arrays of strings
- Format Validation - Custom format validators
- i18n Support - Internationalization guide