ZDDC/zddc/internal/jsonschema/format.go
2026-06-11 13:32:31 -05:00

29 lines
680 B
Go

package jsonschema
import (
"regexp"
"time"
)
var (
dateRe = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)
emailRe = regexp.MustCompile(`^[^\s@]+@[^\s@]+\.[^\s@]+$`)
)
// formatValid checks whether s satisfies the named format. Returns true for
// formats we don't support — JSON Schema treats `format` as an annotation by
// default, so unknown formats are non-failing.
func formatValid(format, s string) bool {
switch format {
case "date":
if !dateRe.MatchString(s) {
return false
}
// Reject obviously-malformed dates like 2026-13-40.
_, err := time.Parse("2006-01-02", s)
return err == nil
case "email":
return emailRe.MatchString(s)
}
return true
}