Dropdown oneOf implementation, data does not pass validation

I have multiple drop-downs in the form I am implementing. I do not want to expose the underlying keys in the UI, so I am using oneOf in the schema so I can specify titles.

This is working fine, and I am getting the ‘const’ value in the data.

However, when I send this JSON back to my API, it is not passing schema validation. The string value is not detected as ‘oneOf’ the provided options (I guess because they are objects).

I am using NewtonSoft.Json in my .NET 7 C# API for validation.
In the interrim, I have written some code to convert oneOf properties in the schema to an ‘enum’ of the keys before validation, so the validation can pass. But I don’t like this solution.

I have a few questions:

  • Is the data I am getting back from JsonForms valid to the schema I provide? Since the result is a string key, but the oneOf options are objects
  • Is this an issue with Newtonsoft itself?
  • If so, has anybody encountered this before? How did you overcome it?

Here is an example schema:

{
  "type": "object",
  "required": [
    "USER"
  ],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "USER": {
      "type": "string"
    },
    "PASSWORD": {
      "type": "string"
    },
    "SINGLE_CHOICE": {
      "type": "string",
      "title": "Single choice",
      "description": "Some description",
      "oneOf": [
        {
          "const": "Yes",
          "title": "Yes"
        },
        {
          "const": "No",
          "title": "No"
        }
      ]
    },
    "MULTI_CHOICE": {
      "type": "array",
      "title": "Multi choice",
      "description": "Some description",
      "uniqueItems": true,
      "items": {
        "type": "string",
        "oneOf": [
          {
            "const": "A",
            "title": "A"
          },
          {
            "const": "B",
            "title": "B"
          },
          {
            "const": "C",
            "title": "C"
          }
        ]
      }
    }
  },
  "additionalProperties": false
}

And is some example data I am validating against in a format it would come back from Json forms

{
  "$schema": "./schema_camel.json",
  "USER": "value",
  "SINGLE_CHOICE": "Yes",
  "MULTI_CHOICE": ["A", "C"] 
}

Hi @Droxx,

Is the data I am getting back from JsonForms valid to the schema I provide? Since the result is a string key, but the oneOf options are objects

Yes the data is valid. You can check this easily with an online validator like this one. The oneOf are not objects but just nested JSON Schemas. title is an annotation and const is just a syntactic sugar for enum with one element.

Is this an issue with Newtonsoft itself?

I don’t have any experience with Newtonsoft. According to their website they have “Complete support for Draft 2019-09 of JSON Schema”. So I would expect them to not error on such a basic use case of JSON Schema.

I would therefore recommend to first double check your code whether you are actually validating the posted data to the posted schema.

If it’s a problem with Newtonsoft because it can’t refer a type for the oneOf entries you might want to try adding "type": "string" to all entries.

HI @sdirix,

Thanks for your reply. The examples I posted are exactly what I am validating using Newtonsoft. Read directly from a file in my unit-tests.

I also checked this validation using online tools, so I can see that it is correct, and passes validation. Yet newtonsoft does not agree.

I tried adding type:string to each of the oneOf objects, but this still does not validate using Newtonsoft.

I switched out newtonsoft to use JsonSchema.Net. And this validates correctly.

I would rather be using Newtonsoft. So my question now becomes, is this a known issue with Newtonsoft’s validation? (I cannot find any discussion about this elsewhere).
Or is there perhaps an option I must enable on Newtonsoft to get this to process correctly.

The online validator is actually from Newtonsoft. So if it works there it should also work in your implementation. Sadly I can’t help you diagnose the error as I don’t have experience with Newtonsoft.

Thanks for the feedback @sdirix.

I believe this may be an issue in the NJsonSchema validation library I am using. So I have opened an issue there. In the meantime I will move forward with JsonSchema.Net