diff --git a/internal/block/int64_component/int64_component.go b/internal/block/int64_component/int64_component.go index 6fae531..07979cf 100644 --- a/internal/block/int64_component/int64_component.go +++ b/internal/block/int64_component/int64_component.go @@ -58,12 +58,14 @@ func (t *Int64ComponentInstance) Value() interface{} { func (t *Int64ComponentInstance) FromJSON(component block.Component, data json.RawMessage) error { var s int64 - err := json.Unmarshal(data, &s) - if err != nil { - return fmt.Errorf("Error unmarshalling property %s: %w", component.Identifier(), err) - } - t.value = s + if data != nil { + err := json.Unmarshal(data, &s) + if err != nil { + return err + } + t.value = s + } for _, v := range component.Validators() { if err := v(s); err != nil { diff --git a/internal/block/int64_component/validators/max_test.go b/internal/block/int64_component/validators/max_test.go new file mode 100644 index 0000000..4dabf49 --- /dev/null +++ b/internal/block/int64_component/validators/max_test.go @@ -0,0 +1,37 @@ +package int64_component_validators + +import ( + "blocky/internal/block/int64_component" + "testing" +) + +func TestMax(t *testing.T) { + cfg := &int64_component.Int64ComponentConfig{} + Max(10)(cfg) + + if len(cfg.Validators) != 1 { + t.Fatalf("expect 1 validator, got %d", len(cfg.Validators)) + } + + validate := cfg.Validators[0] + + tests := []struct { + name string + value int64 + wantErr bool + }{ + + {"below value", 5, false}, + {"equal to value", 10, false}, + {"above value", 15, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validate(tt.value) + if (err != nil) != tt.wantErr { + t.Errorf("validate(%v) error = %v, wanterr %v", tt.value, err, tt.wantErr) + } + }) + } +}