forked from alecthomas/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 3
/
subschemas_conditional.go
59 lines (49 loc) · 1.88 KB
/
subschemas_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
package jsonschema
import "reflect"
// File and methods named in respect to https://json-schema.org/latest/json-schema-validation.html#rfc.section.6.6
var ifThenElseType = reflect.TypeOf((*ifThenElse)(nil)).Elem()
// Implement IfThenElse() when condition needs to be used
// {
// "if": { "properties": { "power": { "minimum": 9000 } } },
// "then": { "required": [ "disbelief" ] },
// "else": { "required": [ "confidence" ] }
// }
type ifThenElse interface {
IfThenElse() SchemaCondition
}
// SchemaCondition holds data for if/then/else jsonschema statements
//
// If: A reflect.StructField that defines the condition to be met.
// Then: A type that will be converted to a jsonschema subschema and evaluated if the condition is met
// Else: A type that will be converted to a jsonschema subschema and evaluated if the condition is not met
type SchemaCondition struct {
If reflect.StructField
Then interface{}
Else interface{}
}
// Append jsonschema rules from IfThenElse interface to the jsonschema for the struct that implements them
func (r *Reflector) addSubschemasForConditionalCases(schema *Type, definitions Definitions, t reflect.Type) {
if schema == nil {
return
}
t, nonNilPointer := getNonNilPointerTypeAndInterface(t)
if t.Implements(ifThenElseType) {
condition := nonNilPointer.(ifThenElse).IfThenElse()
r.reflectCondition(definitions, condition, schema)
}
}
func (r *Reflector) reflectCondition(definitions Definitions, sc SchemaCondition, t *Type) {
conditionSchema := Type{}
conditionSchema.structKeywordsFromTags(r.getJSONSchemaTags(sc.If, nil))
t.If = &Type{
Properties: map[string]*Type{
sc.If.Tag.Get("json"): &conditionSchema,
},
}
if reflect.TypeOf(sc.Then) != nil {
t.Then = r.reflectTypeToSchema(definitions, reflect.TypeOf(sc.Then))
}
if reflect.TypeOf(sc.Else) != nil {
t.Else = r.reflectTypeToSchema(definitions, reflect.TypeOf(sc.Else))
}
}