-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathreference.go
42 lines (34 loc) · 1012 Bytes
/
reference.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
34
35
36
37
38
39
40
41
42
package schema
import (
"errors"
"fmt"
)
// Reference validates the ID of a linked resource.
type Reference struct {
Path string
validator FieldValidator
SchemaValidator Validator
}
// Compile validates v.Path against rc and stores the a FieldValidator for later use by v.Validate.
func (r *Reference) Compile(rc ReferenceChecker) error {
if rc == nil {
return fmt.Errorf("rc can not be nil")
}
if v, sv := rc.ReferenceChecker(r.Path); v != nil && sv != nil {
r.validator = v
r.SchemaValidator = sv
return nil
}
return fmt.Errorf("can't find resource '%s'", r.Path)
}
// Validate validates and sanitizes IDs against the reference path.
func (r Reference) Validate(value interface{}) (interface{}, error) {
if r.validator == nil {
return nil, errors.New("not successfully compiled")
}
return r.validator.Validate(value)
}
// GetField implements the FieldGetter interface.
func (r Reference) GetField(name string) *Field {
return r.SchemaValidator.GetField(name)
}