<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MemphisNguyen</title>
    <description>The latest articles on DEV Community by MemphisNguyen (@memphisnguyen).</description>
    <link>https://dev.to/memphisnguyen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F184762%2Fae88862d-1548-47d5-840a-841895c62063.png</url>
      <title>DEV Community: MemphisNguyen</title>
      <link>https://dev.to/memphisnguyen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/memphisnguyen"/>
    <language>en</language>
    <item>
      <title>AdonisJS validate file in Object</title>
      <dc:creator>MemphisNguyen</dc:creator>
      <pubDate>Sat, 27 Aug 2022 19:44:00 +0000</pubDate>
      <link>https://dev.to/memphisnguyen/adonisjs-validate-file-in-object-8f8</link>
      <guid>https://dev.to/memphisnguyen/adonisjs-validate-file-in-object-8f8</guid>
      <description>&lt;p&gt;This is my experience about validating request data using AdonisJS validator and the solution for it. I can't easily find an article about this issue so I decide to write this so that others which have the same issue could find the solution easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;This issue happened when I had a data block that need to submit to the backend to handle. Its format looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "users": [
    {
      "name": "John Doe",
      "avatar": &amp;lt;File&amp;gt;
    },
    ...
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And following the documents from AdonisJS, I use this snippet to validate the data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;params = await request.validate({
  schema: schema.create({
    users: schema.array().members(
      schema.object().members({
        name: schema.string({ trim: true }),
        avatar: schema.file({
          size: '10mb',
          extnames: ['jpg', 'heic']
        }),
      })
    ),
  }),
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;The problem is that submitted data in the backend will be stored by AdonisJS in 2 parts, &lt;code&gt;request.all()&lt;/code&gt; and &lt;code&gt;request.allFiles()&lt;/code&gt;. So the data will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;request.all() : { "users": [ { "name": "John Doe" } ] }
request.allFiles(): { "users": [ { "avatar": &amp;lt;File&amp;gt; } ] }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to running the validation, these data pieces will need to be merged to make sure it pass the rules.&lt;br&gt;
Unfortunately, following &lt;a href="https://github.com/adonisjs/core/issues/3435#issuecomment-998479662"&gt;AdonisJS's author idea&lt;/a&gt; that deep merge will affect to the performance of the framework. Therefore, &lt;strong&gt;shallow merge is used to combine them&lt;/strong&gt; and it looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data = {
  ...body,
  ...files,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you know what happened. After the merge, user item only has avatar property.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;As the author suggestion in the comment about, I've change the way to validate the data by &lt;strong&gt;perform deep merge data by using lodash merge and use AdonisJS standalone validator to validate the merged data&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import lodashMerge from 'lodash/merge'
import { validator } from '@ioc:Adonis/Core/Validator'
...
allData = lodashMerge({}, request.all(), request.allFiles())

params = await validator.validate({
  data: allData,
  schema: schema.create({
    users: schema.array().members(
      schema.object().members({
        name: schema.string({ trim: true }),
        avatar: schema.file({
          size: '10mb',
          extnames: ['jpg', 'heic']
        }),
      })
    ),
  }),
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's how I got the params as same as the way it's submitted.&lt;/p&gt;

&lt;p&gt;Readers, let me know if you can see any other way to resolve this issues.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>adonisjs</category>
      <category>programming</category>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>[Help] Multi-schema on AdonisJS</title>
      <dc:creator>MemphisNguyen</dc:creator>
      <pubDate>Sat, 27 Aug 2022 19:02:37 +0000</pubDate>
      <link>https://dev.to/memphisnguyen/help-multi-schema-on-adonisjs-426a</link>
      <guid>https://dev.to/memphisnguyen/help-multi-schema-on-adonisjs-426a</guid>
      <description>&lt;p&gt;Hi there,&lt;br&gt;
I'm working on project using AdonisJS and Postgres to serve for multi-tenant purpose. I want to be able to access multiple schema with 1 source code only but I couldn't find any document about it.&lt;/p&gt;

&lt;p&gt;In basic, I will use the client ID to determine the schema that would be used to query the data from.&lt;br&gt;
I can find some document about putting the schema with the table name in the model but that's not what I want because the model will be sticked to one schema only.&lt;/p&gt;

&lt;p&gt;Have you experienced with this kind of structure and can you help me?&lt;br&gt;
Thanks.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>adonisjs</category>
    </item>
    <item>
      <title>Passing object as props in VueJS</title>
      <dc:creator>MemphisNguyen</dc:creator>
      <pubDate>Sat, 03 Oct 2020 09:24:09 +0000</pubDate>
      <link>https://dev.to/memphisnguyen/passing-object-as-props-in-vuejs-7og</link>
      <guid>https://dev.to/memphisnguyen/passing-object-as-props-in-vuejs-7og</guid>
      <description>&lt;p&gt;Hello everyone.&lt;br&gt;
I'm currently working on a project require render JSX buts now I'm stucking with a problem that passing JSON object as props.&lt;/p&gt;

&lt;p&gt;Even though VueJS supports passing a JSON object variable for the prop but not able to pass JSON object directly like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Comp :property="{\"key": \"value\"}" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Have you ever met this problem and having any suggestions for this?&lt;/p&gt;

&lt;p&gt;I'm using encodeURIComponent and treat it as a string for now but it's not a pretty solution.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
