@@ -11,6 +11,210 @@ import (
1111 "github.com/stretchr/testify/require"
1212)
1313
14+ func TestValidate_TopLevel_Success (t * testing.T ) {
15+ t .Parallel ()
16+
17+ t .Run ("nil schema returns nil" , func (t * testing.T ) {
18+ t .Parallel ()
19+
20+ var schema * oas3.JSONSchema [oas3.Referenceable ]
21+ errs := oas3 .Validate (t .Context (), schema )
22+ require .Nil (t , errs , "nil schema should return nil errors" )
23+ })
24+
25+ t .Run ("bool schema returns nil" , func (t * testing.T ) {
26+ t .Parallel ()
27+
28+ schema := oas3 .NewJSONSchemaFromBool (true )
29+ errs := oas3 .Validate (t .Context (), schema )
30+ require .Nil (t , errs , "bool schema should return nil errors" )
31+ })
32+
33+ t .Run ("bool false schema returns nil" , func (t * testing.T ) {
34+ t .Parallel ()
35+
36+ schema := oas3 .NewJSONSchemaFromBool (false )
37+ errs := oas3 .Validate (t .Context (), schema )
38+ require .Nil (t , errs , "bool false schema should return nil errors" )
39+ })
40+
41+ t .Run ("valid schema returns nil errors" , func (t * testing.T ) {
42+ t .Parallel ()
43+
44+ yml := `
45+ type: string
46+ title: Valid Schema
47+ `
48+ var schema oas3.JSONSchema [oas3.Referenceable ]
49+ _ , err := marshaller .Unmarshal (t .Context (), bytes .NewBufferString (yml ), & schema )
50+ require .NoError (t , err )
51+
52+ errs := oas3 .Validate (t .Context (), & schema )
53+ require .Empty (t , errs , "valid schema should return no errors" )
54+ })
55+ }
56+
57+ func TestValidate_TopLevel_Error (t * testing.T ) {
58+ t .Parallel ()
59+
60+ t .Run ("invalid schema returns errors" , func (t * testing.T ) {
61+ t .Parallel ()
62+
63+ yml := `
64+ type: invalid_type
65+ `
66+ var schema oas3.JSONSchema [oas3.Referenceable ]
67+ _ , err := marshaller .Unmarshal (t .Context (), bytes .NewBufferString (yml ), & schema )
68+ require .NoError (t , err )
69+
70+ errs := oas3 .Validate (t .Context (), & schema )
71+ require .NotEmpty (t , errs , "invalid schema should return errors" )
72+ })
73+ }
74+
75+ func TestSchema_Validate_OpenAPIVersions_Success (t * testing.T ) {
76+ t .Parallel ()
77+
78+ tests := []struct {
79+ name string
80+ version string
81+ yml string
82+ }{
83+ {
84+ name : "OpenAPI 3.0 version via context" ,
85+ version : "3.0.3" ,
86+ yml : `
87+ type: string
88+ ` ,
89+ },
90+ {
91+ name : "OpenAPI 3.1 version via context" ,
92+ version : "3.1.0" ,
93+ yml : `
94+ type: string
95+ ` ,
96+ },
97+ {
98+ name : "OpenAPI 3.2 version via context" ,
99+ version : "3.2.0" ,
100+ yml : `
101+ type: string
102+ ` ,
103+ },
104+ }
105+
106+ for _ , tt := range tests {
107+ t .Run (tt .name , func (t * testing.T ) {
108+ t .Parallel ()
109+
110+ var schema oas3.Schema
111+ _ , err := marshaller .Unmarshal (t .Context (), bytes .NewBufferString (tt .yml ), & schema )
112+ require .NoError (t , err )
113+
114+ dv := & oas3.ParentDocumentVersion {
115+ OpenAPI : & tt .version ,
116+ }
117+
118+ errs := schema .Validate (t .Context (), validation .WithContextObject (dv ))
119+ require .Empty (t , errs , "valid schema should return no errors for version %s" , tt .version )
120+ })
121+ }
122+ }
123+
124+ func TestSchema_Validate_SchemaField_Success (t * testing.T ) {
125+ t .Parallel ()
126+
127+ tests := []struct {
128+ name string
129+ yml string
130+ }{
131+ {
132+ name : "explicit 3.0 $schema field" ,
133+ yml : `
134+ $schema: "https://spec.openapis.org/oas/3.0/dialect/2024-10-18"
135+ type: string
136+ ` ,
137+ },
138+ {
139+ name : "explicit 3.1 $schema field" ,
140+ yml : `
141+ $schema: "https://spec.openapis.org/oas/3.1/meta/2024-11-10"
142+ type: string
143+ ` ,
144+ },
145+ }
146+
147+ for _ , tt := range tests {
148+ t .Run (tt .name , func (t * testing.T ) {
149+ t .Parallel ()
150+
151+ var schema oas3.Schema
152+ _ , err := marshaller .Unmarshal (t .Context (), bytes .NewBufferString (tt .yml ), & schema )
153+ require .NoError (t , err )
154+
155+ errs := schema .Validate (t .Context ())
156+ require .Empty (t , errs , "valid schema should return no errors" )
157+ })
158+ }
159+ }
160+
161+ func TestSchema_Validate_UnsupportedVersion_Defaults (t * testing.T ) {
162+ t .Parallel ()
163+
164+ t .Run ("unsupported OpenAPI version defaults to 3.1" , func (t * testing.T ) {
165+ t .Parallel ()
166+
167+ yml := `
168+ type: string
169+ `
170+ var schema oas3.Schema
171+ _ , err := marshaller .Unmarshal (t .Context (), bytes .NewBufferString (yml ), & schema )
172+ require .NoError (t , err )
173+
174+ version := "2.0.0"
175+ dv := & oas3.ParentDocumentVersion {
176+ OpenAPI : & version ,
177+ }
178+
179+ errs := schema .Validate (t .Context (), validation .WithContextObject (dv ))
180+ require .Empty (t , errs , "unsupported version should default to 3.1 and validate successfully" )
181+ })
182+
183+ t .Run ("Arazzo version is unsupported and defaults to 3.1" , func (t * testing.T ) {
184+ t .Parallel ()
185+
186+ yml := `
187+ type: string
188+ `
189+ var schema oas3.Schema
190+ _ , err := marshaller .Unmarshal (t .Context (), bytes .NewBufferString (yml ), & schema )
191+ require .NoError (t , err )
192+
193+ version := "1.0.0"
194+ dv := & oas3.ParentDocumentVersion {
195+ Arazzo : & version ,
196+ }
197+
198+ errs := schema .Validate (t .Context (), validation .WithContextObject (dv ))
199+ require .Empty (t , errs , "Arazzo version should default to 3.1 and validate successfully" )
200+ })
201+
202+ t .Run ("unsupported $schema field defaults to 3.1" , func (t * testing.T ) {
203+ t .Parallel ()
204+
205+ yml := `
206+ $schema: "https://json-schema.org/draft/2020-12/schema"
207+ type: string
208+ `
209+ var schema oas3.Schema
210+ _ , err := marshaller .Unmarshal (t .Context (), bytes .NewBufferString (yml ), & schema )
211+ require .NoError (t , err )
212+
213+ errs := schema .Validate (t .Context ())
214+ require .Empty (t , errs , "unsupported $schema should default to 3.1" )
215+ })
216+ }
217+
14218func TestJSONSchema_Validate_Error (t * testing.T ) {
15219 t .Parallel ()
16220
0 commit comments