Documentation
¶
Index ¶
- Variables
- func MaxBytes(n int) codex.Constraint[[]byte]
- func MaxDuration(d time.Duration) codex.Constraint[time.Duration]
- func MaxFloat(n float64) codex.Constraint[float64]
- func MaxInt(n int) codex.Constraint[int]
- func MaxInt32(n int32) codex.Constraint[int32]
- func MaxInt64(n int64) codex.Constraint[int64]
- func MaxLen(n int) codex.Constraint[string]
- func MaxUint(n uint) codex.Constraint[uint]
- func MaxUint64(n uint64) codex.Constraint[uint64]
- func MinBytes(n int) codex.Constraint[[]byte]
- func MinDuration(d time.Duration) codex.Constraint[time.Duration]
- func MinFloat(n float64) codex.Constraint[float64]
- func MinInt(n int) codex.Constraint[int]
- func MinInt32(n int32) codex.Constraint[int32]
- func MinInt64(n int64) codex.Constraint[int64]
- func MinLen(n int) codex.Constraint[string]
- func MinUint(n uint) codex.Constraint[uint]
- func MinUint64(n uint64) codex.Constraint[uint64]
- func OneOf(values ...string) codex.Constraint[string]
- func Pattern(re *regexp.Regexp) codex.Constraint[string]
- func RangeFloat(min, max float64) codex.Constraint[float64]
- func RangeInt(min, max int) codex.Constraint[int]
- func RangeInt32(min, max int32) codex.Constraint[int32]
- func RangeInt64(min, max int64) codex.Constraint[int64]
- func RangeUint(min, max uint) codex.Constraint[uint]
- func RangeUint64(min, max uint64) codex.Constraint[uint64]
- func URLWithSchemes(schemes ...string) codex.Constraint[string]
Constants ¶
This section is empty.
Variables ¶
var CIDR = codex.Constraint[string]{ Name: "cidr", Check: func(v string) bool { _, _, err := net.ParseCIDR(v) return err == nil }, Message: func(v string) string { return fmt.Sprintf("invalid CIDR notation: %q", v) }, }
CIDR is a Constraint that requires a valid CIDR notation string (e.g. "192.168.0.0/24", "10.0.0.0/8", "::/0"). Host bits may be set (e.g. "192.168.0.1/24" is accepted). No schema annotation — there is no standard JSON Schema format for CIDR.
var Date = codex.Constraint[string]{ Name: "date", Check: func(v string) bool { _, err := time.Parse("2006-01-02", v) return err == nil }, Message: func(v string) string { return fmt.Sprintf("invalid date (expected YYYY-MM-DD): %q", v) }, Schema: withFormat("date"), }
Date is a Constraint that requires an ISO 8601 date string (YYYY-MM-DD).
var DateTime = codex.Constraint[string]{ Name: "date-time", Check: func(v string) bool { _, err := time.Parse(time.RFC3339, v) if err == nil { return true } _, err = time.Parse(time.RFC3339Nano, v) return err == nil }, Message: func(v string) string { return fmt.Sprintf("invalid date-time (expected RFC 3339): %q", v) }, Schema: withFormat("date-time"), }
DateTime is a Constraint that requires an RFC 3339 date-time string.
var Email = codex.Constraint[string]{ Name: "email", Check: func(v string) bool { return reEmail.MatchString(v) }, Message: func(v string) string { return fmt.Sprintf("invalid email address: %q", v) }, Schema: withFormat("email"), }
Email is a Constraint that requires a valid email address. Validation uses a standard format check; it does not perform DNS lookup.
var HTTPPath = codex.Constraint[string]{ Name: "http-path", Check: func(v string) bool { return httpPathRe.MatchString(v) }, Message: func(v string) string { switch { case v == "" || v[0] != '/': return fmt.Sprintf("http path must start with '/', got %q", v) case strings.ContainsRune(v, 0): return fmt.Sprintf("http path must not contain null bytes, got %q", v) case strings.ContainsRune(v, ' '): return fmt.Sprintf("http path must not contain unencoded spaces, got %q", v) default: return fmt.Sprintf("invalid http path: %q", v) } }, }
HTTPPath is a Constraint that validates an HTTP path string. It requires the path to start with '/' and contain no unencoded spaces or null bytes. OpenAPI-style path parameters (e.g. /users/{id}) are permitted.
var Hostname = codex.Constraint[string]{ Name: "hostname", Check: func(v string) bool { return len(v) <= 253 && len(v) > 0 && reHostname.MatchString(v) }, Message: func(v string) string { return fmt.Sprintf("invalid hostname: %q", v) }, Schema: withFormat("hostname"), }
Hostname is a Constraint that requires a valid RFC 1123 hostname. Labels must be 1–63 characters; total length must not exceed 253 characters.
var IPv4 = codex.Constraint[string]{ Name: "ipv4", Check: func(v string) bool { ip := net.ParseIP(v) return ip != nil && ip.To4() != nil && strings.Contains(v, ".") }, Message: func(v string) string { return fmt.Sprintf("invalid IPv4 address: %q", v) }, Schema: withFormat("ipv4"), }
IPv4 is a Constraint that requires a valid IPv4 address.
var IPv6 = codex.Constraint[string]{ Name: "ipv6", Check: func(v string) bool { ip := net.ParseIP(v) return ip != nil && ip.To4() == nil }, Message: func(v string) string { return fmt.Sprintf("invalid IPv6 address: %q", v) }, Schema: withFormat("ipv6"), }
IPv6 is a Constraint that requires a valid IPv6 address.
var MQTTPublishTopic = codex.Constraint[string]{ Name: "mqtt-publish-topic", Check: func(v string) bool { return v != "" && !strings.ContainsRune(v, 0) && len(v) <= 65535 && !strings.ContainsAny(v, "+#") }, Message: func(v string) string { switch { case v == "": return "mqtt publish topic must not be empty" case strings.ContainsRune(v, 0): return "mqtt publish topic must not contain null bytes" case len(v) > 65535: return fmt.Sprintf("mqtt publish topic exceeds maximum length of 65535 bytes, got %d", len(v)) case strings.ContainsAny(v, "+#"): return fmt.Sprintf("mqtt publish topic must not contain wildcard characters '+' or '#', got %q", v) default: return fmt.Sprintf("invalid mqtt publish topic: %q", v) } }, }
MQTTPublishTopic is a Constraint that validates an MQTT topic string for publishing. It applies all rules from MQTTTopic and additionally forbids wildcard characters ('+' and '#'), which are reserved for subscriptions only.
var MQTTTopic = codex.Constraint[string]{ Name: "mqtt-topic", Check: func(v string) bool { return v != "" && !strings.ContainsRune(v, 0) && utf8.RuneCountInString(v) > 0 && len(v) <= 65535 }, Message: func(v string) string { switch { case v == "": return "mqtt topic must not be empty" case strings.ContainsRune(v, 0): return "mqtt topic must not contain null bytes" case len(v) > 65535: return fmt.Sprintf("mqtt topic exceeds maximum length of 65535 bytes, got %d", len(v)) default: return fmt.Sprintf("invalid mqtt topic: %q", v) } }, }
MQTTTopic is a Constraint that validates an MQTT topic string for general use (subscribe or publish). It requires the string to be non-empty, contain no null bytes (U+0000), and be at most 65535 UTF-8 bytes — as required by the MQTT specification (section 4.7).
var NegativeFloat = codex.Constraint[float64]{ Name: "negative", Check: func(v float64) bool { return v < 0 }, Message: func(v float64) string { return fmt.Sprintf("expected negative number, got %g", v) }, Schema: func(s schema.Schema) schema.Schema { s.Maximum = float64ptr(0) s.ExclusiveMaximum = true return s }, }
NegativeFloat is a Constraint that requires float64 < 0.
var NegativeInt = codex.Constraint[int]{ Name: "negative", Check: func(v int) bool { return v < 0 }, Message: func(v int) string { return fmt.Sprintf("expected negative integer, got %d", v) }, Schema: func(s schema.Schema) schema.Schema { s.Maximum = float64ptr(0) s.ExclusiveMaximum = true return s }, }
NegativeInt is a Constraint that requires int < 0.
var NegativeInt32 = codex.Constraint[int32]{ Name: "negative", Check: func(v int32) bool { return v < 0 }, Message: func(v int32) string { return fmt.Sprintf("expected negative integer, got %d", v) }, Schema: func(s schema.Schema) schema.Schema { s.Maximum = float64ptr(0) s.ExclusiveMaximum = true return s }, }
NegativeInt32 is a Constraint that requires int32 < 0.
var NegativeInt64 = codex.Constraint[int64]{ Name: "negative", Check: func(v int64) bool { return v < 0 }, Message: func(v int64) string { return fmt.Sprintf("expected negative integer, got %d", v) }, Schema: func(s schema.Schema) schema.Schema { s.Maximum = float64ptr(0) s.ExclusiveMaximum = true return s }, }
NegativeInt64 is a Constraint that requires int64 < 0.
var NonEmptyString = codex.Constraint[string]{ Name: "non-empty", Check: func(v string) bool { return v != "" }, Message: func(v string) string { return "expected non-empty string" }, Schema: func(s schema.Schema) schema.Schema { s.MinLength = intptr(1) return s }, }
NonEmptyString is a Constraint that requires a non-empty string.
var NonNegativeDuration = codex.Constraint[time.Duration]{ Name: "nonNegative", Check: func(v time.Duration) bool { return v >= 0 }, Message: func(v time.Duration) string { return fmt.Sprintf("expected non-negative duration, got %s", v) }, }
NonNegativeDuration is a Constraint that requires time.Duration >= 0.
var NonZeroFloat = codex.Constraint[float64]{ Name: "nonzero", Check: func(v float64) bool { return v != 0 }, Message: func(v float64) string { return "expected non-zero number, got 0" }, }
NonZeroFloat is a Constraint that requires float64 != 0.
var NonZeroInt = codex.Constraint[int]{ Name: "nonzero", Check: func(v int) bool { return v != 0 }, Message: func(_ int) string { return "expected non-zero integer, got 0" }, }
NonZeroInt is a Constraint that requires int != 0.
var PositiveDuration = codex.Constraint[time.Duration]{ Name: "positive", Check: func(v time.Duration) bool { return v > 0 }, Message: func(v time.Duration) string { return fmt.Sprintf("expected positive duration, got %s", v) }, }
PositiveDuration is a Constraint that requires time.Duration > 0.
var PositiveFloat = codex.Constraint[float64]{ Name: "positive", Check: func(v float64) bool { return v > 0 }, Message: func(v float64) string { return fmt.Sprintf("expected positive number, got %g", v) }, Schema: func(s schema.Schema) schema.Schema { s.Minimum = float64ptr(0) s.ExclusiveMinimum = true return s }, }
PositiveFloat is a Constraint that requires float64 > 0.
var PositiveInt = codex.Constraint[int]{ Name: "positive", Check: func(v int) bool { return v > 0 }, Message: func(v int) string { return fmt.Sprintf("expected positive integer, got %d", v) }, Schema: func(s schema.Schema) schema.Schema { s.Minimum = float64ptr(0) s.ExclusiveMinimum = true return s }, }
PositiveInt is a Constraint that requires int > 0.
var PositiveInt32 = codex.Constraint[int32]{ Name: "positive", Check: func(v int32) bool { return v > 0 }, Message: func(v int32) string { return fmt.Sprintf("expected positive integer, got %d", v) }, Schema: func(s schema.Schema) schema.Schema { s.Minimum = float64ptr(0) s.ExclusiveMinimum = true return s }, }
PositiveInt32 is a Constraint that requires int32 > 0.
var PositiveInt64 = codex.Constraint[int64]{ Name: "positive", Check: func(v int64) bool { return v > 0 }, Message: func(v int64) string { return fmt.Sprintf("expected positive integer, got %d", v) }, Schema: func(s schema.Schema) schema.Schema { s.Minimum = float64ptr(0) s.ExclusiveMinimum = true return s }, }
PositiveInt64 is a Constraint that requires int64 > 0.
var PositiveUint = codex.Constraint[uint]{ Name: "positive", Check: func(v uint) bool { return v > 0 }, Message: func(v uint) string { return fmt.Sprintf("expected positive integer, got %d", v) }, Schema: func(s schema.Schema) schema.Schema { s.Minimum = float64ptr(0) s.ExclusiveMinimum = true return s }, }
PositiveUint is a Constraint that requires uint > 0.
var PositiveUint64 = codex.Constraint[uint64]{ Name: "positive", Check: func(v uint64) bool { return v > 0 }, Message: func(v uint64) string { return fmt.Sprintf("expected positive integer, got %d", v) }, Schema: func(s schema.Schema) schema.Schema { s.Minimum = float64ptr(0) s.ExclusiveMinimum = true return s }, }
PositiveUint64 is a Constraint that requires uint64 > 0.
var SemVer = codex.Constraint[string]{ Name: "semver", Check: func(v string) bool { return reSemVer.MatchString(v) }, Message: func(v string) string { return fmt.Sprintf("invalid semantic version (expected MAJOR.MINOR.PATCH): %q", v) }, Schema: func(s schema.Schema) schema.Schema { s.Pattern = reSemVer.String() return s }, }
SemVer is a Constraint that requires a semantic version string following semver.org spec, with an optional leading "v" prefix. Examples: "1.2.3", "v2.0.0-alpha+build.123".
var Slug = codex.Constraint[string]{ Name: "slug", Check: func(v string) bool { return reSlug.MatchString(v) }, Message: func(v string) string { return fmt.Sprintf("invalid slug (lowercase alphanumeric and hyphens only): %q", v) }, Schema: func(s schema.Schema) schema.Schema { s.Pattern = reSlug.String() return s }, }
Slug is a Constraint that requires a URL-friendly slug (lowercase alphanumeric and hyphens). Example valid slugs: "hello-world", "my-post-123".
var Time = codex.Constraint[string]{ Name: "time", Check: func(v string) bool { dummy := "2000-01-01T" + v _, err := time.Parse(time.RFC3339, dummy) if err == nil { return true } _, err = time.Parse(time.RFC3339Nano, dummy) return err == nil }, Message: func(v string) string { return fmt.Sprintf("invalid time (expected HH:MM:SS[.frac]Z or ±offset): %q", v) }, Schema: withFormat("time"), }
Time is a Constraint that requires an RFC 3339 full-time string (e.g. "10:30:00Z", "10:30:00.5+02:00").
var URI = codex.Constraint[string]{ Name: "uri", Check: func(v string) bool { u, err := url.ParseRequestURI(v) return err == nil && u.Scheme != "" && u.Host != "" }, Message: func(v string) string { return fmt.Sprintf("invalid URI: %q", v) }, Schema: withFormat("uri"), }
URI is a Constraint that requires a valid absolute URI with any scheme. Use this for non-HTTP URIs such as grpc://, ws://, or custom schemes.
var URL = URLWithSchemes("http", "https")
URL is a Constraint that requires a valid absolute URL with http or https scheme.
var UUID = codex.Constraint[string]{ Name: "uuid", Check: func(v string) bool { return reUUID.MatchString(v) }, Message: func(v string) string { return fmt.Sprintf("invalid UUID: %q", v) }, Schema: withFormat("uuid"), }
UUID is a Constraint that requires a valid UUID (any version, RFC 4122 format).
Functions ¶
func MaxBytes ¶
func MaxBytes(n int) codex.Constraint[[]byte]
MaxBytes returns a Constraint that requires a byte slice of at most n bytes. The check applies to the decoded byte count, not the base64-encoded string length.
func MaxDuration ¶ added in v0.3.0
MaxDuration returns a Constraint that requires time.Duration <= d.
func MaxFloat ¶
func MaxFloat(n float64) codex.Constraint[float64]
MaxFloat returns a Constraint that requires float64 <= n.
func MaxInt ¶
func MaxInt(n int) codex.Constraint[int]
MaxInt returns a Constraint that requires int <= n.
func MaxInt32 ¶ added in v0.3.0
func MaxInt32(n int32) codex.Constraint[int32]
MaxInt32 returns a Constraint that requires int32 <= n.
func MaxInt64 ¶ added in v0.3.0
func MaxInt64(n int64) codex.Constraint[int64]
MaxInt64 returns a Constraint that requires int64 <= n.
func MaxLen ¶
func MaxLen(n int) codex.Constraint[string]
MaxLen returns a Constraint that requires a string of at most n characters.
func MaxUint ¶ added in v0.3.0
func MaxUint(n uint) codex.Constraint[uint]
MaxUint returns a Constraint that requires uint <= n.
func MaxUint64 ¶ added in v0.3.0
func MaxUint64(n uint64) codex.Constraint[uint64]
MaxUint64 returns a Constraint that requires uint64 <= n.
func MinBytes ¶
func MinBytes(n int) codex.Constraint[[]byte]
MinBytes returns a Constraint that requires a byte slice of at least n bytes. The check applies to the decoded byte count, not the base64-encoded string length.
func MinDuration ¶ added in v0.3.0
MinDuration returns a Constraint that requires time.Duration >= d.
func MinFloat ¶
func MinFloat(n float64) codex.Constraint[float64]
MinFloat returns a Constraint that requires float64 >= n.
func MinInt ¶
func MinInt(n int) codex.Constraint[int]
MinInt returns a Constraint that requires int >= n.
func MinInt32 ¶ added in v0.3.0
func MinInt32(n int32) codex.Constraint[int32]
MinInt32 returns a Constraint that requires int32 >= n.
func MinInt64 ¶ added in v0.3.0
func MinInt64(n int64) codex.Constraint[int64]
MinInt64 returns a Constraint that requires int64 >= n.
func MinLen ¶
func MinLen(n int) codex.Constraint[string]
MinLen returns a Constraint that requires a string of at least n characters.
func MinUint ¶ added in v0.3.0
func MinUint(n uint) codex.Constraint[uint]
MinUint returns a Constraint that requires uint >= n.
func MinUint64 ¶ added in v0.3.0
func MinUint64(n uint64) codex.Constraint[uint64]
MinUint64 returns a Constraint that requires uint64 >= n.
func OneOf ¶
func OneOf(values ...string) codex.Constraint[string]
OneOf returns a Constraint that requires the string to be one of the given values.
func Pattern ¶
func Pattern(re *regexp.Regexp) codex.Constraint[string]
Pattern returns a Constraint that requires the string to match the given regular expression. The caller is responsible for compiling the regexp (use regexp.MustCompile for literals).
func RangeFloat ¶
func RangeFloat(min, max float64) codex.Constraint[float64]
RangeFloat returns a Constraint that requires min <= float64 <= max.
func RangeInt ¶
func RangeInt(min, max int) codex.Constraint[int]
RangeInt returns a Constraint that requires min <= int <= max.
func RangeInt32 ¶ added in v0.3.0
func RangeInt32(min, max int32) codex.Constraint[int32]
RangeInt32 returns a Constraint that requires min <= int32 <= max.
func RangeInt64 ¶ added in v0.3.0
func RangeInt64(min, max int64) codex.Constraint[int64]
RangeInt64 returns a Constraint that requires min <= int64 <= max.
func RangeUint ¶ added in v0.3.0
func RangeUint(min, max uint) codex.Constraint[uint]
RangeUint returns a Constraint that requires min <= uint <= max.
func RangeUint64 ¶ added in v0.3.0
func RangeUint64(min, max uint64) codex.Constraint[uint64]
RangeUint64 returns a Constraint that requires min <= uint64 <= max.
func URLWithSchemes ¶ added in v0.3.0
func URLWithSchemes(schemes ...string) codex.Constraint[string]
URLWithSchemes returns a Constraint that requires a valid absolute URL whose scheme is one of the provided values. The host must be non-empty. Schema annotation uses JSON Schema format "uri".
Types ¶
This section is empty.