diff --git a/src/pkg/jsonschema/schema.go b/src/pkg/jsonschema/schema.go index 3c268661..72cfe939 100644 --- a/src/pkg/jsonschema/schema.go +++ b/src/pkg/jsonschema/schema.go @@ -13,6 +13,7 @@ import ( "github.com/cldcvr/terrarium/src/pkg/pb/terrariumpb" "github.com/rotisserie/eris" "github.com/xeipuuv/gojsonschema" + "google.golang.org/protobuf/types/known/structpb" ) type Node struct { @@ -158,5 +159,30 @@ func (n *Node) ToProto() *terrariumpb.JSONSchema { } } + if n.Default != nil { + switch defaultValue := n.Default.(type) { + case float64: + protoSchema.Default = &structpb.Value{ + Kind: &structpb.Value_NumberValue{ + NumberValue: defaultValue, + }, + } + case int: + protoSchema.Default = &structpb.Value{ + Kind: &structpb.Value_NumberValue{ + NumberValue: float64(defaultValue), + }, + } + case string: + protoSchema.Default = &structpb.Value{ + Kind: &structpb.Value_StringValue{ + StringValue: defaultValue, + }, + } + // Add more cases as needed for other types + default: + protoSchema.Default = nil + } + } return protoSchema } diff --git a/src/pkg/jsonschema/schema_test.go b/src/pkg/jsonschema/schema_test.go index 7399d716..6799831c 100644 --- a/src/pkg/jsonschema/schema_test.go +++ b/src/pkg/jsonschema/schema_test.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/xeipuuv/gojsonschema" + "google.golang.org/protobuf/types/known/structpb" ) func TestNode_Compile(t *testing.T) { @@ -254,6 +255,121 @@ func TestNode_ToProto(t *testing.T) { }, wantErr: false, }, + { + name: "JSONSchema with Default float", + jsn: &Node{ + Title: "Test Title", + Description: "Test Description", + Type: "object", + Properties: map[string]*Node{ + "property1": { + Title: "Property 1", + Type: "string", + }, + "property2": { + Title: "Property 2", + Type: "integer", + }, + }, + Default: 80.0, // Adding a Default value of type float64 + }, + validator: func(t *testing.T, result *terrariumpb.JSONSchema) { + require.NotNil(t, result) + assert.Equal(t, "Test Title", result.Title) + assert.Equal(t, "Test Description", result.Description) + assert.Equal(t, "object", result.Type) + require.Len(t, result.Properties, 2) + assert.NotNil(t, result.Properties["property1"]) + assert.NotNil(t, result.Properties["property2"]) + + // Asserting the Default field value + require.NotNil(t, result.Default) + assert.IsType(t, &structpb.Value_NumberValue{}, result.Default.Kind) + defaultValue := result.Default.GetNumberValue() + assert.Equal(t, float64(80.0), defaultValue) + }, + wantErr: false, + }, + { + name: "JSONSchema with Default int", + jsn: &Node{ + Title: "Test Title", + Description: "Test Description", + Type: "object", + Properties: map[string]*Node{ + "property1": { + Title: "Property 1", + Type: "string", + }, + "property2": { + Title: "Property 2", + Type: "integer", + }, + }, + Default: 100, // Adding a Default value of type int + }, + validator: func(t *testing.T, result *terrariumpb.JSONSchema) { + require.NotNil(t, result) + assert.NotNil(t, result.Default) + + intValue, ok := result.Default.GetKind().(*structpb.Value_NumberValue) + require.True(t, ok, "Expected Default to be of type int") + assert.Equal(t, float64(100), intValue.NumberValue) + }, + wantErr: false, + }, + { + name: "JSONSchema with Default string", + jsn: &Node{ + Title: "Test Title", + Description: "Test Description", + Type: "object", + Properties: map[string]*Node{ + "property1": { + Title: "Property 1", + Type: "string", + }, + "property2": { + Title: "Property 2", + Type: "integer", + }, + }, + Default: "hello", // Adding a Default value of type string + }, + validator: func(t *testing.T, result *terrariumpb.JSONSchema) { + require.NotNil(t, result) + assert.NotNil(t, result.Default) + + stringValue, ok := result.Default.GetKind().(*structpb.Value_StringValue) + require.True(t, ok, "Expected Default to be of type string") + assert.Equal(t, "hello", stringValue.StringValue) + }, + wantErr: false, + }, + { + name: "JSONSchema with nil Default", + jsn: &Node{ + Title: "Test Title", + Description: "Test Description", + Type: "object", + Properties: map[string]*Node{ + "property1": { + Title: "Property 1", + Type: "string", + }, + "property2": { + Title: "Property 2", + Type: "integer", + }, + }, + Default: nil, // Setting Default as nil + }, + validator: func(t *testing.T, result *terrariumpb.JSONSchema) { + require.NotNil(t, result) + assert.Nil(t, result.Default) + }, + wantErr: false, + }, } for _, tt := range tests {