-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.DatetimeYearsOld
marrow16 edited this page Jan 21, 2023
·
5 revisions
Check that a date (datetime represented as string or time.Time) meets the specified minimum and/or maximum years-old. Can also be used to simply check a minimum age or maximum age
Notes:
- If the value being checked contains a time (hh:mm:ss), it is ignored (very few people know, or are expected to specify, their exact time of birth)
- If the value being checked is in the future - this constraint fails
- If both
MinimumandMaximumare set to zero (or less) then no check is performed - This constraint is strict - if the property value is not a valid ISO date/datetime then this constraint fails
age
| Field | Type | Description |
|---|---|---|
Minimum |
int | is the minimum age (not checked if this value is zero or less) |
Maximum |
int | is the maximum age (not checked if this value is zero or less) |
ExclusiveMin |
bool | if set to true, specifies the minimum value is exclusive |
ExclusiveMax |
bool | if set to true, specifies the maximum value is exclusive |
ThisYear |
bool | if set to true, only checks the minimum/maximum age against the current year - i.e. the current age is calculated based on 23:59:59.999999999 at 31st December of the current year |
ThresholdDate |
string | is an optional string representing a threshold date at which the age is calculated If this is specified, the year part is ignored (the current year is always used) Note: if specified, this also overrides the ThisYear flag |
LeapdayAdjust |
bool | if set, adjusts the way leapday birthdays are age calculated By default, leapday birthdays are taken as 1st March when the current year is not a leap year Setting LeapdayAdjust to true means that leapday birthdays are taken as 28th Feb |
Message |
string | the violation message to be used if the constraint fails. If empty, the default message is used |
Stop |
bool | when set to true, Stop prevents further validation checks on the property if this constraint fails |
Programmatic example...
package main
import (
"fmt"
"time"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
Properties: valix.Properties{
"dateOfBirth": {
Type: valix.JsonString,
Constraints: valix.Constraints{
&valix.DatetimeYearsOld{
Minimum: 18,
Maximum: 30,
ThisYear: true,
Message: "You must be between 18 and 30 to go on a Club 18-30 holiday!",
},
},
},
},
}
now := time.Now()
years := time.Hour * 24 * 365
tenYearsAgo := now.Add(0 - (10 * years))
ok, violations, _ := validator.ValidateString(fmt.Sprintf(`{"dateOfBirth": "%s"}`, tenYearsAgo.Format("2006-01-02")))
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
fortyYearsAgo := now.Add(0 - (40 * years))
ok, violations, _ = validator.ValidateString(fmt.Sprintf(`{"dateOfBirth": "%s"}`, fortyYearsAgo.Format("2006-01-02")))
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
twentyYearsAgo := now.Add(0 - (20 * years))
ok, violations, _ = validator.ValidateString(fmt.Sprintf(`{"dateOfBirth": "%s"}`, twentyYearsAgo.Format("2006-01-02")))
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
}Struct v8n tag example...
package main
import (
"fmt"
"time"
"github.com/marrow16/valix"
)
type MyStruct struct {
DateOfBirth string `json:"dateOfBirth" v8n:"&age{min: 18, max: 30, ThisYear: true, msg: 'You must be between 18 and 30 to go on a Club 18-30 holiday!'}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
now := time.Now()
years := time.Hour * 24 * 365
tenYearsAgo := now.Add(0 - (10 * years))
ok, violations, _ := validator.ValidateString(fmt.Sprintf(`{"dateOfBirth": "%s"}`, tenYearsAgo.Format("2006-01-02")))
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
fortyYearsAgo := now.Add(0 - (40 * years))
ok, violations, _ = validator.ValidateString(fmt.Sprintf(`{"dateOfBirth": "%s"}`, fortyYearsAgo.Format("2006-01-02")))
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
twentyYearsAgo := now.Add(0 - (20 * years))
ok, violations, _ = validator.ValidateString(fmt.Sprintf(`{"dateOfBirth": "%s"}`, twentyYearsAgo.Format("2006-01-02")))
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
}