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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.