forked from getkin/kin-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_formats.go
33 lines (26 loc) · 947 Bytes
/
schema_formats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package openapi3
import (
"fmt"
"regexp"
)
var SchemaStringFormats = make(map[string]*regexp.Regexp, 8)
func DefineStringFormat(name string, pattern string) {
re, err := regexp.Compile(pattern)
if err != nil {
err := fmt.Errorf("Format '%v' has invalid pattern '%v': %v", name, pattern, err)
panic(err)
}
SchemaStringFormats[name] = re
}
func init() {
// This pattern catches only some suspiciously wrong-looking email addresses.
// Use DefineStringFormat(...) if you need something stricter.
DefineStringFormat("email", `^[^@]+@[^@<>",\w]+$`)
// Base64
// The pattern supports base64 and b./ase64url. Padding ('=') is supported.
DefineStringFormat("byte", `(^$|^[a-zA-Z0-9+/\-_]*=*$)`)
// date
DefineStringFormat("date", `^[0-9]{4}-(0[0-9]|11|12)-([0-2][0-9]|30|31)$`)
// date-time
DefineStringFormat("date-time", `^[0-9]{4}-(0[0-9]|11|12)-([0-2][0-9]|30|31)T[0-9]{2}:[0-9]{2}:[0-9]{2}(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`)
}