Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions opengin/core-api/pkg/schema/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ func ValidateSchema(value interface{}, schema *SchemaInfo) error {

// Helper functions for schema validation
func validateScalarValue(value interface{}, typeInfo *typeinference.TypeInfo) error {
if typeInfo == nil {
return fmt.Errorf("type info is nil")
}

if value == nil {
if !typeInfo.IsNullable {
return fmt.Errorf("value cannot be null")
Expand Down Expand Up @@ -288,6 +292,10 @@ func validateMapValue(value interface{}, schema *SchemaInfo) error {
if err := ValidateSchema(val, propSchema); err != nil {
return fmt.Errorf("invalid value for key %s: %v", key, err)
}
} else if propSchema == nil {
return fmt.Errorf("schema for key %s is nil", key)
} else if propSchema.TypeInfo == nil {
return fmt.Errorf("type info for key %s is nil", key)
} else if !propSchema.TypeInfo.IsNullable {
return fmt.Errorf("required key %s is missing", key)
}
Expand Down
40 changes: 40 additions & 0 deletions opengin/core-api/pkg/schema/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,46 @@ func TestValidateSchema(t *testing.T) {
}
}

func TestValidateSchemaRejectsNilTypeInfo(t *testing.T) {
schema := &SchemaInfo{StorageType: ScalarData}

err := ValidateSchema("value", schema)

assert.EqualError(t, err, "type info is nil")
}

func TestValidateSchemaRejectsInvalidMissingPropertySchema(t *testing.T) {
testCases := []struct {
name string
property *SchemaInfo
expectedErr string
}{
{
name: "nil property schema",
property: nil,
expectedErr: "schema for key name is nil",
},
{
name: "nil property type info",
property: &SchemaInfo{StorageType: ScalarData},
expectedErr: "type info for key name is nil",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
schema := &SchemaInfo{
StorageType: MapData,
Properties: map[string]*SchemaInfo{"name": tc.property},
}

err := ValidateSchema(map[string]interface{}{}, schema)

assert.EqualError(t, err, tc.expectedErr)
})
}
}

func TestValidateTabularData(t *testing.T) {
// Create a schema for tabular data
schema := &SchemaInfo{
Expand Down