-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatterns.go
More file actions
85 lines (70 loc) · 2.66 KB
/
Copy pathpatterns.go
File metadata and controls
85 lines (70 loc) · 2.66 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package schema
import "github.com/lestrrat-go/json-schema/keywords"
// Common Pattern Helpers
//
// This file provides convenient one-liners for frequent JSON Schema validation patterns.
// These functions create pre-configured Builder instances for common use cases.
// Email creates a Builder for email validation using the "email" format
func Email() *Builder {
return NewBuilder().Types(StringType).Format(keywords.FormatEmail)
}
// URI creates a Builder for URI validation using the "uri" format
func URI() *Builder {
return NewBuilder().Types(StringType).Format(keywords.FormatURI)
}
// NonEmptyString creates a Builder for non-empty string validation
func NonEmptyString() *Builder {
return NewBuilder().Types(StringType).MinLength(1)
}
// PositiveNumber creates a Builder for positive number validation (>= 0)
func PositiveNumber() *Builder {
return NewBuilder().Types(NumberType).Minimum(0)
}
// PositiveInteger creates a Builder for positive integer validation (>= 0)
func PositiveInteger() *Builder {
return NewBuilder().Types(IntegerType).Minimum(0)
}
// Enum creates a Builder for enum validation with the given values
func Enum(values ...any) *Builder {
return NewBuilder().Enum(values...)
}
// OneOf creates a Builder for oneOf validation with the given schemas
func OneOf(schemas ...*Schema) *Builder {
schemaOrBools := make([]SchemaOrBool, len(schemas))
for i, s := range schemas {
schemaOrBools[i] = s
}
return NewBuilder().OneOf(schemaOrBools...)
}
// AnyOf creates a Builder for anyOf validation with the given schemas
func AnyOf(schemas ...*Schema) *Builder {
schemaOrBools := make([]SchemaOrBool, len(schemas))
for i, s := range schemas {
schemaOrBools[i] = s
}
return NewBuilder().AnyOf(schemaOrBools...)
}
// AllOf creates a Builder for allOf validation with the given schemas
func AllOf(schemas ...*Schema) *Builder {
schemaOrBools := make([]SchemaOrBool, len(schemas))
for i, s := range schemas {
schemaOrBools[i] = s
}
return NewBuilder().AllOf(schemaOrBools...)
}
// Optional creates a Builder that allows either the given schema or null
func Optional(s *Schema) *Builder {
return NewBuilder().AnyOf(s, NewBuilder().Types(NullType).MustBuild())
}
// UUID creates a Builder for UUID validation using the "uuid" format
func UUID() *Builder {
return NewBuilder().Types(StringType).Format(keywords.FormatUUID)
}
// Date creates a Builder for date validation using the "date" format
func Date() *Builder {
return NewBuilder().Types(StringType).Format(keywords.FormatDate)
}
// DateTime creates a Builder for date-time validation using the "date-time" format
func DateTime() *Builder {
return NewBuilder().Types(StringType).Format(keywords.FormatDateTime)
}