Episode completed

Next epsiode starting in 3 seconds

Published at

Script

Our blog can now display blog-posts with formatting through markdown. If you were to create a blogpost with invalid markdown though, we just get an error message when reading the post. To prevent this, we can add a custom validation for the body. To do that, we need to go into the Controller for the Posts. There, we need to import MMark again to use the parser for validation. Then we can write a custom validator. First, we need to set the type of the Function. This takes text and returns a ValidatorResult, which is used by the validateField function later. To get a result, we can just try to parse the given text and return Success if everything works correctly and a Failure with an error message if the parse fails. Now, we can add a validateField statement to the buildPost function. This checks the post-body with our validator function. We can now test the validator by creating a post with invalid markdown. We can see our custom error message working. If we now input correct markdown, the post can be created and is rendered correctly.

Discussion