validate

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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).

View Source
var DateTime = codex.Constraint[string]{
	Name: "date-time",
	Check: func(v string) bool {
		_, err := time.Parse(time.RFC3339, 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.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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".

View Source
var URL = codex.Constraint[string]{
	Name: "url",
	Check: func(v string) bool {
		u, err := url.ParseRequestURI(v)
		if err != nil {
			return false
		}
		return (u.Scheme == "http" || u.Scheme == "https") && u.Host != ""
	},
	Message: func(v string) string { return fmt.Sprintf("invalid URL: %q", v) },
	Schema:  withFormat("uri"),
}

URL is a Constraint that requires a valid absolute URL with http or https scheme.

View Source
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 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 MaxLen

func MaxLen(n int) codex.Constraint[string]

MaxLen returns a Constraint that requires a string of at most n characters.

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 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 MinLen

func MinLen(n int) codex.Constraint[string]

MinLen returns a Constraint that requires a string of at least n characters.

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.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL