forked from qri-io/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeywords_conditionals.go
142 lines (120 loc) · 4.39 KB
/
keywords_conditionals.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package jsonschema
import (
"encoding/json"
)
// If MUST be a valid JSON Schema.
// Instances that successfully validate against this keyword's subschema MUST also be valid against the subschema value of the "Then" keyword, if present.
// Instances that fail to validate against this keyword's subschema MUST also be valid against the subschema value of the "Elsee" keyword.
// Validation of the instance against this keyword on its own always succeeds, regardless of the validation outcome of against its subschema.
type If struct {
Schema Schema
Then *Then
Else *Else
}
// NewIf allocates a new If validator
func NewIf() Validator {
return &If{}
}
// Validate implements the Validator interface for If
func (i *If) Validate(propPath string, data interface{}, errs *[]ValError) {
test := &[]ValError{}
i.Schema.Validate(propPath, data, test)
if len(*test) == 0 {
if i.Then != nil {
s := Schema(*i.Then)
sch := &s
sch.Validate(propPath, data, errs)
return
}
} else {
if i.Else != nil {
s := Schema(*i.Else)
sch := &s
sch.Validate(propPath, data, errs)
return
}
}
}
// JSONProp implements JSON property name indexing for If
func (i If) JSONProp(name string) interface{} {
return Schema(i.Schema).JSONProp(name)
}
// JSONChildren implements the JSONContainer interface for If
func (i If) JSONChildren() (res map[string]JSONPather) {
return i.Schema.JSONChildren()
}
// UnmarshalJSON implements the json.Unmarshaler interface for If
func (i *If) UnmarshalJSON(data []byte) error {
var sch Schema
if err := json.Unmarshal(data, &sch); err != nil {
return err
}
*i = If{Schema: sch}
return nil
}
// MarshalJSON implements json.Marshaler for If
func (i If) MarshalJSON() ([]byte, error) {
return json.Marshal(i.Schema)
}
// Then MUST be a valid JSON Schema.
// When present alongside of "if", the instance successfully validates against this keyword if it validates against both the "if"'s subschema and this keyword's subschema.
// When "if" is absent, or the instance fails to validate against its subschema, validation against this keyword always succeeds. Implementations SHOULD avoid attempting to validate against the subschema in these cases.
type Then Schema
// NewThen allocates a new Then validator
func NewThen() Validator {
return &Then{}
}
// Validate implements the Validator interface for Then
func (t *Then) Validate(propPath string, data interface{}, errs *[]ValError) {}
// JSONProp implements JSON property name indexing for Then
func (t Then) JSONProp(name string) interface{} {
return Schema(t).JSONProp(name)
}
// JSONChildren implements the JSONContainer interface for If
func (t Then) JSONChildren() (res map[string]JSONPather) {
return Schema(t).JSONChildren()
}
// UnmarshalJSON implements the json.Unmarshaler interface for Then
func (t *Then) UnmarshalJSON(data []byte) error {
var sch Schema
if err := json.Unmarshal(data, &sch); err != nil {
return err
}
*t = Then(sch)
return nil
}
// MarshalJSON implements json.Marshaler for Then
func (t Then) MarshalJSON() ([]byte, error) {
return json.Marshal(Schema(t))
}
// Else MUST be a valid JSON Schema.
// When present alongside of "if", the instance successfully validates against this keyword if it fails to validate against the "if"'s subschema, and successfully validates against this keyword's subschema.
// When "if" is absent, or the instance successfully validates against its subschema, validation against this keyword always succeeds. Implementations SHOULD avoid attempting to validate against the subschema in these cases.
type Else Schema
// NewElse allocates a new Else validator
func NewElse() Validator {
return &Else{}
}
// Validate implements the Validator interface for Else
func (e *Else) Validate(propPath string, data interface{}, err *[]ValError) {}
// JSONProp implements JSON property name indexing for Else
func (e Else) JSONProp(name string) interface{} {
return Schema(e).JSONProp(name)
}
// JSONChildren implements the JSONContainer interface for Else
func (e Else) JSONChildren() (res map[string]JSONPather) {
return Schema(e).JSONChildren()
}
// UnmarshalJSON implements the json.Unmarshaler interface for Else
func (e *Else) UnmarshalJSON(data []byte) error {
var sch Schema
if err := json.Unmarshal(data, &sch); err != nil {
return err
}
*e = Else(sch)
return nil
}
// MarshalJSON implements json.Marshaler for Else
func (e Else) MarshalJSON() ([]byte, error) {
return json.Marshal(Schema(e))
}