diff --git a/docs/spec/broker.md b/docs/spec/broker.md index 34db7f9cb94..ea46ad9c858 100644 --- a/docs/spec/broker.md +++ b/docs/spec/broker.md @@ -40,10 +40,45 @@ default Broker upon creation if no Broker is specified by the user. A Trigger MAY be created before its assigned Broker exists. A Trigger SHOULD progress to Ready when its assigned Broker exists and is Ready. +#### Event filters + +Implementations of Trigger MUST support both attributes filter and jsExpression +filter. + The attributes filter specifying a list of key-value pairs MUST be supported by Trigger. Events that pass the attributes filter MUST include context or extension attributes that match all key-value pairs exactly. +The jsExpression filter specifying a Javascript expression MUST be supported by +Trigger. Implementations MUST support evaluation of Javascript expressions +following the standard +[EcmaScript 5.1](http://www.ecma-international.org/ecma-262/5.1/), although they +MAY support [EcmaScript 6](http://www.ecma-international.org/ecma-262/6.0/) or +greater. If the evaluation of the expression returns a Javascript value +coercible to the boolean `true`, then the trigger filter MUST pass the event. +Implementations MUST make the event accessible, within the expression, through +the variable `event` and its context attributes and extensions through the +Javascript dot notation `.`. For example the `id` attribute of a CloudEvent +should be accessible with `event.id`. Implementations MUST coerce CloudEvent +types to JS types. For example the value of an attribute with type `Timestamp` +should be translated to the JS type `Date`. + +An example jsExpression is: + +```js +event.id.indexOf("francesco") != -1 && + event.time != null && + event.time.getFullYear() >= 2020 && + event.exta != null; +``` + +This expression MUST pass the event iff the event id contains the string +`francesco`, event time has a year greater or equal to 2020 and event contains +an extension named `exta`. + +If a Trigger specify both attributes and jsExpression filters, then the event +MUST pass only and only if both filters pass the event. + ## Data Plane ### Ingress diff --git a/docs/spec/spec.md b/docs/spec/spec.md index 232e76e8e9d..8435214efec 100644 --- a/docs/spec/spec.md +++ b/docs/spec/spec.md @@ -243,6 +243,7 @@ retrieved from ref. ### TriggerFilter -| Field | Type | Description | Constraints | -| ---------- | ----------------- | ----------------------------------------------------------------------------------- | ----------- | -| attributes | map[string]string | A filter specifying which events match this trigger. Matches exactly on the fields. | | +| Field | Type | Description | Constraints | +| ------------ | ----------------- | ------------------------------------------------------------------------------------------------------------------------- | ----------- | +| attributes | map[string]string | A filter specifying which events match this trigger. Matches exactly on the fields. | | +| jsExpression | string | Javascript expression used for filtering events. If not specified, the behaviour is the same as an expression always true | |