<?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 Ömer Yalhı on Medium]]></title>
        <description><![CDATA[Stories by Ömer Yalhı on Medium]]></description>
        <link>https://medium.com/@oyalhi?source=rss-986ba31b80ae------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iZzjIn_fzYBtKUcH1s68xg.jpeg</url>
            <title>Stories by Ömer Yalhı on Medium</title>
            <link>https://medium.com/@oyalhi?source=rss-986ba31b80ae------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 21:23:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@oyalhi/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[Updating an Object’s Properties using TypeScript]]></title>
            <link>https://medium.com/@oyalhi/updating-an-objects-properties-using-typescript-80ba7edd0084?source=rss-986ba31b80ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/80ba7edd0084</guid>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Ömer Yalhı]]></dc:creator>
            <pubDate>Sat, 04 May 2019 19:53:52 GMT</pubDate>
            <atom:updated>2019-05-27T21:10:42.223Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EUBXmOVl8F6RmVx5Inj_TQ.png" /></figure><h3>Backend Data Validation Using Typescript Interfaces</h3><p><strong>The Problem</strong></p><p>When writing a REST API, a common problem I face is validating and updating an object. I need to make sure the update object has only the certain ‘allowed’ properties so that I don’t inadvertently change protected properties, such as a password or any other field that I don’t want it to change.</p><p>I usually do this by checking each property manually and updating the object accordingly. However, I wanted a better, more re-usable approach, so that I don’t have to manually check each property. In this article, I try to explain the steps and the final solution that I’ve come up with.</p><p>Let’s assume we have the following User interface:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2483e63387fbb2577bdd7ae1d1b77434/href">https://medium.com/media/2483e63387fbb2577bdd7ae1d1b77434/href</a></iframe><p><strong>Approach #1 (buggy)</strong></p><p>Let’s assume that we have a REST API where it allows update to the user. Updatable properties are `firstName`, `lastName` and `birthYear`. We could write the route as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bef5f29b1576b593c7e2c0cc61eb6bc2/href">https://medium.com/media/bef5f29b1576b593c7e2c0cc61eb6bc2/href</a></iframe><p>Here is what is happening:</p><p>1. find the user via the `id` from the `params`</p><p>2. if not found, send 404 and return</p><p>3. get the requested properties to change, which are in `req.body`</p><p>4. create a new object that updates the properties with the `json` objects’ keys. Here we have a problem though. The new updated user will add any property that is sent via the JSON object or update a property that is not allowed, such as a password. For example, if the JSON object is the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7634f26828785be2861468c28c521fb0/href">https://medium.com/media/7634f26828785be2861468c28c521fb0/href</a></iframe><p>our code will inadvertently change the password and add a new property `homeTown` which doesn’t exist in our interface. Which is far from ideal let’s say.</p><p>5. update users array with the new user object</p><p>6. send back the updated user</p><p><strong>Approach #2</strong></p><p>A better approach would be to change only updatable properties:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52717221bfa2a5e68f9ebcfec3099b64/href">https://medium.com/media/52717221bfa2a5e68f9ebcfec3099b64/href</a></iframe><p>1. find the user via the `id` from the `params`</p><p>2. if not found, send 404 and return</p><p>3. get the requested properties to change, which are in `req.body`</p><p>4. if exists update each property with the new value, if not use the current value</p><p>5. send back the updated user</p><p>This code would run without bugs. However, what if we had many more properties? What if the properties need to change in the future? We need a more robust way to write it.</p><p><strong>Approach #3</strong></p><p>With the help of TypeScript let’s define an interface with the updatable properties of our user object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7679f5679d95abc351fa67479116e8d2/href">https://medium.com/media/7679f5679d95abc351fa67479116e8d2/href</a></iframe><p>What if we could have access to UpdateUserDto interface’s keys? We could then iterate the interface keys and update the matching key in the user object. With the help of a custom transformer, we can do just that!</p><p>The custom transformer we are going to use is, `ts-transformers-key`. It has 2 transformers, one takes the given type and outputs an array of its keys, very handy. The other is used to compile the `keys` function correctly, a necessary part of the library, but we don’t use it directly. I go into details on how to install the complete project step by step later below.</p><p>Here is the third version of our route:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4536e384b489d34cc344a9b9615921e8/href">https://medium.com/media/4536e384b489d34cc344a9b9615921e8/href</a></iframe><p>1. find the user via the `id` from the `params`</p><p>2. if not found, send 404 and return</p><p>3. get the requested properties to change, which are in `req.body`</p><p>4. get the all the keys that we are allowed to change, defined by the `UpdateUserDto` data transfer object</p><p>5. go over each allowed property, and if it matches the requested key, assign it. If not, then we use the original value. Here, going over the `UpdateUserDto` we are making sure that we are only changing the allowed properties. If the `json` object had a property, let’s say `password`, because it didn’t exist in the dto, it would be ignored.</p><p>6. send back the updated user</p><p><strong>Approach #4</strong></p><p>Usually in my apps, instead of just stripping out the illegal properties and using the legal properties, I would like to reject the request all-together. I feel that the front end should send the request correctly and not rely on the back end to fix it’s errors. Thus my approach would be the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1536205048eca313c3c0417eeb7733bc/href">https://medium.com/media/1536205048eca313c3c0417eeb7733bc/href</a></iframe><p>1. find the user via the `id` from the `params`</p><p>2. if not found, send 404 and return</p><p>3. get the requested properties to change, which are in `req.body`</p><p>4. get all the keys that we are allowed to change, defined by the `UpdateUserDto` data transfer object</p><p>5. from the requested JSON object, filter out the keys that don’t exist in the UpdateUserDto</p><p>6. length is more than 0 if there are keys that are not present in the DTO, the length would be a truthy value. if so, reject the request.</p><p>7. go over each allowed property, and if it matches the requested key, assign it. If not, then we use the original value. Here, going over the `UpdateUserDto` we are making sure that we are only changing the allowed properties. If the `json` object had a property, let’s say `password` because it didn’t exist in the DTO, it would be ignored.</p><p>8. send back the updated user</p><p>This was our final approach to the problem.</p><p><strong>Step By Step Project Setup</strong></p><p>Note that I am using Mac and fish shell (which I highly recommend), so your commands might differ slightly if you are using a different operating system or shell.</p><p>Clone the minimal Node / Express with TypeScript boilerplate. As the name suggests this is a minimal Node / Express app using TypeScript, so I won’t get into details. You can clone or download the code here:</p><p><a href="https://github.com/oyalhi/typescript-node-express-minimal-boilerplate">https://github.com/oyalhi/typescript-node-express-minimal-boilerplate</a></p><p>After cloning or downloading, go into the project folder, install the dependencies and open your editor, here I’m using VS Code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4516593a8018a953f7042f99fa079539/href">https://medium.com/media/4516593a8018a953f7042f99fa079539/href</a></iframe><p>Let’s run the boilerplate in watch mode by simply running the `watch` script which simply runs `nodemon` that uses the `nodemon.json` config file in the root of the folder:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c09ed3e647687e857fa0f2c634a19b21/href">https://medium.com/media/c09ed3e647687e857fa0f2c634a19b21/href</a></iframe><p>If all went well, we should see the message that the server is listening on port 3000. We can exit the app and install the necessary libraries.</p><p>First we need to install the custom transformer, `ts-transformers-key`:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34128b8a744c0a4802caa813178395c5/href">https://medium.com/media/34128b8a744c0a4802caa813178395c5/href</a></iframe><p>Unfortunately, TypeScript itself does not currently provide an easy way to use custom transformers. But with the help of a custom library `ttypescript` (note the double ‘t’) we can easily compile our code. Let’s install the library:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe247ce946e34a9a04b65ae171b56ac0/href">https://medium.com/media/fe247ce946e34a9a04b65ae171b56ac0/href</a></iframe><p>We now need to add the following line to tsconfig.json to successfully compile our transformer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce46d2c4a7ae123330bde0bf520c8fa7/href">https://medium.com/media/ce46d2c4a7ae123330bde0bf520c8fa7/href</a></iframe><p>So the final version of `tsconfig.json` looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/667c629d1e539d4c48e9e5af9a5b1125/href">https://medium.com/media/667c629d1e539d4c48e9e5af9a5b1125/href</a></iframe><p>Finally we need to tell `ts-node` to use the newly installed `ttypescript` compiler.</p><p>First update the `start` script in `package.json`:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18f467a2b6a6570beb921127769a9445/href">https://medium.com/media/18f467a2b6a6570beb921127769a9445/href</a></iframe><p>Then we update the `nodemon.json`:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50a34502536aa2ef22f74cefc5132507/href">https://medium.com/media/50a34502536aa2ef22f74cefc5132507/href</a></iframe><p>That’s it, we’ve updated nodemon as well the `start` script. Just run `yarn start` or `yarn watch` and implement our approach to the code. If you are feeling lazy, you can find the final code here:</p><p><a href="https://github.com/oyalhi/update-using-dto">https://github.com/oyalhi/update-using-dto</a></p><p><strong>Conclusion</strong></p><p>Updating an object is a common scenario in a back end application. I wanted a solution that did not require a manual update of the code every time the requirements for an object has changed. Also, a solution that I can use in any project without rewriting the same boilerplate code.</p><p>We went through 4 solutions, each one improving the previous version. The first approach was buggy, had the possibility of changing our object. The second approach was too manual, required to assign each property manually. The third approach solved the problem; however was accepting illegal requests, though only using accepted keys. Our fourth and final approach rejected illegal requests for a rock solid approach.</p><p>I wanted to go one step further and create a generic function that could be used anywhere easily; however, we cannot use the `keys` dynamically and thus can’t create a generic function. But for now, what we have should suffice.</p><p>I hope you liked it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80ba7edd0084" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Important note about the Swift Lazy initialization]]></title>
            <link>https://medium.com/@oyalhi/important-note-about-the-swift-lazy-variables-88bde1012e2d?source=rss-986ba31b80ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/88bde1012e2d</guid>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Ömer Yalhı]]></dc:creator>
            <pubDate>Wed, 31 May 2017 15:17:41 GMT</pubDate>
            <atom:updated>2017-06-01T04:19:52.611Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are just learning about the Swift lazy initialization and also computed properties; there is a very important point you need to watch out for.</p><p>There are many very good writings about the Swift lazy initialization, so I will not repeat what’s been said. For example here:</p><p><a href="http://mikebuss.com/2014/06/22/lazy-initialization-swift/">http://mikebuss.com/2014/06/22/lazy-initialization-swift/</a></p><blockquote>Lazy variables look like computed properties; <strong>BUT</strong>, they are <strong>NOT</strong>.</blockquote><p>What do I mean? Let’s look at the Person class given in the above link:</p><pre><strong>class</strong> <strong>Person</strong> {<br>    <strong>var</strong> name: <strong>String</strong><br>    <br>    <strong>lazy</strong> <strong>var</strong> personalizedGreeting: <strong>String</strong> <strong>=</strong> { [<strong>unowned</strong> <strong>self</strong>] <strong>in</strong><br>        <strong>return</strong> &quot;Hello, \(<strong>self.</strong>name)!&quot;<br>    }()<br>    <br>    <strong>init</strong>(name: <strong>String</strong>) {<br>        <strong>self.</strong>name <strong>=</strong> name<br>    }<br>}</pre><p>Now, we create a simple john variable and assign a name John, then print out the personalized greeting. Then we change the person name to be ‘Jackson’ and again print out the personlized greeting:</p><pre><strong>let</strong> john = <strong>Person(name:</strong> &quot;John&quot;<strong>)<br>print(</strong>john.personalizedGreeting<strong>)   </strong>// prints &#39;Hello, John!&#39; </pre><pre>john.name = &quot;Jackson&quot;<br><strong>print(</strong>john.personalizedGreeting<strong>)   </strong>// prints &#39;Hello, John!&#39;</pre><p>Even though we changed the name, the personalized greeting still printed out the old name. This is because Lazy variables are NOT computed properties. <em>They are just initialized at the time of their first use, then they work as regular properties.</em></p><p>We set the personalizedGreeting to the name John the first time we initialized it, then we never updated it and because it is NOT a computed property, the output never changes.</p><p>In this particular example, it is better to use a computed property then a lazy variable, such as:</p><pre><strong>class</strong> <strong>Person</strong> {<br>    <strong>var</strong> name: <strong>String</strong><br>    <br>    <strong>var</strong> personalizedGreeting: <strong>String</strong> { <br>        <strong>return</strong> &quot;Hello, \(<strong>self.</strong>name)!&quot;<br>    }<br>    <br>    <strong>init</strong>(name: <strong>String</strong>) {<br>        <strong>self.</strong>name <strong>=</strong> name<br>    }<br>}</pre><p>In this case, because personalizedGreeting is a computed property, anytime the name is changed, the greeting is automatically updated:</p><pre><strong>let</strong> john = <strong>Person(name:</strong> &quot;John&quot;<strong>)<br>print(</strong>john.personalizedGreeting<strong>)   </strong>// prints &#39;Hello, John!&#39;</pre><pre>john.name = &quot;Jackson&quot;<br><strong>print(</strong>john.personalizedGreeting<strong>)   </strong>// prints &#39;Hello, Jackson!&#39;</pre><p>It is easy for a beginner to confuse a property which is lazily initialized where the initial code runs once, versus a computed property, where the code is ‘computed’ each time the property is accessed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88bde1012e2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dispatch Barriers in Swift 3]]></title>
            <link>https://medium.com/@oyalhi/dispatch-barriers-in-swift-3-6c4a295215d6?source=rss-986ba31b80ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c4a295215d6</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ömer Yalhı]]></dc:creator>
            <pubDate>Tue, 27 Dec 2016 08:44:58 GMT</pubDate>
            <atom:updated>2016-12-27T08:54:04.262Z</atom:updated>
            <content:encoded><![CDATA[<p>Dispatch barriers can be used to speed serial queues for read write operations.</p><p><strong>Serial Queues<br></strong>Tasks in serial queues execute one a time in first in first out format. Task1 has to finish for Task2 to start. Task2 has to finish for Task3 to start and so on.</p><p>Below code reads and writes to the dictionary. Because the read and write operations are serial, we don’t run in to <a href="https://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem">Readers–writers problem</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6653f76cfd784f85c5533a690711763c/href">https://medium.com/media/6653f76cfd784f85c5533a690711763c/href</a></iframe><p><strong>Concurrent Queues</strong><br>Tasks in concurrent queues execute in any order and can start simultaneously. Below code reads and writes to the dictionary concurrently. This is very fast compared to the above serial queue. However, because we may be reading while writing at the same time, we will run in to the readers-writers problem.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c06e9208527c24a5575e6483327a720a/href">https://medium.com/media/c06e9208527c24a5575e6483327a720a/href</a></iframe><p><strong>Concurrent Queue with Barrier</strong></p><p>What if there was a way that you can ensure no writing occurs while reading and no reading occurs while writing using concurrency? Well, there is a way to this using <em>Concurrent Queue with Barrier</em>. If we take the concurrent code above, and insert a barrier to the write operation, we ensure that the writing will occur after all the reading in the queue is performed and that no reading occurs while writing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10cfbcce81459808b25b0b2822973205/href">https://medium.com/media/10cfbcce81459808b25b0b2822973205/href</a></iframe><p>Using concurrent queues with barriers helps us improve and speed up our code while eliminating the readers-writers problem, which is also important for singletons.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c4a295215d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>