Skip to main content

Hello everyone,

I'm looking for a way to validate JSON documents in Uniface against a JSON Schema (to check both syntax and structure).
Has anyone here already implemented such a solution or integrated an external library or service for this purpose?

Any tips, sample code, or recommended approach would be greatly appreciated.

Thanks in advance!



------------------------------
Didier
------------------------------

Hello everyone,

I'm looking for a way to validate JSON documents in Uniface against a JSON Schema (to check both syntax and structure).
Has anyone here already implemented such a solution or integrated an external library or service for this purpose?

Any tips, sample code, or recommended approach would be greatly appreciated.

Thanks in advance!



------------------------------
Didier
------------------------------

Hi

I haven't really used it to validate but to convert from RESTful to Uniface and vice versa. The API has names that can be translated into different systems managed by Uniface itself. Thus, a JSON arrives and is converted following a Schema. And when the response is generated (componenttostruct, structtojson) is also converted.
In the operation init of each webservice a dictionary is created for #uniface or for #csharp that allows you to translate the body (and the querystring).

So, when a JSON arrives  {"data":[{"key":"111"}]}
The webservice converts it to a struct and knows how to translate it as if the original JSON had been {"EXAMPLE.MODEL":[{"ID":"111"}]}
To be processed immediately by Uniface (jsontostruct structtocomponent)

This mechanism allows us to be very flexible to include or not fields and entities and change their names in the JSON.

It is based in a recursive operation to analyze the SCHEMA and define a struct with the translation. And another recursive operation to apply the translation to body and querystring if needed.
{
   "$schema": "http://json-schema.org/draft/2020-12/schema",
    "$id": "/example",
    "title": "Example",
    "type": "object",
    "properties": {
       "data": {
          "type": "array",
          "#uniface": "EXAMPLE.MODEL",
          "#csharp": "Example",
          "items": {
             "type": "object",
             "properties": {
                "key": {
                   "type": "string",
                   "#uniface": "ID",
                   "#csharp": "Id"
                },
             ...
As every loop/recursion is done using structs, the result is really fast. Although we include "type" or even other restrictions, do not use the definition to validate the contents. Validation is done by Uniface mechanisms and returned using ERRORS entity + FIELD and REASON fields, which are included in JSON Schema in order to be included and translated. So, Uniface can add "ID" + "This field is mandatory" and in JSON will be included "key" + "This field is mandatory".

Hope this helps a little bit


------------------------------
Luis Vila
luis.vila@uniface.es
Spain
------------------------------