Skip to content

Commit

Permalink
Add JSON message validation stream processor example
Browse files Browse the repository at this point in the history
  • Loading branch information
davegarvey committed Oct 4, 2024
1 parent 2878cae commit b2619eb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"x-tyk-api-gateway": {
"info": {
"dbId": "66fe500b1da0b00001da209c",
"dbId": "66ffabd12a4a5100016d007e",
"id": "02715dce83cb47ce59487abe573222f7",
"name": "Streams - Simple PubSub",
"orgId": "5e9d9544a1dcd60001d0ed20",
Expand Down Expand Up @@ -59,6 +59,44 @@
"ws_path": "/ws"
}
}
},
"Validated Pub": {
"input": {
"http_server": {
"allowed_verbs": [
"POST"
],
"path": "/validated-pub",
"timeout": "5s"
}
},
"output": {
"http_server": {
"stream_path": "/validated-sse"
}
},
"pipeline": {
"processors": [
{
"json_schema": {
"schema": "{\n \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\n \"type\": \"string\",\n \"minLength\": 5\n },\n \"age\": {\n \"type\": \"integer\",\n \"minimum\": 19\n }\n },\n \"required\": [\"name\", \"age\"],\n \"additionalProperties\": false\n\n}\n"
}
},
{
"catch": [
{
"log": {
"level": "ERROR",
"message": "JSON schema validated failed due to: ${!error()}"
}
},
{
"mapping": "root = deleted()"
}
]
}
]
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
echo "Streams - Validated PubSub"
echo "Part 1: Listening for validated server-sent events"
echo "This script connects to a server-sent event endpoint, and waits to receive messages."
echo "Use script 3 to send messages."

curl http://tyk-gateway.localhost:8080/streams-simple-pubsub/validated-sse
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
echo "Streams - Validated PubSub"
echo "Part 2: Publishing a valid message"
echo "This script publishes a valid message to the clients connected in script 1."
echo "Check the first terminal to see the messages received."

curl http://tyk-gateway.localhost:8080/streams-simple-pubsub/validated-pub -d '{ "name": "Tyk Streams", "age": 21 }'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
echo "Streams - Validated PubSub"
echo "Part 3: Publishing an invalid message"
echo "This script publishes an invalid message, which the gateway prevents subscribers from receiving."
echo "Check the first terminal to see that the message is not received, and the fourth terminal to see the logged error."

curl http://tyk-gateway.localhost:8080/streams-simple-pubsub/validated-pub -d '{ "name": "Tyk", "age": 3 }'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
echo "Streams - Validated PubSub"
echo "Part 4: Displaying errors from the application log"
echo "This script watches the Tyk Gateway application log for 'Schema validation failed' messages.".
echo "It will display the error message generated by the invalid request sent by script 3."

docker logs -f tyk-demo-tyk-gateway-1 2>&1 | grep -E "@service=benthos.*path=root.pipeline.processors.1.catch.0"

0 comments on commit b2619eb

Please sign in to comment.