forked from qri-io/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keywords_conditional.go
206 lines (169 loc) · 5.24 KB
/
keywords_conditional.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package jsonschema
import (
"context"
"encoding/json"
jptr "github.com/qri-io/jsonpointer"
)
// If defines the if JSON Schema keyword
type If Schema
// NewIf allocates a new If keyword
func NewIf() Keyword {
return &If{}
}
// Register implements the Keyword interface for If
func (f *If) Register(uri string, registry *SchemaRegistry) {
(*Schema)(f).Register(uri, registry)
}
// Resolve implements the Keyword interface for If
func (f *If) Resolve(pointer jptr.Pointer, uri string) *Schema {
return nil
}
// ValidateKeyword implements the Keyword interface for If
func (f *If) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[If] Validating")
thenKW := currentState.Local.keywords["then"]
elseKW := currentState.Local.keywords["else"]
if thenKW == nil && elseKW == nil {
// no then or else for if, aborting validation
schemaDebug("[If] Aborting validation as no then or else is present")
return
}
subState := currentState.NewSubState()
subState.ClearState()
subState.DescendBase("if")
subState.DescendRelative("if")
subState.Errs = &[]KeyError{}
sch := Schema(*f)
sch.ValidateKeyword(ctx, subState, data)
currentState.Misc["ifResult"] = subState.IsValid()
}
// JSONProp implements the JSONPather for If
func (f If) JSONProp(name string) interface{} {
return Schema(f).JSONProp(name)
}
// JSONChildren implements the JSONContainer interface for If
func (f If) JSONChildren() (res map[string]JSONPather) {
return Schema(f).JSONChildren()
}
// UnmarshalJSON implements the json.Unmarshaler interface for If
func (f *If) UnmarshalJSON(data []byte) error {
var sch Schema
if err := json.Unmarshal(data, &sch); err != nil {
return err
}
*f = If(sch)
return nil
}
// MarshalJSON implements the json.Marshaler interface for If
func (f If) MarshalJSON() ([]byte, error) {
return json.Marshal(Schema(f))
}
// Then defines the then JSON Schema keyword
type Then Schema
// NewThen allocates a new Then keyword
func NewThen() Keyword {
return &Then{}
}
// Register implements the Keyword interface for Then
func (t *Then) Register(uri string, registry *SchemaRegistry) {
(*Schema)(t).Register(uri, registry)
}
// Resolve implements the Keyword interface for Then
func (t *Then) Resolve(pointer jptr.Pointer, uri string) *Schema {
return (*Schema)(t).Resolve(pointer, uri)
}
// ValidateKeyword implements the Keyword interface for Then
func (t *Then) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[Then] Validating")
ifResult, okIf := currentState.Misc["ifResult"]
if !okIf {
schemaDebug("[Then] If result not found, skipping")
// if not found
return
}
if !(ifResult.(bool)) {
schemaDebug("[Then] If result is false, skipping")
// if was false
return
}
subState := currentState.NewSubState()
subState.DescendBase("then")
subState.DescendRelative("then")
sch := Schema(*t)
sch.ValidateKeyword(ctx, subState, data)
currentState.UpdateEvaluatedPropsAndItems(subState)
}
// JSONProp implements the JSONPather for Then
func (t Then) JSONProp(name string) interface{} {
return Schema(t).JSONProp(name)
}
// JSONChildren implements the JSONContainer interface for Then
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 the json.Marshaler interface for Then
func (t Then) MarshalJSON() ([]byte, error) {
return json.Marshal(Schema(t))
}
// Else defines the else JSON Schema keyword
type Else Schema
// NewElse allocates a new Else keyword
func NewElse() Keyword {
return &Else{}
}
// Register implements the Keyword interface for Else
func (e *Else) Register(uri string, registry *SchemaRegistry) {
(*Schema)(e).Register(uri, registry)
}
// Resolve implements the Keyword interface for Else
func (e *Else) Resolve(pointer jptr.Pointer, uri string) *Schema {
return (*Schema)(e).Resolve(pointer, uri)
}
// ValidateKeyword implements the Keyword interface for Else
func (e *Else) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[Else] Validating")
ifResult, okIf := currentState.Misc["ifResult"]
if !okIf {
// if not found
return
}
if ifResult.(bool) {
// if was true
return
}
subState := currentState.NewSubState()
subState.DescendBase("else")
subState.DescendRelative("else")
sch := Schema(*e)
sch.ValidateKeyword(ctx, subState, data)
}
// JSONProp implements the JSONPather 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 the json.Marshaler interface for Else
func (e Else) MarshalJSON() ([]byte, error) {
return json.Marshal(Schema(e))
}