Skip to content

Commit 26566d8

Browse files
committed
cleaned docs and comments
1 parent c17d473 commit 26566d8

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

openapi_vocabulary/coercion.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ type coercionExtension struct {
1717
}
1818

1919
var (
20-
// Pre-compiled regex for boolean coercion
2120
booleanRegex = regexp.MustCompile(`^(true|false)$`)
22-
// Pre-compiled regex for number validation
23-
numberRegex = regexp.MustCompile(`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$`)
24-
// Pre-compiled regex for integer validation
21+
numberRegex = regexp.MustCompile(`^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$`)
2522
integerRegex = regexp.MustCompile(`^-?(?:0|[1-9]\d*)$`)
2623
)
2724

@@ -35,7 +32,7 @@ func (c *coercionExtension) Validate(ctx *jsonschema.ValidatorContext, v any) {
3532
return // Not a string - let normal validation handle it
3633
}
3734

38-
// Check if we should coerce and validate the string format
35+
// check if we should coerce and validate the string format
3936
if c.shouldCoerceToBoolean() {
4037
if !c.isValidBooleanString(str) {
4138
ctx.AddError(&CoercionError{
@@ -71,8 +68,6 @@ func (c *coercionExtension) Validate(ctx *jsonschema.ValidatorContext, v any) {
7168
}
7269
return
7370
}
74-
75-
// No coercion applies - let normal validation handle it
7671
}
7772

7873
func (c *coercionExtension) shouldCoerceToBoolean() bool {
@@ -123,7 +118,7 @@ func (c *coercionExtension) isValidIntegerString(s string) bool {
123118
return err == nil
124119
}
125120

126-
// compileCoercion compiles coercion behavior based on schema type
121+
// CompileCoercion compiles the coercion extension if coercion is allowed and applicable
127122
func CompileCoercion(ctx *jsonschema.CompilerContext, obj map[string]any, allowCoercion bool) (jsonschema.SchemaExt, error) {
128123
if !allowCoercion {
129124
return nil, nil // Coercion disabled
@@ -146,13 +141,13 @@ func CompileCoercion(ctx *jsonschema.CompilerContext, obj map[string]any, allowC
146141
}, nil
147142
}
148143

149-
// isCoercibleType checks if the type supports scalar coercion
144+
// IsCoercibleType checks if the schema type is one that supports coercion
150145
func IsCoercibleType(schemaType any) bool {
151146
switch t := schemaType.(type) {
152147
case string:
153148
return t == "boolean" || t == "number" || t == "integer"
154149
case []any:
155-
// For type arrays, check if any coercible type is present
150+
// for type arrays, check if any coercible type is present
156151
for _, item := range t {
157152
if str, ok := item.(string); ok {
158153
if str == "boolean" || str == "number" || str == "integer" {

schema_validation/validate_schema.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func (s *schemaValidator) validateSchemaWithVersion(schema *base.Schema, payload
137137
if decodedObject == nil && len(payload) > 0 {
138138
err := json.Unmarshal(payload, &decodedObject)
139139
if err != nil {
140+
140141
// cannot decode the request body, so it's not valid
141142
violation := &liberrors.SchemaValidationFailure{
142143
Reason: err.Error(),
@@ -160,15 +161,10 @@ func (s *schemaValidator) validateSchemaWithVersion(schema *base.Schema, payload
160161

161162
}
162163

163-
// Build the compiled JSON Schema with version awareness
164164
jsch, err := helpers.NewCompiledSchemaWithVersion("schema", jsonSchema, s.options, version)
165165

166166
var schemaValidationErrors []*liberrors.SchemaValidationFailure
167-
168-
// is the schema even valid? did it compile?
169167
if err != nil {
170-
171-
// Handle any compilation error (including vocabulary errors)
172168
violation := &liberrors.SchemaValidationFailure{
173169
Reason: err.Error(),
174170
Location: "schema compilation",
@@ -189,7 +185,6 @@ func (s *schemaValidator) validateSchemaWithVersion(schema *base.Schema, payload
189185
return false, validationErrors
190186
}
191187

192-
// 4. validate the object against the schema
193188
if jsch != nil && decodedObject != nil {
194189
scErrs := jsch.Validate(decodedObject)
195190
if scErrs != nil {
@@ -209,7 +204,6 @@ func (s *schemaValidator) validateSchemaWithVersion(schema *base.Schema, payload
209204
col = schema.GoLow().Type.KeyNode.Column
210205
}
211206

212-
// add the error to the list
213207
validationErrors = append(validationErrors, &liberrors.ValidationError{
214208
ValidationType: helpers.Schema,
215209
Message: "schema does not pass validation",

0 commit comments

Comments
 (0)