Adding Validation to a Component

Let’s handle that NameVideo command in the video-publishing Component:

 function​ createHandlers ({ messageStore }) {
 return​ {
  NameVideo: command => {
const​ context = {
  command: command,
  messageStore: messageStore
  }
 
 return​ Bluebird.resolve(context)
.then(loadVideo)
  .then(ensureCommandHasNotBeenProcessed)
  .then(ensureNameIsValid)
  .then(writeVideoNamedEvent)
.​catch​(CommandAlreadyProcessedError, () => {})
.​catch​(
  ValidationError,
  err => writeVideoNameRejectedEvent(context, err.message)
  )
  },
 // ...
  }
 }

First, build a context consisting of the command in question and the messageStore.

Get Practical Microservices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.