Skip to content

Feature request - DateTime Validation #3

@Kosiare

Description

@Kosiare

I’d like to request support for validating date and time formats within structs. Currently it looks that validation for fields like time.Time is limited and it would be beneficial to have built-in rules for handling different date and time types.

Proposed Features:

  • datetime validation – Ensures the field follows a valid datetime format (e.g., 2006-01-02 15:04:05).
  • date – Accepts only date values without time (e.g., 2006-01-02).
  • time – Accepts only time values (e.g., 15:04:05).
  • today – Ensures the value represents today’s date.
  • before:<date> and after:<date> – Validates that a date/time value falls before or after a specified date.
  • before_now and after_now – Checks if the given date/time is before or after the current timestamp.
  • timezone – Ensures the value has a valid timezone (e.g., UTC, America/New_York).

Example usage:

type Event struct {
	StartTime string `validation:"req datetime"`               // Must be a valid datetime
	EndDate   string `validation:"req date before:2025-01-01"` // Must be a date before 2025-01-01
	TimeOnly  string `validation:"req time"`                   // Must be a valid time
	CreatedAt string `validation:"req datetime before_now"`    // Must be before the current timestamp
}

s := &Event{
	StartTime: "2024-02-18 14:30:00",
	EndDate:   "2024-12-31",
	TimeOnly:  "12:45:30",
	CreatedAt: "2023-05-10 08:00:00",
}

o := &structvalidator.ValidationOptions{
	RestrictFields: map[string]bool{
		"StartTime": true,
		"EndDate":   true,
		"TimeOnly":  true,
		"CreatedAt": true,
	},
}

isValid, fieldsWithInvalidValue := structvalidator.Validate(s, o)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions