-
Notifications
You must be signed in to change notification settings - Fork 6
/
jsval.go
59 lines (50 loc) · 1.79 KB
/
jsval.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//go:generate go run internal/cmd/gentest/gentest.go schema.json generated_validator_test.go
//go:generate go run internal/cmd/genmaybe/genmaybe.go
// Package jsval implements an input validator, based on JSON Schema.
// The main purpose is to validate JSON Schemas (see
// https://github.com/lestrrat-go/jsschema), and to automatically
// generate validators from schemas, but jsval can be used independently
// of JSON Schema.
package jsval
import "github.com/pkg/errors"
// New creates a new JSVal instance.
func New() *JSVal {
return &JSVal{
ConstraintMap: &ConstraintMap{},
}
}
// Validate validates the input, and return an error
// if any of the validations fail
func (v *JSVal) Validate(x interface{}) error {
name := v.Name
if len(name) == 0 {
return errors.Wrapf(v.root.Validate(x), "validator %p failed", v)
}
return errors.Wrapf(v.root.Validate(x), "validator %s failed", name)
}
// SetName sets the name for the validator
func (v *JSVal) SetName(s string) *JSVal {
v.Name = s
return v
}
// SetRoot sets the root Constraint object.
func (v *JSVal) SetRoot(c Constraint) *JSVal {
v.root = c
return v
}
// Root returns the root Constraint object.
func (v *JSVal) Root() Constraint {
return v.root
}
// SetConstraintMap allows you to set the map that is referred to
// when resolving JSON references within constraints. By setting
// this to a common map, for example, you can share the same references
// to save you some memory space and sanity. See an example in the
// `generated_validator_test.go` file.
func (v *JSVal) SetConstraintMap(cm *ConstraintMap) *JSVal {
v.ConstraintMap = cm
return v
}
func (p JSValSlice) Len() int { return len(p) }
func (p JSValSlice) Less(i, j int) bool { return p[i].Name < p[j].Name }
func (p JSValSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }