@@ -123,14 +123,44 @@ describe('RecordsWrite schema definition', () => {
123123 } ) . throws ( 'must NOT have additional properties' ) ;
124124 } ) ;
125125
126- it ( 'should pass if `contextId` and `protocol` are both present' , ( ) => {
126+ it ( 'should pass if `protocol` exists and its related properties are all present' , ( ) => {
127+ const validMessage = {
128+ recordId : 'anyRecordId' ,
129+ contextId : 'someContext' , // must exist because `protocol` exists
130+ descriptor : {
131+ interface : 'Records' ,
132+ method : 'Write' ,
133+ protocol : 'someProtocolId' ,
134+ protocolPath : 'foo/bar' , // must exist because `protocol` exists
135+ schema : 'http://foo.bar/schema' , // must exist because `protocol` exists
136+ dataCid : 'anyCid' ,
137+ dataFormat : 'application/json' ,
138+ dataSize : 123 ,
139+ dateCreated : '2022-12-19T10:20:30.123456' ,
140+ dateModified : '2022-12-19T10:20:30.123456'
141+ } ,
142+ authorization : {
143+ payload : 'anyPayload' ,
144+ signatures : [ {
145+ protected : 'anyProtectedHeader' ,
146+ signature : 'anySignature'
147+ } ]
148+ }
149+ } ;
150+
151+ Message . validateJsonSchema ( validMessage ) ;
152+ } ) ;
153+
154+ it ( 'should throw if `protocolPath` contains invalid characters' , ( ) => {
127155 const invalidMessage = {
128156 recordId : 'anyRecordId' ,
129- contextId : 'someContext' , // protocol must exist
157+ contextId : 'someContext' ,
130158 descriptor : {
131159 interface : 'Records' ,
132160 method : 'Write' ,
133- protocol : 'someProtocolId' , // contextId must exist
161+ protocol : 'http://foo.bar' ,
162+ protocolPath : 'invalid:path' , // `:` is not a valid char in `protocolPath`
163+ schema : 'http://foo.bar/schema' ,
134164 dataCid : 'anyCid' ,
135165 dataFormat : 'application/json' ,
136166 dataSize : 123 ,
@@ -146,10 +176,12 @@ describe('RecordsWrite schema definition', () => {
146176 }
147177 } ;
148178
149- Message . validateJsonSchema ( invalidMessage ) ;
179+ expect ( ( ) => {
180+ Message . validateJsonSchema ( invalidMessage ) ;
181+ } ) . throws ( 'protocolPath: must match pattern "^[a-zA-Z]+(/[a-zA-Z]+)*$' ) ;
150182 } ) ;
151183
152- it ( 'should pass if `contextId` and `protocol` are both not present' , ( ) => {
184+ it ( 'should pass if none of `protocol` related properties are present' , ( ) => {
153185 const invalidMessage = {
154186 recordId : 'anyRecordId' ,
155187 descriptor : {
@@ -227,6 +259,94 @@ describe('RecordsWrite schema definition', () => {
227259 } ) . throws ( 'must have required property \'contextId\'' ) ;
228260 } ) ;
229261
262+ it ( 'should throw if `protocol` is set but `protocolPath` is missing' , ( ) => {
263+ const invalidMessage = {
264+ recordId : 'anyRecordId' ,
265+ contextId : 'anyContextId' , // required by protocol-based message
266+ descriptor : {
267+ interface : 'Records' ,
268+ method : 'Write' ,
269+ protocol : 'http://foo.bar' ,
270+ // protocolPath : 'foo/bar', // intentionally missing
271+ dataCid : 'anyCid' ,
272+ dataFormat : 'application/json' ,
273+ dataSize : 123 ,
274+ dateCreated : '2022-12-19T10:20:30.123456' ,
275+ dateModified : '2022-12-19T10:20:30.123456'
276+ } ,
277+ authorization : {
278+ payload : 'anyPayload' ,
279+ signatures : [ {
280+ protected : 'anyProtectedHeader' ,
281+ signature : 'anySignature'
282+ } ]
283+ }
284+ } ;
285+
286+ expect ( ( ) => {
287+ Message . validateJsonSchema ( invalidMessage ) ;
288+ } ) . throws ( 'descriptor: must have required property \'protocolPath\'' ) ;
289+ } ) ;
290+
291+ it ( 'should throw if `protocolPath` is set but `protocol` is missing' , ( ) => {
292+ const invalidMessage = {
293+ recordId : 'anyRecordId' ,
294+ contextId : 'anyContextId' ,
295+ descriptor : {
296+ interface : 'Records' ,
297+ method : 'Write' ,
298+ // protocol : 'http://foo.bar', // intentionally missing
299+ protocolPath : 'foo/bar' ,
300+ dataCid : 'anyCid' ,
301+ dataFormat : 'application/json' ,
302+ dataSize : 123 ,
303+ dateCreated : '2022-12-19T10:20:30.123456' ,
304+ dateModified : '2022-12-19T10:20:30.123456'
305+ } ,
306+ authorization : {
307+ payload : 'anyPayload' ,
308+ signatures : [ {
309+ protected : 'anyProtectedHeader' ,
310+ signature : 'anySignature'
311+ } ]
312+ }
313+ } ;
314+
315+ expect ( ( ) => {
316+ Message . validateJsonSchema ( invalidMessage ) ;
317+ } ) . throws ( 'descriptor: must have required property \'protocol\'' ) ;
318+ } ) ;
319+
320+ it ( 'should throw if `protocol` is set but `schema` is missing' , ( ) => {
321+ const invalidMessage = {
322+ recordId : 'anyRecordId' ,
323+ contextId : 'anyContextId' , // required by protocol-based message
324+ descriptor : {
325+ interface : 'Records' ,
326+ method : 'Write' ,
327+ protocol : 'http://foo.bar' ,
328+ protocolPath : 'foo/bar' ,
329+ // schema : 'http://foo.bar/schema', // intentionally missing
330+ dataCid : 'anyCid' ,
331+ dataFormat : 'application/json' ,
332+ dataSize : 123 ,
333+ dateCreated : '2022-12-19T10:20:30.123456' ,
334+ dateModified : '2022-12-19T10:20:30.123456'
335+ } ,
336+ authorization : {
337+ payload : 'anyPayload' ,
338+ signatures : [ {
339+ protected : 'anyProtectedHeader' ,
340+ signature : 'anySignature'
341+ } ]
342+ }
343+ } ;
344+
345+ expect ( ( ) => {
346+ Message . validateJsonSchema ( invalidMessage ) ;
347+ } ) . throws ( 'descriptor: must have required property \'schema\'' ) ;
348+ } ) ;
349+
230350 it ( 'should throw if published is false but datePublished is present' , ( ) => {
231351 const invalidMessage = {
232352 recordId : 'anyRecordId' ,
0 commit comments