Table of tables in Kong schema


#1

Hey, I am trying to build a custom plugin, what I require is a table of table in schema.lua file is this possible? or is this even a recommended way in kong?

basically, I want to achieve

return {
    fields = {
       robo_list = { type = "array", required = false, default = { { field = "one" }, { field = "two" } }}
    }
}

Is this the right way to handle this problem, or should I create duo file or perhaps a JSON in a string?
And I go with the above-mentioned way how should I send a curl request to make it work?


#2

Hi!

The schema handling code in the current version of Kong has a number of limitations for dealing with more complex type definitions. The good news is that schema handling has been completely revamped for the new release, and will be available for custom plugins in the near future (we’re rolling out the new data model gradually, so it will be available for the Route and Service entities first, and extended to other entities like Plugin later).

In the mean time, instead of creating an array of objects with n fields, you are probably better off creating n arrays, one for each field. Less than ideal, but it works.

Alternatively, making it an array of strings where each string is a JSON object would work as well, but you would only be able to access/update them as “whole objects” and not addressing individual fields, and you would also need to be mindful of the proper escaping involved when doing requests (which indeed might result in some ugly requests), and encode/decode yourself in the plugin code. That’s cumbersome and not a very good experience, so I would just go with the multiple arrays.


#3

Hi hlsham,

Circling back on this - as now those updates are in. How do I do an array of schemas or something similar?

Basically looking for:

{
"items" : 
   [
      {... table ....}, 
      {... table2 ....}
   ]
}

something like that


#4

@Ross_Sbriscia While the new schema implementation is shipped in 0.13.0, it isn’t available for plugins yet, and these must still use the previous schema definition.
As @hisham said, we are gradually rolling it out. Our focus is still on improving plugin development experience, and we are focused a plugin SDK to go alongside the new schema definition.
The suggestion given by @hisham still stands. As of today, we advise you to follow what many other plugins are already doing, and store such complex types as a JSON-encoded string.


#5

Hi Ross,

I had the exact same need, in our schema.lua, we went with:

my_array = { required = true, unique = true, type = "array" }

and when we posted to it, we did this:

{“config.my_array”: [{“key”: “value”, “key”: “value”, i.e. json table}, {json table}, {json table}]}

And that worked out great, we don’t have a contractual schema, but it allowed us to have an array of table objects, be sure to check for nulls etc… before referencing a property in the table, hope it helps.