...
1 package jsonschema
2
3 import (
4 "context"
5 "encoding/json"
6
7 jptr "github.com/qri-io/jsonpointer"
8 )
9
10
11 type If Schema
12
13
14 func NewIf() Keyword {
15 return &If{}
16 }
17
18
19 func (f *If) Register(uri string, registry *SchemaRegistry) {
20 (*Schema)(f).Register(uri, registry)
21 }
22
23
24 func (f *If) Resolve(pointer jptr.Pointer, uri string) *Schema {
25 return nil
26 }
27
28
29 func (f *If) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
30 schemaDebug("[If] Validating")
31 thenKW := currentState.Local.keywords["then"]
32 elseKW := currentState.Local.keywords["else"]
33
34 if thenKW == nil && elseKW == nil {
35
36 schemaDebug("[If] Aborting validation as no then or else is present")
37 return
38 }
39
40 subState := currentState.NewSubState()
41 subState.ClearState()
42 subState.DescendBase("if")
43 subState.DescendRelative("if")
44
45 subState.Errs = &[]KeyError{}
46 sch := Schema(*f)
47 sch.ValidateKeyword(ctx, subState, data)
48
49 currentState.Misc["ifResult"] = subState.IsValid()
50 }
51
52
53 func (f If) JSONProp(name string) interface{} {
54 return Schema(f).JSONProp(name)
55 }
56
57
58 func (f If) JSONChildren() (res map[string]JSONPather) {
59 return Schema(f).JSONChildren()
60 }
61
62
63 func (f *If) UnmarshalJSON(data []byte) error {
64 var sch Schema
65 if err := json.Unmarshal(data, &sch); err != nil {
66 return err
67 }
68 *f = If(sch)
69 return nil
70 }
71
72
73 func (f If) MarshalJSON() ([]byte, error) {
74 return json.Marshal(Schema(f))
75 }
76
77
78 type Then Schema
79
80
81 func NewThen() Keyword {
82 return &Then{}
83 }
84
85
86 func (t *Then) Register(uri string, registry *SchemaRegistry) {
87 (*Schema)(t).Register(uri, registry)
88 }
89
90
91 func (t *Then) Resolve(pointer jptr.Pointer, uri string) *Schema {
92 return (*Schema)(t).Resolve(pointer, uri)
93 }
94
95
96 func (t *Then) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
97 schemaDebug("[Then] Validating")
98 ifResult, okIf := currentState.Misc["ifResult"]
99 if !okIf {
100 schemaDebug("[Then] If result not found, skipping")
101
102 return
103 }
104 if !(ifResult.(bool)) {
105 schemaDebug("[Then] If result is false, skipping")
106
107 return
108 }
109
110 subState := currentState.NewSubState()
111 subState.DescendBase("then")
112 subState.DescendRelative("then")
113
114 sch := Schema(*t)
115 sch.ValidateKeyword(ctx, subState, data)
116 currentState.UpdateEvaluatedPropsAndItems(subState)
117 }
118
119
120 func (t Then) JSONProp(name string) interface{} {
121 return Schema(t).JSONProp(name)
122 }
123
124
125 func (t Then) JSONChildren() (res map[string]JSONPather) {
126 return Schema(t).JSONChildren()
127 }
128
129
130 func (t *Then) UnmarshalJSON(data []byte) error {
131 var sch Schema
132 if err := json.Unmarshal(data, &sch); err != nil {
133 return err
134 }
135 *t = Then(sch)
136 return nil
137 }
138
139
140 func (t Then) MarshalJSON() ([]byte, error) {
141 return json.Marshal(Schema(t))
142 }
143
144
145 type Else Schema
146
147
148 func NewElse() Keyword {
149 return &Else{}
150 }
151
152
153 func (e *Else) Register(uri string, registry *SchemaRegistry) {
154 (*Schema)(e).Register(uri, registry)
155 }
156
157
158 func (e *Else) Resolve(pointer jptr.Pointer, uri string) *Schema {
159 return (*Schema)(e).Resolve(pointer, uri)
160 }
161
162
163 func (e *Else) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
164 schemaDebug("[Else] Validating")
165 ifResult, okIf := currentState.Misc["ifResult"]
166 if !okIf {
167
168 return
169 }
170 if ifResult.(bool) {
171
172 return
173 }
174
175 subState := currentState.NewSubState()
176 subState.DescendBase("else")
177 subState.DescendRelative("else")
178
179 sch := Schema(*e)
180 sch.ValidateKeyword(ctx, subState, data)
181 }
182
183
184 func (e Else) JSONProp(name string) interface{} {
185 return Schema(e).JSONProp(name)
186 }
187
188
189 func (e Else) JSONChildren() (res map[string]JSONPather) {
190 return Schema(e).JSONChildren()
191 }
192
193
194 func (e *Else) UnmarshalJSON(data []byte) error {
195 var sch Schema
196 if err := json.Unmarshal(data, &sch); err != nil {
197 return err
198 }
199 *e = Else(sch)
200 return nil
201 }
202
203
204 func (e Else) MarshalJSON() ([]byte, error) {
205 return json.Marshal(Schema(e))
206 }
207
View as plain text