Skip to content

Commit

Permalink
fix: resolve errcheck and redundant len() nil checks
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoChengH authored and kaptinlin committed Nov 7, 2024
1 parent f56c7b7 commit 7744ec3
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions additionalProperties.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func evaluateAdditionalProperties(schema *Schema, object map[string]interface{},
if !properties[propName] {
result, _, _ := schema.AdditionalProperties.evaluate(propValue, dynamicScope)
if result != nil {
//nolint:errcheck
result.SetEvaluationPath(fmt.Sprintf("/additionalProperties/%s", propName)).
SetSchemaLocation(schema.GetSchemaLocation(fmt.Sprintf("/additionalProperties/%s", propName))).
SetInstanceLocation(fmt.Sprintf("/%s", propName))
Expand Down
2 changes: 1 addition & 1 deletion allOf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
//
// Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-allof
func evaluateAllOf(schema *Schema, instance interface{}, evaluatedProps map[string]bool, evaluatedItems map[int]bool, dynamicScope *DynamicScope) ([]*EvaluationResult, *EvaluationError) {
if schema.AllOf == nil || len(schema.AllOf) == 0 {
if len(schema.AllOf) == 0 {
return nil, nil // No allOf constraints to validate against.
}

Expand Down
2 changes: 1 addition & 1 deletion anyOf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
//
// Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-anyof
func evaluateAnyOf(schema *Schema, data interface{}, evaluatedProps map[string]bool, evaluatedItems map[int]bool, dynamicScope *DynamicScope) ([]*EvaluationResult, *EvaluationError) {
if schema.AnyOf == nil || len(schema.AnyOf) == 0 {
if len(schema.AnyOf) == 0 {
return nil, nil // No anyOf constraints to validate against.
}

Expand Down
2 changes: 2 additions & 0 deletions conditional.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func evaluateConditional(schema *Schema, instance interface{}, evaluatedProps ma
results := []*EvaluationResult{}

if ifResult != nil {
//nolint:errcheck
ifResult.SetEvaluationPath("/if").
SetSchemaLocation(schema.GetSchemaLocation("/if")).
SetInstanceLocation("")
Expand All @@ -38,6 +39,7 @@ func evaluateConditional(schema *Schema, instance interface{}, evaluatedProps ma
thenResult, thenEvaluatedProps, thenEvaluatedItems := schema.Then.evaluate(instance, dynamicScope)

if thenResult != nil {
//nolint:errcheck
thenResult.SetEvaluationPath("/then").
SetSchemaLocation(schema.GetSchemaLocation("/then")).
SetInstanceLocation("")
Expand Down
1 change: 1 addition & 0 deletions contains.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func evaluateContains(schema *Schema, data []interface{}, evaluatedProps map[str
result, _, _ := schema.Contains.evaluate(item, dynamicScope)

if result != nil {
//nolint:errcheck
result.SetEvaluationPath("/contains").
SetSchemaLocation(schema.GetSchemaLocation("/contains")).
SetInstanceLocation(fmt.Sprintf("/%d", i))
Expand Down
1 change: 1 addition & 0 deletions content.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func evaluateContent(schema *Schema, instance interface{}, evaluatedProps map[st
if schema.ContentSchema != nil {
result, _, _ := schema.ContentSchema.evaluate(parsedValue, dynamicScope)
if result != nil {
//nolint:errcheck
result.SetEvaluationPath("/contentSchema").
SetSchemaLocation(schema.GetSchemaLocation("/contentSchema")).
SetInstanceLocation("")
Expand Down
3 changes: 2 additions & 1 deletion dependentSchemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
//
// Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-dependentschemas
func evaluateDependentSchemas(schema *Schema, instance interface{}, evaluatedProps map[string]bool, evaluatedItems map[int]bool, dynamicScope *DynamicScope) ([]*EvaluationResult, *EvaluationError) {
if schema.DependentSchemas == nil || len(schema.DependentSchemas) == 0 {
if len(schema.DependentSchemas) == 0 {
return nil, nil // No dependentSchemas constraints to validate against.
}

Expand All @@ -31,6 +31,7 @@ func evaluateDependentSchemas(schema *Schema, instance interface{}, evaluatedPro
if depSchema != nil {
result, schemaEvaluatedProps, schemaEvaluatedItems := depSchema.evaluate(object, dynamicScope)
if result != nil {
//nolint:errcheck
result.SetEvaluationPath(fmt.Sprintf("/dependentSchemas/%s", propName)).
SetSchemaLocation(schema.GetSchemaLocation(fmt.Sprintf("/dependentSchemas/%s", propName))).
SetInstanceLocation(fmt.Sprintf("/%s", propName))
Expand Down
2 changes: 1 addition & 1 deletion enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "reflect"
//
// Reference: https://json-schema.org/draft/2020-12/json-schema-validation#name-enum
func evaluateEnum(schema *Schema, instance interface{}) *EvaluationError {
if schema.Enum != nil && len(schema.Enum) > 0 {
if len(schema.Enum) > 0 {
for _, enumValue := range schema.Enum {
if reflect.DeepEqual(instance, enumValue) {
return nil // Match found.
Expand Down
1 change: 1 addition & 0 deletions items.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func evaluateItems(schema *Schema, array []interface{}, evaluatedProps map[strin
item := array[i]
result, _, _ := schema.Items.evaluate(item, dynamicScope)
if result != nil {
//nolint:errcheck
result.SetEvaluationPath(fmt.Sprintf("/items/%d", i)).
SetSchemaLocation(schema.GetSchemaLocation(fmt.Sprintf("/items/%d", i))).
SetInstanceLocation(fmt.Sprintf("/%d", i))
Expand Down
1 change: 1 addition & 0 deletions not.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func evaluateNot(schema *Schema, instance interface{}, evaluatedProps map[string
result, _, _ := schema.Not.evaluate(instance, dynamicScope)

if result != nil {
//nolint:errcheck
result.SetEvaluationPath("/oneOf").
SetSchemaLocation(schema.GetSchemaLocation("/oneOf")).
SetInstanceLocation("")
Expand Down
2 changes: 1 addition & 1 deletion oneOf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
//
// Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-oneof
func evaluateOneOf(schema *Schema, instance interface{}, evaluatedProps map[string]bool, evaluatedItems map[int]bool, dynamicScope *DynamicScope) ([]*EvaluationResult, *EvaluationError) {
if schema.OneOf == nil || len(schema.OneOf) == 0 {
if len(schema.OneOf) == 0 {
return nil, nil // No oneOf constraints to validate against.
}

Expand Down
1 change: 1 addition & 0 deletions patternProperties.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func evaluatePatternProperties(schema *Schema, object map[string]interface{}, ev
// Evaluate the property value directly using the associated schema or boolean.
result, _, _ := patternSchema.evaluate(propValue, dynamicScope)
if result != nil {
//nolint:errcheck
result.SetEvaluationPath(fmt.Sprintf("/patternProperties/%s", propName)).
SetSchemaLocation(schema.GetSchemaLocation(fmt.Sprintf("/patternProperties/%s", propName))).
SetInstanceLocation(fmt.Sprintf("/%s", propName))
Expand Down
2 changes: 1 addition & 1 deletion prefixItems.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
//
// If validation fails, it returns a EvaluationError detailing the index and discrepancy.
func evaluatePrefixItems(schema *Schema, array []interface{}, evaluatedProps map[string]bool, evaluatedItems map[int]bool, dynamicScope *DynamicScope) ([]*EvaluationResult, *EvaluationError) {
if schema.PrefixItems == nil || len(schema.PrefixItems) == 0 {
if len(schema.PrefixItems) == 0 {
return nil, nil // If no prefixItems are defined, there is nothing to validate against.
}

Expand Down
2 changes: 2 additions & 0 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func evaluateProperties(schema *Schema, object map[string]interface{}, evaluated
if exists {
result, _, _ := propSchema.evaluate(propValue, dynamicScope)
if result != nil {
//nolint:errcheck
result.SetEvaluationPath(fmt.Sprintf("/properties/%s", propName)).
SetSchemaLocation(schema.GetSchemaLocation(fmt.Sprintf("/properties/%s", propName))).
SetInstanceLocation(fmt.Sprintf("/%s", propName))
Expand All @@ -45,6 +46,7 @@ func evaluateProperties(schema *Schema, object map[string]interface{}, evaluated
result, _, _ := propSchema.evaluate(nil, dynamicScope)

if result != nil {
//nolint:errcheck
result.SetEvaluationPath(fmt.Sprintf("/properties/%s", propName)).
SetSchemaLocation(schema.GetSchemaLocation(fmt.Sprintf("/properties/%s", propName))).
SetInstanceLocation(fmt.Sprintf("/%s", propName))
Expand Down
1 change: 1 addition & 0 deletions propertyNames.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func evaluatePropertyNames(schema *Schema, object map[string]interface{}, evalua
result, _, _ := schema.PropertyNames.evaluate(propName, dynamicScope)

if result != nil {
//nolint:errcheck
result.SetEvaluationPath(fmt.Sprintf("/propertyNames/%s", propName)).
SetSchemaLocation(schema.GetSchemaLocation(fmt.Sprintf("/propertyNames/%s", propName))).
SetInstanceLocation(fmt.Sprintf("/%s", propName))
Expand Down
2 changes: 1 addition & 1 deletion result.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewEvaluationResult(schema *Schema) *EvaluationResult {
schema: schema,
Valid: true,
}

//nolint:errcheck
e.CollectAnnotations()

return e
Expand Down
1 change: 1 addition & 0 deletions unevaluatedItems.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func evaluateUnevaluatedItems(schema *Schema, data interface{}, evaluatedProps m
if _, evaluated := evaluatedItems[i]; !evaluated {
result, _, _ := schema.UnevaluatedItems.evaluate(item, dynamicScope)
if result != nil {
//nolint:errcheck
result.SetEvaluationPath(fmt.Sprintf("/unevaluatedItems/%d", i)).
SetSchemaLocation(schema.GetSchemaLocation(fmt.Sprintf("/unevaluatedItems/%d", i))).
SetInstanceLocation(fmt.Sprintf("/%d", i))
Expand Down
1 change: 1 addition & 0 deletions unevaluatedProperties.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func evaluateUnevaluatedProperties(schema *Schema, data interface{}, evaluatedPr
// If property has not been evaluated, validate it against the "unevaluatedProperties" schema.
result, _, _ := schema.UnevaluatedProperties.evaluate(propValue, dynamicScope)
if result != nil {
//nolint:errcheck
result.SetEvaluationPath("/unevaluatedProperties").
SetSchemaLocation(schema.GetSchemaLocation("/unevaluatedProperties")).
SetInstanceLocation(fmt.Sprintf("/%s", propName))
Expand Down
Loading

0 comments on commit 7744ec3

Please sign in to comment.