File tree Expand file tree Collapse file tree 5 files changed +50
-3
lines changed
Expand file tree Collapse file tree 5 files changed +50
-3
lines changed Original file line number Diff line number Diff 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+
163175type Meta struct {
164176 Key any
165177 Kind Kind
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package cmw
22
33import (
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
3435func (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
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments