-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathexample_vocab_discriminator_test.go
170 lines (157 loc) · 3.23 KB
/
example_vocab_discriminator_test.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
package jsonschema_test
import (
"fmt"
"log"
"strings"
"github.com/santhosh-tekuri/jsonschema/v6"
)
// SchemaExt --
type discriminator struct {
pname string
values map[string]*jsonschema.Schema
}
func (d *discriminator) Validate(ctx *jsonschema.ValidatorContext, v any) {
obj, ok := v.(map[string]any)
if !ok {
return
}
pvalue, ok := obj[d.pname]
if !ok {
return
}
value, ok := pvalue.(string)
if !ok {
return
}
sch := d.values[value]
if sch == nil {
return
}
if err := ctx.Validate(sch, v, nil); err != nil {
ctx.AddErr(err)
} else {
ctx.EvaluatedProp(d.pname)
}
}
// Vocab --
func discriminatorVocab() *jsonschema.Vocabulary {
url := "http://example.com/meta/discriminator"
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(`{
"properties" : {
"discriminator": {
"type": "object",
"minProperties": 1,
"maxProperties": 1,
"patternProperties": {
".*": {
"type": "object",
"patternProperties": {
".*": {
"$ref": "https://json-schema.org/draft/2020-12/schema"
}
}
}
}
}
}
}`))
if err != nil {
log.Fatal(err)
}
c := jsonschema.NewCompiler()
if err := c.AddResource(url, schema); err != nil {
log.Fatal(err)
}
sch, err := c.Compile(url)
if err != nil {
log.Fatal(err)
}
return &jsonschema.Vocabulary{
URL: url,
Schema: sch,
Subschemas: []jsonschema.SchemaPath{
{jsonschema.Prop("discriminator"), jsonschema.AllProp{}, jsonschema.AllProp{}},
},
Compile: compileDiscriminator,
}
}
func compileDiscriminator(ctx *jsonschema.CompilerContext, obj map[string]any) (jsonschema.SchemaExt, error) {
v, ok := obj["discriminator"]
if !ok {
return nil, nil
}
d, ok := v.(map[string]any)
if !ok {
return nil, nil
}
var pname string
var pvalue any
for key, value := range d {
pname = key
pvalue = value
break
}
values := map[string]*jsonschema.Schema{}
vm, ok := pvalue.(map[string]any)
if !ok {
return nil, nil
}
for value := range vm {
values[value] = ctx.Enqueue([]string{"discriminator", pname, value})
}
return &discriminator{pname, values}, nil
}
// Example --
func Example_vocab_discriminator() {
// if kind is fish, swimmingSpeed is required
// if kind is dog, runningSpeed is required
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(`{
"type": "object",
"properties": {
"kind": { "type": "string" }
},
"required": ["kind"],
"discriminator": {
"kind": {
"fish": {
"type": "object",
"properties": {
"swimmingSpeed": { "type": "number" }
},
"required": ["swimmingSpeed"]
},
"dog": {
"type": "object",
"properties": {
"runningSpeed": { "type": "number" }
},
"required": ["runningSpeed"]
}
}
}
}`))
if err != nil {
log.Fatal(err)
}
inst, err := jsonschema.UnmarshalJSON(strings.NewReader(`{
"kind": "fish",
"runningSpeed": 5
}`))
if err != nil {
log.Fatal(err)
}
c := jsonschema.NewCompiler()
c.AssertVocabs()
c.RegisterVocabulary(discriminatorVocab())
if err := c.AddResource("schema.json", schema); err != nil {
log.Fatal(err)
}
sch, err := c.Compile("schema.json")
if err != nil {
log.Fatal(err)
}
err = sch.Validate(inst)
fmt.Println("valid:", err == nil)
// Output:
// valid: false
}