Skip to content

Commit 88757e0

Browse files
author
Chance Kirsch
committed
Fix the Println formatting issues
1 parent 1598a37 commit 88757e0

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

openapi3/ISSUE_230.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ if err != nil {
8989

9090
if doc.IsOpenAPI3_1() {
9191
openapi3.UseJSONSchema2020Validator = true
92-
fmt.Println("OpenAPI 3.1 document detected")
92+
fmt.Println("openAPI 3.1 document detected")
9393
}
9494
```
9595

openapi3/example_jsonschema2020_test.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ func Example_jsonSchema2020Validator() {
4848
}
4949

5050
if err := schema.VisitJSON(validData, openapi3.EnableJSONSchema2020()); err != nil {
51-
fmt.Println("Validation failed:", err)
51+
fmt.Println("validation failed:", err)
5252
} else {
53-
fmt.Println("Valid data passed")
53+
fmt.Println("valid data passed")
5454
}
5555

5656
// Valid with null age
@@ -61,9 +61,9 @@ func Example_jsonSchema2020Validator() {
6161
}
6262

6363
if err := schema.VisitJSON(validWithNull, openapi3.EnableJSONSchema2020()); err != nil {
64-
fmt.Println("Validation failed:", err)
64+
fmt.Println("validation failed:", err)
6565
} else {
66-
fmt.Println("Valid data with null passed")
66+
fmt.Println("valid data with null passed")
6767
}
6868

6969
// Invalid: wrong const value
@@ -74,13 +74,13 @@ func Example_jsonSchema2020Validator() {
7474
}
7575

7676
if err := schema.VisitJSON(invalidData, openapi3.EnableJSONSchema2020()); err != nil {
77-
fmt.Println("Invalid data rejected")
77+
fmt.Println("invalid data rejected")
7878
}
7979

8080
// Output:
81-
// Valid data passed
82-
// Valid data with null passed
83-
// Invalid data rejected
81+
// valid data passed
82+
// valid data with null passed
83+
// invalid data rejected
8484
}
8585

8686
// Example demonstrates type arrays with null support
@@ -92,21 +92,21 @@ func Example_typeArrayWithNull() {
9292

9393
// Both string and null are valid
9494
if err := schema.VisitJSON("hello", openapi3.EnableJSONSchema2020()); err == nil {
95-
fmt.Println("String accepted")
95+
fmt.Println("string accepted")
9696
}
9797

9898
if err := schema.VisitJSON(nil, openapi3.EnableJSONSchema2020()); err == nil {
99-
fmt.Println("Null accepted")
99+
fmt.Println("null accepted")
100100
}
101101

102102
if err := schema.VisitJSON(123, openapi3.EnableJSONSchema2020()); err != nil {
103-
fmt.Println("Number rejected")
103+
fmt.Println("number rejected")
104104
}
105105

106106
// Output:
107-
// String accepted
108-
// Null accepted
109-
// Number rejected
107+
// string accepted
108+
// null accepted
109+
// number rejected
110110
}
111111

112112
// Example demonstrates the const keyword
@@ -117,16 +117,16 @@ func Example_constKeyword() {
117117
}
118118

119119
if err := schema.VisitJSON("production", openapi3.EnableJSONSchema2020()); err == nil {
120-
fmt.Println("Const value accepted")
120+
fmt.Println("const value accepted")
121121
}
122122

123123
if err := schema.VisitJSON("development", openapi3.EnableJSONSchema2020()); err != nil {
124-
fmt.Println("Other value rejected")
124+
fmt.Println("other value rejected")
125125
}
126126

127127
// Output:
128-
// Const value accepted
129-
// Other value rejected
128+
// const value accepted
129+
// other value rejected
130130
}
131131

132132
// Example demonstrates the examples field
@@ -144,11 +144,11 @@ func Example_examplesField() {
144144

145145
// Examples don't affect validation, any string is valid
146146
if err := schema.VisitJSON("yellow", openapi3.EnableJSONSchema2020()); err == nil {
147-
fmt.Println("Any string accepted")
147+
fmt.Println("any string accepted")
148148
}
149149

150150
// Output:
151-
// Any string accepted
151+
// any string accepted
152152
}
153153

154154
// Example demonstrates backward compatibility with nullable
@@ -162,16 +162,16 @@ func Example_nullableBackwardCompatibility() {
162162

163163
// Automatically converted to type array ["string", "null"]
164164
if err := schema.VisitJSON("hello", openapi3.EnableJSONSchema2020()); err == nil {
165-
fmt.Println("String accepted")
165+
fmt.Println("string accepted")
166166
}
167167

168168
if err := schema.VisitJSON(nil, openapi3.EnableJSONSchema2020()); err == nil {
169-
fmt.Println("Null accepted")
169+
fmt.Println("null accepted")
170170
}
171171

172172
// Output:
173-
// String accepted
174-
// Null accepted
173+
// string accepted
174+
// null accepted
175175
}
176176

177177
// Example demonstrates complex nested schemas
@@ -222,11 +222,11 @@ func Example_complexNestedSchema() {
222222
}
223223

224224
if err := schema.VisitJSON(validData, openapi3.EnableJSONSchema2020()); err == nil {
225-
fmt.Println("Complex nested object validated")
225+
fmt.Println("complex nested object validated")
226226
}
227227

228228
// Output:
229-
// Complex nested object validated
229+
// complex nested object validated
230230
}
231231

232232
// Example demonstrates using both validators for comparison
@@ -241,16 +241,16 @@ func Example_comparingValidators() {
241241
// Test with built-in validator (no option)
242242
err1 := schema.VisitJSON(testValue)
243243
if err1 != nil {
244-
fmt.Println("Built-in validator: rejected")
244+
fmt.Println("built-in validator: rejected")
245245
}
246246

247247
// Test with JSON Schema 2020-12 validator
248248
err2 := schema.VisitJSON(testValue, openapi3.EnableJSONSchema2020())
249249
if err2 != nil {
250-
fmt.Println("JSON Schema 2020-12 validator: rejected")
250+
fmt.Println("visit JSON Schema 2020-12 validator: rejected")
251251
}
252252

253253
// Output:
254-
// Built-in validator: rejected
255-
// JSON Schema 2020-12 validator: rejected
254+
// built-in validator: rejected
255+
// visit JSON Schema 2020-12 validator: rejected
256256
}

openapi3/schema_jsonschema_validator.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package openapi3
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"strings"
78

@@ -68,14 +69,14 @@ func transformOpenAPIToJSONSchema(schema map[string]any) {
6869
// In OpenAPI 3.0, these are booleans alongside minimum/maximum
6970
// In JSON Schema 2020-12, they are numeric values
7071
if exclusiveMin, ok := schema["exclusiveMinimum"].(bool); ok && exclusiveMin {
71-
if min, ok := schema["minimum"].(float64); ok {
72-
schema["exclusiveMinimum"] = min
72+
if schemaMin, ok := schema["minimum"].(float64); ok {
73+
schema["exclusiveMinimum"] = schemaMin
7374
delete(schema, "minimum")
7475
}
7576
}
7677
if exclusiveMax, ok := schema["exclusiveMaximum"].(bool); ok && exclusiveMax {
77-
if max, ok := schema["maximum"].(float64); ok {
78-
schema["exclusiveMaximum"] = max
78+
if schemaMax, ok := schema["maximum"].(float64); ok {
79+
schema["exclusiveMaximum"] = schemaMax
7980
delete(schema, "maximum")
8081
}
8182
}
@@ -127,7 +128,8 @@ func (v *jsonSchemaValidator) validate(value any) error {
127128

128129
// convertJSONSchemaError converts a jsonschema validation error to OpenAPI SchemaError format
129130
func convertJSONSchemaError(err error) error {
130-
if validationErr, ok := err.(*jsonschema.ValidationError); ok {
131+
var validationErr *jsonschema.ValidationError
132+
if errors.As(err, &validationErr) {
131133
return formatValidationError(validationErr, "")
132134
}
133135
return err
@@ -146,7 +148,7 @@ func formatValidationError(verr *jsonschema.ValidationError, parentPath string)
146148
// Build error message using the Error() method
147149
var msg strings.Builder
148150
if path != "" {
149-
msg.WriteString(fmt.Sprintf(`Error at "%s": `, path))
151+
msg.WriteString(fmt.Sprintf(`error at "%s": `, path))
150152
}
151153
msg.WriteString(verr.Error())
152154

0 commit comments

Comments
 (0)