Custom Tags
Built-in Tags
Sentinel extracts these tags by default:
| Tag | Purpose |
|---|---|
json | JSON serialization |
db | Database mapping |
validate | Validation rules |
scope | Authorization scopes |
encrypt | Encryption markers |
redact | Redaction hints |
desc | Field descriptions |
example | Example values |
Registering Custom Tags
Register additional tags before extraction:
// Register at init or before first Inspect/Scan
sentinel.Tag("graphql")
sentinel.Tag("proto")
sentinel.Tag("xml")
Tags can be registered at any time, but only affect future extractions:
sentinel.Inspect[User]() // "custom" tag NOT extracted
sentinel.Tag("custom")
sentinel.Inspect[Product]() // "custom" tag IS extracted
sentinel.Inspect[User]() // Still cached without "custom"
Accessing Tags
Tags are stored as a map on each field:
type User struct {
Email string `json:"email" validate:"required,email" custom:"pii"`
}
metadata := sentinel.Inspect[User]()
field := metadata.Fields[0]
field.Tags["json"] // "email"
field.Tags["validate"] // "required,email"
field.Tags["custom"] // "pii"
Tag Parsing
Sentinel preserves the raw tag value. Parsing tag syntax is your responsibility:
// Tag value: "required,email,max=100"
rules := field.Tags["validate"]
// Parse as needed for your validation library
parts := strings.Split(rules, ",")
Use Cases
Validation Rules
type Form struct {
Email string `validate:"required,email"`
Password string `validate:"required,min=8"`
}
for _, field := range metadata.Fields {
if rules, ok := field.Tags["validate"]; ok {
// Apply validation logic
}
}
Database Schema
type Model struct {
ID string `db:"id,primarykey"`
Name string `db:"name,index"`
}
for _, field := range metadata.Fields {
if col, ok := field.Tags["db"]; ok {
// Generate DDL or migrations
}
}
API Documentation
type Request struct {
UserID string `json:"user_id" example:"usr_123" desc:"The user identifier"`
}
for _, field := range metadata.Fields {
example := field.Tags["example"]
desc := field.Tags["desc"]
// Generate OpenAPI spec
}
Next Steps
- Scanning — recursive type discovery
- Testing — testing tag extraction
- API Reference — Tag() function documentation