You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Just wanted to bring attention to a great tool that can greatly increase our confidence while processing events.
For those who don't know, Zod lets us create a schema and then validate an object against that schema (plus inferring Typescript type as a bonus)
So for example in BFF template when we materialize upstream thing, in models/thing.js we could do:
import{z}from'zod'constthingEventSchema=z.object({type: z.enum(['thing-submitted','thing-resubmitted','thing-rejected','thing-approved']),thing: z.object({id: z.string(),name: z.string(),status: z.enum(['SUBMITTED','RESUBMITTED','REJECTED','APPROVED']).optional()}),timestamp: z.number(),})exportconsttoUpdateRequest=(uow)=>{constevent=thingEventSchema.parse(uow.event)// will throw if the event doesn't satisfy the schema// Now we can be sure that we're working with the thing we are expecting to work with so we can proceed with confidence// plus, `event` is now typed, and even if we don't use Typescript, we get Intellisense suggestions for properties of `event`return{Key: {pk: event.thing.id,sk: DISCRIMINATOR,},
...updateExpression({
...event.thing,status: EVENT_STATUS_MAP[event.type]||event.thing.status,
...
This also forces us to write tests that resemble real events
We can also validate outgoing events, for example in high responsibility services like ESG.
To summarize, a great tool that can help us in distributed event driven architecture.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hey,
Just wanted to bring attention to a great tool that can greatly increase our confidence while processing events.
For those who don't know, Zod lets us create a schema and then validate an object against that schema (plus inferring Typescript type as a bonus)
So for example in BFF template when we materialize upstream thing, in models/thing.js we could do:
This also forces us to write tests that resemble real events
We can also validate outgoing events, for example in high responsibility services like ESG.
To summarize, a great tool that can help us in distributed event driven architecture.
Beta Was this translation helpful? Give feedback.
All reactions