0

I am validating a FHIR resource using ajv.

ajv -s fhir.schema.json -d SampleOperationOutCome.json

This is the SampleOperationOutCome.json file

{
"resourceType": "OperationOutcome",
"id": "101",
"text": {
    "status": "additional",
    "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p>text.<\/p><\/div>"
},
"issue": [
    {
        "severity": "error",
        "codeerror": "invalid",
        "details": {
            "coding": [
                {
                    "system": "http://hl7.org/fhir/operation-outcome",
                    "code": "MSG_CANT_PARSE_CONTENT"
                }
            ],
            "text": "text"
        }
    }
]

}

You can get the json schema from FHIR build 3.4 json schemas

My main challenge when validating is to understand which is really the source issue. For example, in this case I've change de "code" field to "codeerror", but when validating I get mainly a list of the following error:

{ keyword: 'additionalProperties',
dataPath: '',
schemaPath: '#/additionalProperties',
params: { additionalProperty: 'issue' },
message: 'should NOT have additional properties' },

I do know if it is the normal output, or if the source of this behaviour is the json schema or ajv, but I would expect some message like "invalid field codeerror, one of code expected".

Any suggestion in order to be able do analyse the full output and get the real source?

Thanks.

Marti Pàmies Solà
  • 611
  • 1
  • 6
  • 12

1 Answers1

1

JSON Schema does not define an output format as of draft-7, so this output (error or otherwise) is defined by the library, in this case ajv.

We (JSON Schema) are currently looking at if we should define a standard output format, currently tracking on a github issue.

In the instance of your error, additional properties false prevents additional properties, so you'll have to check your JSON Schema and the instance for which additional properties you've added. Although in this case, you already know because you're testing.

If you're looking for a way to get this information programatically, there's no standard for doing so.

ajv may provide some additional error information if you ask using it's documented API. It LOOKS like it should be giving you some more information that you've presented, although an empty dataPath may be referencing the root of the JSON instance.

Relequestual
  • 11,631
  • 6
  • 47
  • 83
  • Hi Relequestual, thank yo so much for your extended response, although I do not get a solution about how to go faster when validating json resources, now for me is more clear which is the source reason. – Marti Pàmies Solà Aug 02 '18 at 08:57
  • Are you finding the validation is slow? ajv is one of the fastest! – Relequestual Aug 02 '18 at 08:59
  • No excuse for the misunderstanding, when I mean faster, is the whole process of validating a new json resources template. I work in a mode where I first create text based json template document, I validate it and then on runtime environment I just replace values. Create this first template take me so many time regarding unclear validation error responses. Thanks – Marti Pàmies Solà Aug 03 '18 at 10:28
  • OK. I understand it might be a slow process to understand how your data is not valid according to the schema. This gets easier the more familiar you become with JSON Schema. – Relequestual Aug 03 '18 at 10:31
  • Your question here is "How do I get more detailed errors from ajv?" Do you feel I've answered your question? In some situations, more information on the validation error is provided, but not in the situation you present. – Relequestual Aug 03 '18 at 10:32
  • Hi, more than more information what I need is a more precise information, indicating exactly where the error comes from. On my example just an invalid name field generate more than 40 errors, and for me none for them clear to understand. Thks. – Marti Pàmies Solà Aug 06 '18 at 06:31
  • The FHIR JSON Schemas are not the simplest of schemas, and the "error" you're looking for might be nested that many number of levels deep in the tree. All the errors it presents are in fact the errors. What it provides IS actually the precise information, because each one of the layers of keywords fails. Why not try a simple schema you've made yourself to see and understand how errors are presented at different levels? You're asking a library to present information to you that it can't because it can't know the context. – Relequestual Aug 06 '18 at 08:14