<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David Virgil on Medium]]></title>
        <description><![CDATA[Stories by David Virgil on Medium]]></description>
        <link>https://medium.com/@david.virgil.naranjo?source=rss-4d65e0dbc41------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HrV3HB2oV9RbVCwj</url>
            <title>Stories by David Virgil on Medium</title>
            <link>https://medium.com/@david.virgil.naranjo?source=rss-4d65e0dbc41------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 23:43:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@david.virgil.naranjo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Kafka Blue-Green Deployment]]></title>
            <link>https://medium.com/expedia-group-tech/kafka-blue-green-deployment-212065b7fee7?source=rss-4d65e0dbc41------2</link>
            <guid isPermaLink="false">https://medium.com/p/212065b7fee7</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[blue-green-deployment]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[David Virgil]]></dc:creator>
            <pubDate>Thu, 05 Dec 2019 14:01:07 GMT</pubDate>
            <atom:updated>2019-12-05T15:08:48.186Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://medium.com/expedia-group-tech/software/home">EXPEDIA GROUP TECHNOLOGY — SOFTWARE</a></h4><h4>How to deploy a new version of a Kafka cluster in a production environment using a blue-green deployment</h4><figure><a href="https://kafka.apache.org/"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cgnSul9FpbwDssHWH1rnDA.png" /></a><figcaption>Image source: <a href="https://kafka.apache.org/">https://kafka.apache.org</a></figcaption></figure><p>One of the most challenging and error prone tasks that I have faced as a developer, is performing software/infrastructure upgrades in a production <a href="https://kafka.apache.org/">Apache Kafka </a>cluster. This post will explore two different approaches that we have used while working on Expedia Partner Solutions™ (Part of <a href="https://www.expediagroup.com/">Expedia Group</a>™) data lake.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vsv7RejfqSHcIc2Q2awEOg.png" /></figure><h3>Batch deployment</h3><p>The first approach I took was to make partial updates of the cluster and then doing reassignment of partitions. This approach involves batch updates of the production cluster:</p><ol><li>Moving all the traffic from one batch of nodes to the rest of the nodes (the intact ones). To do this you have to use a Kafka command to reassign partitions.</li><li>Updating the batch while the rest of the nodes remain the same and all the traffic has been redirected to them.</li><li>Doing the same with the rest of the batches, reassigning partitions and installing the new software/hardware.</li></ol><p>As you can see this process is quite manual and dangerous. One of the consequences of this approach is that the Kafka cluster would have more load and it would incur into several topic rebalances.</p><p>For example, imagine that you have a production Kafka cluster of 100 brokers. Maybe you have to use update batches of 10 nodes for not affecting the incoming Kafka traffic. This process would involve 10 reassignment of partitions and 10 batch updates.</p><p>If you’re interested to know more about Kafka batch deployment and topic rebalancing I have covered it in detail in <a href="https://dvirgiln.github.io/scale-up-and-update-a-kafka-cluster/">another blog post</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yFE9WqsKv9M0PPr9-gjq0w.png" /></figure><h3>Blue-green Kafka deployment</h3><p>On the other hand, the blue-green deployment technique minimizes the risks of the batching approach explained previously. It consists of creating a group of new instances and when the new instances are ready, the producer traffic is switched over to the new cluster.</p><p>The following diagram shows the architecture to implement.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/0*6ibqMIFRkPqaWSGi.jpg" /><figcaption>Kafka Blue-green architecture in a AWS environment.</figcaption></figure><p>The main idea behind it is to have two Kafka clusters running in parallel and switch the traffic by updating the Route53 DNS record to point to the new cluster. The old cluster is not removed immediately. It is necessary to wait until all the traffic from the producers has been switched and all the consumers have processed all the topics. This means that for all the topics in the old Kafka cluster there is zero lag.</p><h4>Why we did not use AWS CodeDeploy</h4><p>AWS CodeDeploy has an option to make blue-green deployments. It allows deploying a new version of your code/application without affecting users. So, it deploys the code in new instances and once all the instances are ready it unsubscribes the old instances from the load balancer and subscribes the new instances to the autoscaling group.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/0*w8jnPCvAEih08-MS.png" /><figcaption>Creation of Codedeploy deployment in AWS.</figcaption></figure><p>Once the instances are being deregistered from the load balancer, you can choose to terminate them or keep them.</p><p>CodeDeploy works fine when a new version of the application is being deployed. The blue-green deployment really does what it is expected, but in the case of infrastructure changes, CodeDeploy blue-green deployment is not helping much. When AWS CloudFormation makes a launch configuration update to an existing Autoscaling group the existing EC2 instances are terminated and new ones are created. And obviously this is something that is not acceptable in a stateful service like Kafka, so for this reason we do not use CodeDeploy for Kafka deployments.</p><p>It’s worth mentioning though that CodeDeploy has many interesting features including all-at-once / one-by-one deployments, rollbacks and more. A full discussion is beyond the scope of this article so I will write about it in a future post.</p><h4>Blue-green implementation</h4><p>Blue-green deployment requires us to deploy a new Kafka cluster in production. In order to be able to deploy two different Kafka clusters in production with the same CloudFormation codebase, we modified our CloudFormation code to include a function suffix in the name of all of our resources.</p><p>Kafka has a direct dependency on Zookeeper. Zookeeper contains information about the partitions, brokers, partition leaders… Obviously the Zookeeper connect url has to be different between the two Kafka clusters. The only thing that needs to be changed is the property zookeeper.connect inside of server.properties:</p><pre>zookeeper.connect=${Comma separated list of hostnames}/${zook_path}</pre><p>Zookeeper allows using the same Zookeeper cluster by different Kafka clusters. The only thing that has to be modified is the zook_path at the end of the Zookeeper connection.</p><p>The following diagram shows the architecture of a Kafka cluster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*O4qpEdpD9u8Uh60B7oFl-w.png" /><figcaption>Multi AZ Kafka Cluster Architecture in AWS</figcaption></figure><p>As you can see there is a load balancer that balances the traffic across all the Kafka brokers. There is as well a Route53 DNS record that points to the Network Load Balancer. For switching the traffic for one cluster to another, just change the Route53 DNS endpoint to point to the new Kafka cluster Load Balancer. Check out the following diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/0*5Ao4CAyCDjafqHgR.jpg" /></figure><p>It is important to set up the Route53 TTL property accordingly. The Time To Live property allows clients to maintain cached the Route53 endpoint IP address for a time. If the TTL property is set to 10 minutes, in case of a DNS endpoint change, it won’t be effective at least until the TTL has been expired. The next image shows how to configure the TTL in a AWS Route 53:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*ybct7cQUige94tEz0-hzzw.png" /><figcaption>Route54 CName form.</figcaption></figure><p>It is important to understand how the TTL works and how the Kafka client library can be affected. If the TTL is 100 seconds, then the operating system caches the IP address associated and it always responses with the cached IP address during the TTL.</p><h4>Producer code changes</h4><p>Normally the Kafka producers do not create a new connection to Kafka every time they send a new record to Kafka. The producers implement a singleton pattern meaning that a single KafkaClient instance is created when the producer service starts up. With this kind of producer implementation, even if the DNS endpoint has been switched to the new Kafka cluster, producers are not aware of this change, until the service is redeployed or restarted. To solve this problem, we use a scheduled thread that periodically replaces the producer Kafka client connection:</p><pre>public class BlueGreenKafkaProducer{<br>  scheduledService = Executors.newScheduledThreadPool(1);<br>  runnable = new BlueGreenKafkaSwitcher(refreshTime, this);<br>  scheduledService.scheduleAtFixedRate(runnable, INITIAL_DELAY, PERIOD, TimeUnit.MILLISECONDS);</pre><pre>protected void switchClient(){<br>      if(inUse == null){<br>          logger.info(&quot;Initializing Blue Kafka Producer&quot;);<br>          blue = new KafkaProducer(producerProperties);<br>          inUse = BlueGreenKafkaEnum.BLUE;<br>      }<br>      else{<br>          logger.info(&quot;Kafka Producer Switch&quot;);<br>          if(inUse == BlueGreenKafkaEnum.BLUE){<br>              logger.info(&quot;Switching from Kafka Producer Blue to Green&quot;);<br>              green = new KafkaProducer(producerProperties);<br>              inUse = BlueGreenKafkaEnum.GREEN;<br>              blue.close();<br>              blue = null;</pre><pre>          }<br>          else{<br>              logger.info(&quot;Switching from Kafka Producer Green to Blue&quot;);<br>              blue = new KafkaProducer(producerProperties);<br>              inUse = BlueGreenKafkaEnum.BLUE;</pre><pre>              green.close();<br>              green = null;          <br>          }<br>      }<br>  }</pre><pre>   private KafkaProducer getProducer(){<br>        switch (inUse){<br>            case BLUE: return blue;<br>            case GREEN: return green;<br>            default: return blue;<br>        }<br>    }</pre><pre><br>    public boolean send(ProducerRecord record){<br>        return getProducer().send(record);<br>    }</pre><pre>public boolean send(Iterable&lt;? extends ProducerRecord&gt; records){<br>        return getProducer().send(records);<br>    }<br>}</pre><p>In the code snippet above:</p><ul><li>The second line creates a new Scheduled Thread.</li><li>In the third line we define the Runnable object that will contain the logic to switch the Kafka Client.</li><li>On the fourth line we schedule the runnable to run at fixed intervals.</li><li>The seventh line shows the switcher method. This method is being called by the the Runnable instance that will make the client switch.</li><li>It is important to close the previous connections to avoid keep opening threads all the time.</li></ul><pre>public class BlueGreenKafkaSwitcher implements Runnable {<br>  public void run() { <br>    bluegreen.switchClient();<br>  }<br>}</pre><h4>Configuration</h4><p>Our library every n seconds has to create a new connection to Kafka to check if the endpoint has changed. The previous piece of code does this, but we need to find the right scheduling period. I would say that every 30 minutes is fine. So every 30 minutes the switcher will be called.</p><p>Remember to set a scheduler period greater than the TTL, because in the other case it could happen that the switcher would change to the cached endpoint value.</p><h4>Deployment steps</h4><p>Doing a Blue-green Kafka deployment requires a series of steps:</p><ol><li>Include the library changes and make sure that all the producers include this library in their classpath.</li><li>Deploy a new Kafka cluster that contains some infrastructure/software upgrades.</li><li>Deploy the connectors/consumers for the new Kafka cluster.</li><li>Modify the original Route53 to point to the load balancer of the upgraded Kafka cluster.</li><li>Wait until all the producers have switched their traffic (depends on the switcher period property).</li><li>Wait until all the consumers from the initial Kafka cluster have zero lag. This means that they have consumed all the data.</li><li>At this point we can delete the old Kafka cluster.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yFE9WqsKv9M0PPr9-gjq0w.png" /></figure><h3>Conclusion</h3><p>Kafka blue-green deploys have a lot of moving parts, but are still less complicated than Kafka batch deploys as we don’t have to worry about topic rebalancing and service availability. Because blue-green is easier to automate our deployments are more reliable and there is less chance of making a human error. Following the approach above will ensure zero downtime and zero data loss.</p><p><em>Originally published at </em><a href="https://dvirgiln.github.io/kafka-blue-green-deployment/"><em>https://dvirgiln.github.io</em></a><em>.</em></p><figure><a href="https://lifeatexpedia.com/brands"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5tdXqz9KfPYEgPG9thFk9w.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=212065b7fee7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/expedia-group-tech/kafka-blue-green-deployment-212065b7fee7">Kafka Blue-Green Deployment</a> was originally published in <a href="https://medium.com/expedia-group-tech">Expedia Group Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Applicative and Validated Scala Cats: Nested Form Validation]]></title>
            <link>https://medium.com/expedia-group-tech/applicative-and-validated-scala-cats-nested-form-validation-f7741567fe2e?source=rss-4d65e0dbc41------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7741567fe2e</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[monads]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[David Virgil]]></dc:creator>
            <pubDate>Fri, 29 Nov 2019 14:01:01 GMT</pubDate>
            <atom:updated>2019-11-29T14:01:01.649Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://medium.com/expedia-group-tech/software/home">EXPEDIA GROUP TECHNOLOGY — SOFTWARE</a></h4><h4>Usage of the Cats library Applicative and Validated type classes to perform multilevel form validation. So much fun guaranteed…</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*eO2dh8OGZDYUlKLcrUrfLA.png" /><figcaption>Image source: Typelevel</figcaption></figure><p>A quick search on Google will return several posts on form validation using the Scala <a href="https://typelevel.org/cats/">Cats library</a>. It’s difficult though to find a good example of nested form validation with Cats. In this post we show how to compose Cats Applicative and Validated functors to ensure all elements of a form will be validated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AZpg_TdELqc_lgxbZedekg.png" /></figure><p>The Applicative Cats<em> type class</em> is well suited to form validation because of its parallel computation nature. While in a monad everything is computed sequentially, in an Applicative functor the computation is not restricted to be executed in any particular order. It is executed in parallel. While in a monad if during the execution of the monad it fails, the execution is stopped, in an applicative due to its nature, the computation continues.</p><p>With Applicatives we are able to return all the errors that one form has, while if we would use a monad to try the same, we could just return the first error. That’s why Applicative functors are so useful for form validation. The validation of every field can be done in parallel and at the end the result of the validation can be joined and merged at the end of the process.</p><p>The goal of this post is to show how to implement a nested form validation using <em>Cats</em>. The majority of the examples you can find online are just a one-level form, like for instance an Address:</p><pre>case class <strong><em>Address</em></strong>(street: String, number: String, postalCode: String, county: Option[String], country: String)</pre><p>But in the real world forms are quite complex, and normally the structures contain three or four nested objects. For instance:</p><pre>case class <strong><em>Property</em></strong>(listing: Listing)<br>case class <strong><em>Listing</em></strong>(id: Option[String], contact: Contact, address: Address, location: GeoLocation)<br>case class <strong><em>Contact</em></strong>(phone: String, formattedPhone: String)<br>case class <strong><em>Address</em></strong>(address: String, postalCode: String, countryCode: String, city: String, state: Option[String], country: String)<br>case class <strong><em>GeoLocation</em></strong>(lat: String, lng: String)</pre><p>In the previous code it has been modeled a hotel property listing. Obviously we could have modeled a form validator based on one big validator that validates all the different values of the nested elements. But the solution is not quite clean and it is quite coupled. For instance the address case class could be used in other parts of our code as part of another class.</p><p>Let’s review what it is exactly the syntax of the Applicative functor:</p><pre>trait <strong><em>Applicative</em></strong>[F[_]] extends <strong><em>Apply</em></strong>[F]{<br> def pure[A](x: A): F[A]<br> override def map[A, B](fa: F[A])(f: A =&gt; B): F[B] = ap(pure(f))(fa)<br>}</pre><p>It contains 2 functions:</p><ul><li><strong><em>Pure</em></strong> containerize an object of type A into F[A]. If our applicative is applied over the monad List, it will add the element to the List. The same applies to Option or Future.</li><li><strong><em>Map</em></strong>: as applicative is a functor it should have the map operation. We can see that to implement the map operation it has been done in terms of <em>pure</em> and ap. The operation ap is the most important operation of Applicative. It is implemented in the class Apply that this class is mixing.</li></ul><p>This is the syntax of the Apply trait:</p><pre>trait <strong><em>Apply</em></strong>[F[_]] extends <strong><em>Functor</em></strong>[F] {<br> def <strong><em>ap</em></strong>[A, B](ff: F[A =&gt; B])(fa: F[A]): F[B]<br>}</pre><p>As we can see it has two curried parameters. The first contains a function inside of our container F[A =&gt;B] and the second is the containerized first element of the function. First time you read this syntax it could seem a cryptic, but you will see the usage more clearly once we start with our example.</p><h4>Let’s do it!</h4><p>We want to create a composite validator using Cats for the following form:</p><pre>case class Property(listing: Listing)<br>case class Listing(id: Option[String], contact: Contact, address: Address, location: GeoLocation)<br>case class Contact(phone: String, formattedPhone: String)<br>case class Address(address: String, postalCode: String, countryCode: String, city: String, state: Option[String], country: String)<br>case class GeoLocation(lat: String, lng: String)</pre><p>We can start with the implementation of the AddressValidator:</p><pre><br>def validateAddress(address: String): F[String] = address.pure[F]</pre><pre>def validatePostalCode(postalCode: String): F[String] = postalCode.pure[F]</pre><pre>def validateCountryCode(countryCode: String): F[String] =<br>   if (countryCode.size == 2) countryCode.pure[F]<br>   else   <br>A.raiseError(mkError(AddressNonCorrectCountryCodeError))</pre><pre>def validateState(address: Address): F[Option[String]] = {<br>        if (address.countryCode == &quot;US&quot;) address.state match {<br>          case None =&gt; A.raiseError(mkError(AddressShouldContainStateError))<br>          case Some(_) =&gt; address.state.pure[F]<br>        }<br>        else {<br>          address.state match {<br>            case None =&gt; address.state.pure[F]<br>            case Some(_) =&gt; A.raiseError(mkError(AddressShouldNotContainStateError))<br>          }<br>        }<br>}</pre><pre><br>def validateCity(city: String): F[String] =<br>        if (city.isEmpty)<br>          A.raiseError(mkError(AddressCityIsEmptyError))<br>        else<br>          city.pure[F]</pre><pre><br>def validateCountry(country: String): F[String] =<br>        if (country.isEmpty)<br>          A.raiseError(mkError(AddressCountryIsEmptyError))<br>        else<br>          country.pure[F]</pre><pre><br>def <strong><em>validate</em></strong>(address: Address): F[Address] = {<br>        (Address.apply _).curried.pure[F].<br>          ap(validateAddress(address.address)).<br>          ap(validatePostalCode(address.postalCode)).<br>          ap(validateCountryCode(address.countryCode)).<br>          ap(validateCity(address.city)).<br>          ap(validateState(address)).<br>          ap(validateCountry(address.country))<br>}</pre><p>Considerations of the previous code:</p><p>- validate<strong> </strong>function: it receives an address as a parameter and it returns a monad of address, in this case we do not know the kind of monad until this class is instantiated.</p><p>-(Address.apply _).curried.pure[F]: we are building an address as a curried nested function of parameters. What (Address.apply _).curried does? It converts a constructor case class Address into a nested function:</p><pre>case class Address(address: String, postalCode: String, countryCode: String, city: String, state: Option[String], country: String)</pre><p>to</p><pre>String =&gt; String =&gt; String =&gt;String =&gt; Option[String] =&gt; String =&gt; Address</pre><p>Magic? No, Scala.</p><p>- Continuation of code comments: so, we have a nested function and we applied the pure function from Applicative, so we have a:</p><pre>F[String =&gt; String =&gt; String =&gt;String =&gt; Option[String] =&gt; String =&gt; Address]</pre><p>- Looks like the first parameter of the ap function from Aplicative class, isn’t it?<br>- The lines after the curried constructor: we are able to apply the method ap to the function F[String =&gt; String …] and construct our F[Address].<br>- You can realize that when we construct our F[Address]<strong> </strong>we are calling a method validateCountry, validatePostalCode… These functions check the validity of each attribute and return a F[]. <br>- In all the validation methods, you can see that to construct a valid value we just need to call the F[A].pure to containerize the value.<br>- In case that there is an error, we can raise it using A.raiseError(mkError) <strong><em>.</em></strong></p><p>In the AddressValidator we have seen the root of our validators, but what about the validator that is a composition of other validators. We need to make use of another class provided by Cats called <strong><em>Validated</em></strong>. Let’s understand Validated first:</p><pre>sealed abstract class <strong><em>Validated</em></strong>[+E, +A] extends Product with Serializable {<br>def fold[B](fe: E =&gt; B, fa: A =&gt; B): B =<br> this match {<br> case Invalid(e) =&gt; fe(e)<br> case Valid(a) =&gt; fa(a)<br> }<br>def isValid: Boolean = fold(_ =&gt; false, _ =&gt; true)<br> def isInvalid: Boolean = fold(_ =&gt; true, _ =&gt; false)</pre><p>Validated is not a monad. If you try to execute the <em>flatMap</em> operation, you can see that doesn’t exist. Validated is a type class that computes if an object is valid or invalid. It can be used in conjunction with the Applicative type class to allow accumulation of errors. In other cases we may want to have a monadic validation, and fail with the first error. In that case we could use the <em>Either</em> monad.</p><p>Validated has 2 type parameters E and A. The E type correspond to the kind of error that is returned in case of error. The A type parameter correspond to the return type in case of a valid response. Similar to the Either type parameters.</p><p>To help us, Cats provides an extension type of the main Validated type, that we are going to use in our example:</p><pre>type ValidatedNel[+E, +A] = Validated[NonEmptyList[E], A]</pre><p>The cats.data.ValidatedNel models the error accumulation use case, that it is exactly what we need.</p><p>We can see part of the implementation of the ListingValidator.</p><pre>def validateId(idOpt: Option[String], emptyID: Boolean): Validated[PropertyValidationError, Option[String]] = {<br> emptyID match {<br> case true =&gt; idOpt match {<br> case None =&gt; Valid(idOpt)<br> case Some(_) =&gt; Invalid(IdNonEmptyError)<br> }<br> case false =&gt; idOpt match {<br> case None =&gt; Invalid(IdEmptyError)<br> case Some(id) =&gt; Try(UUID.fromString(id)).isSuccess match {<br> case true =&gt; Valid(idOpt)<br> case false =&gt; Invalid(NonUUIDFormatError)<br> }<br> }<br> }<br>}</pre><pre>def validateGeoLocation(location: GeoLocation): Validated[PropertyValidationError, GeoLocation] = {<br> val tryLat = Try(location.lat.toDouble)<br> val tryLong = Try(location.lng.toDouble)<br> if (tryLat.isSuccess &amp;&amp; tryLong.isSuccess)<br> Valid(location)<br> else Invalid(GeoLocationNonExistingError)<br> }</pre><pre>def validate(listing: Listing, emptyID: Boolean): ValidatedNel[PropertyValidationError, Listing] = {<br>        Apply[ValidatedNel[PropertyValidationError, ?]].map4(<br>          validateId(listing.id, emptyID).toValidatedNel,<br>          ContactValidator.validate(listing.contact),<br>          AddressValidator.validate(listing.address),<br>          validateGeoLocation(listing.location).toValidatedNel<br>        ) {<br>            case (id, contact, address, location) =&gt; Listing(id, contact, address, location)<br>          }<br>}</pre><p>Considerations about the previous code:<br>- This validator contains inline validations, like the geolocation validation and the id validation.<br>- It is composed by other validators classes, like the ContactValidator or the AddressValidator.<br>- The validate function is the main function of the validation. It creates an Applicative using the Apply constructor. The container in this case is ValidatedNel, to aggregate PropertyValidationErrors. <br>- We can see that it is applied the function map4, that belongs to SemiGroupal. SemiGroupal captures the idea of composing independent effectful values. This is exactly what we need in our application. <br>- In the validateGeoLocation returns a Valid or Invalid value. We can see that the function returns a Validated type.</p><h4>Source Code</h4><p>The complete source code can be found <a href="https://github.com/dvirgiln/listing-api">here</a>. The source code contains a Rest API that uses the Cats form validation described above. It also contains JUnit tests that validate the different parts of the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GrkYzPHO_yjqYhHlCwfEvg.png" /></figure><h4>Conclusions</h4><p>Cats, Monads, Applicative, Semigroupal… all of this could seem a bit cryptic and chaotic at first. With suitable application however, it can help you to solve complex scenarios.</p><p>I hope this post will inspire you to start using Cats in your codebase. If you’d like to learn more I highly recommend reading <a href="https://underscore.io/books/scala-with-cats/">Scala with Cats</a> by Underscore. The book is comprehensive and explains well the subtleties of using Cats.</p><p><em>Originally published at </em><a href="https://dvirgiln.github.io/kafka-blue-green-deployment/"><em>https://dvirgiln.github.io</em></a><em>.</em></p><figure><a href="https://lifeatexpedia.com/brands"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5tdXqz9KfPYEgPG9thFk9w.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7741567fe2e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/expedia-group-tech/applicative-and-validated-scala-cats-nested-form-validation-f7741567fe2e">Applicative and Validated Scala Cats: Nested Form Validation</a> was originally published in <a href="https://medium.com/expedia-group-tech">Expedia Group Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>