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.