@@ -46,7 +46,15 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
46
46
schemaurl ?: string ;
47
47
datacontentencoding ?: string ;
48
48
49
- constructor ( event : CloudEventV1 | CloudEventV1Attributes | CloudEventV03 | CloudEventV03Attributes ) {
49
+ /**
50
+ * Creates a new CloudEvent object with the provided properties. If there is a chance that the event
51
+ * properties will not conform to the CloudEvent specification, you may pass a boolean `false` as a
52
+ * second parameter to bypass event validation.
53
+ *
54
+ * @param {object } event the event properties
55
+ * @param {boolean? } strict whether to perform event validation when creating the object - default: true
56
+ */
57
+ constructor ( event : CloudEventV1 | CloudEventV1Attributes | CloudEventV03 | CloudEventV03Attributes , strict = true ) {
50
58
// copy the incoming event so that we can delete properties as we go
51
59
// everything left after we have deleted know properties becomes an extension
52
60
const properties = { ...event } ;
@@ -105,20 +113,20 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
105
113
for ( const [ key , value ] of Object . entries ( properties ) ) {
106
114
// Extension names should only allow lowercase a-z and 0-9 in the name
107
115
// names should not exceed 20 characters in length
108
- if ( ! key . match ( / ^ [ a - z 0 - 9 ] { 1 , 20 } $ / ) ) {
116
+ if ( ! key . match ( / ^ [ a - z 0 - 9 ] { 1 , 20 } $ / ) && strict ) {
109
117
throw new ValidationError ( "invalid extension name" ) ;
110
118
}
111
119
112
120
// Value should be spec compliant
113
121
// https://github.com/cloudevents/spec/blob/master/spec.md#type-system
114
- if ( ! isValidType ( value ) ) {
122
+ if ( ! isValidType ( value ) && strict ) {
115
123
throw new ValidationError ( "invalid extension value" ) ;
116
124
}
117
125
118
126
this [ key ] = value ;
119
127
}
120
128
121
- this . validate ( ) ;
129
+ strict ? this . validate ( ) : undefined ;
122
130
123
131
Object . freeze ( this ) ;
124
132
}
@@ -187,6 +195,7 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
187
195
/**
188
196
* Clone a CloudEvent with new/update attributes
189
197
* @param {object } options attributes to augment the CloudEvent with
198
+ * @param {boolean } strict whether or not to use strict validation when cloning (default: true)
190
199
* @throws if the CloudEvent does not conform to the schema
191
200
* @return {CloudEvent } returns a new CloudEvent
192
201
*/
@@ -198,7 +207,8 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
198
207
| CloudEventV03
199
208
| CloudEventV03Attributes
200
209
| CloudEventV03OptionalAttributes ,
210
+ strict = true ,
201
211
) : CloudEvent {
202
- return new CloudEvent ( Object . assign ( { } , this . toJSON ( ) , options ) as CloudEvent ) ;
212
+ return new CloudEvent ( Object . assign ( { } , this . toJSON ( ) , options ) as CloudEvent , strict ) ;
203
213
}
204
214
}
0 commit comments