Validate a random key name of a key value pair in a controller for consumption of an api

So, we want to add a feature to an e-commerce to digest externally given key value pairs in the system.

The idea behind it is that what every system you attach to import a ressource (for example an order) can write or read those assets.

We would like to validate that:

  • randomkeyname is unique in this array
  • randomkeyname is not longer than 16 characters
  • randomkeyvalue is not longer than 256 characters
  • the quantity of key value pairs is equal or less than 8

So now coming from a soft python experience I am aware how validate the presence of information, but the above validation of the key value pairs, how should they happen? Do I go with a regex on the key and values and count the pairs?

[…]

/// More order information
        "metadata": {
          "randomkeyname": "randomvalue",
          "randomkeyname1": "randomvalue",
        },
/// More order information