Skip to content

Commit 8bd66e5

Browse files
Fix validation of CMW
Fixes #23 Signed-off-by: Yogesh Deshpande <[email protected]> Minor correction Signed-off-by: Yogesh Deshpande <[email protected]> Further correction Signed-off-by: Yogesh Deshpande <[email protected]>
1 parent 0879f49 commit 8bd66e5

File tree

6 files changed

+53
-3
lines changed

6 files changed

+53
-3
lines changed

cmw.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ func (o CMW) ValidateCollection() error {
160160
return o.collection.validate()
161161
}
162162

163+
// Valid checks whether a CMW is a valid CMW or not?
164+
func (o CMW) Valid() error {
165+
switch o.kind {
166+
case KindMonad:
167+
return o.monad.valid()
168+
case KindCollection:
169+
return o.ValidateCollection()
170+
default:
171+
return fmt.Errorf("unknown kind: %s", o.kind.String())
172+
}
173+
}
174+
163175
type Meta struct {
164176
Key any
165177
Kind Kind

cmw_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,19 @@ func Test_GetCollectionGet(t *testing.T) {
180180
assert.EqualError(t, err, `item not found for key "uh?"`)
181181
assert.Nil(t, itemNotFound)
182182
}
183+
184+
func Test_Valid(t *testing.T) {
185+
typ := "text/plain; charset=utf-8"
186+
val := []byte{0xff}
187+
ind := Indicator(Evidence)
188+
189+
cmw, err := NewMonad(typ, val, ind)
190+
require.NoError(t, err)
191+
err = cmw.Valid()
192+
require.NoError(t, err)
193+
194+
cmw = makeCMWCollection()
195+
require.NoError(t, err)
196+
err = cmw.Valid()
197+
require.NoError(t, err)
198+
}

collection.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func (o collection) validate() error {
3030
}
3131

3232
for k, v := range o.cmap {
33+
if v.kind != KindCollection {
34+
continue
35+
}
3336
if err := v.validate(); err != nil {
3437
return fmt.Errorf("invalid collection at key %q: %w", k, err)
3538
}

example_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ func Example_roundtrip_JSON_collection() {
147147
if err != nil {
148148
log.Fatalf("unmarshal JSON collection failed: %v", err)
149149
}
150-
150+
err = o.ValidateCollection()
151+
if err != nil {
152+
log.Fatalf("validate JSON collection failed: %v", err)
153+
}
151154
b, err := o.MarshalJSON()
152155
if err != nil {
153156
log.Fatalf("marshal collection to JSON failed: %v", err)

monad.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmw
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67

78
"github.com/fxamacker/cbor/v2"
@@ -28,7 +29,7 @@ func (o *monad) UnmarshalJSON(b []byte) error {
2829

2930
o.format = FormatJSONRecord
3031

31-
return nil
32+
return o.valid()
3233
}
3334

3435
func (o monad) MarshalCBOR() ([]byte, error) {
@@ -59,6 +60,20 @@ func (o *monad) UnmarshalCBOR(b []byte) error {
5960
panic(fmt.Sprintf("want CBOR Tag or CBOR array, got 0x%02x", b[0]))
6061
}
6162

63+
return o.valid()
64+
}
65+
66+
func (o monad) valid() error {
67+
f := o.format.String()
68+
switch f {
69+
case "unknown":
70+
case "JSON collection":
71+
case "CBOR collection":
72+
return fmt.Errorf("invalid monad format: %s", f)
73+
}
74+
if !o.typ.IsSet() {
75+
return errors.New("type not set")
76+
}
6277
return nil
6378
}
6479

monad_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ func Test_Deserialize_monad_ok(t *testing.T) {
8888

8989
err := actual.Deserialize(tt.tv)
9090
assert.NoError(t, err)
91-
91+
err = actual.monad.valid()
92+
assert.NoError(t, err)
9293
assert.Equal(t, KindMonad, actual.GetKind())
9394
assert.Equal(t, tt.exp.format, actual.GetFormat())
9495
assert.Equal(t, tt.exp, actual.monad)

0 commit comments

Comments
 (0)