-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathany.go
More file actions
39 lines (32 loc) · 776 Bytes
/
any.go
File metadata and controls
39 lines (32 loc) · 776 Bytes
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
package check
// AnySchema accepts any value.
type AnySchema struct {
optional bool
}
func NewAny() *AnySchema {
return &AnySchema{}
}
// clone returns a copy of this schema to preserve immutability in chains.
func (s *AnySchema) clone() *AnySchema {
copy := *s
return ©
}
func (s *AnySchema) Optional() *AnySchema {
s = s.clone()
s.optional = true
return s
}
func (s *AnySchema) IsOptional() bool { return s.optional }
func (s *AnySchema) Parse(value any) (any, error) {
errs := s.Validate(value)
if len(errs) > 0 {
return nil, ValidationErrors(errs)
}
return value, nil
}
func (s *AnySchema) Validate(value any) []ValidationError {
if value == nil && !s.optional {
return []ValidationError{{Message: "required value is missing"}}
}
return nil
}