Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinct OneOf gets parsed as single OneOf group in schema #785

Closed
jqian33 opened this issue Jun 19, 2020 · 5 comments
Closed

Distinct OneOf gets parsed as single OneOf group in schema #785

jqian33 opened this issue Jun 19, 2020 · 5 comments

Comments

@jqian33
Copy link

jqian33 commented Jun 19, 2020

apib:

## X
- One Of
    - foo(number)
    - foo (boolean)
- One Of
    - bar (number)
    - bar (boolean)

json schema:

{
   "oneOf":[
      {
         "properties":{
            "foo":{
               "type":[
                  "number"
               ]
            }
         }
      },
      {
         "properties":{
            "foo":{
               "type":[
                  "boolean"
               ]
            }
         }
      },
      {
         "properties":{
            "bar":{
               "type":[
                  "number"
               ]
            }
         }
      },
      {
         "properties":{
            "bar":{
               "type":[
                  "boolean"
               ]
            }
         }
      }
   ]
}

I am expecting the two One Of statements to be in distinct groups:

{
   "oneOf":[
      {
         "properties":{
            "foo":{
               "type":[
                  "number"
               ]
            }
         }
      },
      {
         "properties":{
            "foo":{
               "type":[
                  "boolean"
               ]
            }
         }
      }
   ],
   "oneOf":[
      {
         "properties":{
            "bar":{
               "type":[
                  "number"
               ]
            }
         }
      },
      {
         "properties":{
            "bar":{
               "type":[
                  "boolean"
               ]
            }
         }
      }
   ]
}

Is there a different mson syntax i should use to achieve the expected schema or is this a bug?

@kylef
Copy link
Member

kylef commented Jun 19, 2020

Can you try and explain the type of structure you are trying to describe, and then I can attempt to advise on MSON used to describe it. It's not entirely clear to me on your intent from the One Of example.

Note your expected JSON Schema contains two oneOf keys in the same object, JSON keys are expected to be unique.

@jqian33
Copy link
Author

jqian33 commented Jun 19, 2020

Both properties foo and bar can be either a number of boolean.

@kylef
Copy link
Member

kylef commented Jun 19, 2020

I'd suggest using enum to declare and exclusive list of types for each property. The following MSON will describe an object which may contain properties (foo and bar) which must be either a string or number:

+ foo (enum[string, number])
+ bar (enum[string, number])

You can extract the enum as a type, if desired:

## Data Structures

## MyObject

+ foo (StringOrNumber)
+ bar (StringOrNumber)

### StringOrNumber (enum[string, number])

These will product the following JSON Schema in Drafter 5:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "foo": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        }
      ]
    },
    "bar": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        }
      ]
    }
  }
}

If you want to descibe that they both need to be a number, or both need to be a string, then the following MSON will describe that as follows:

+ One Of
    + Properties
        + foo (number)
        + bar (number)
    + Properties
        + foo (string)
        + bar (string)

Which currently produces the following JSON Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "allOf": [
    {
      "oneOf": [
        {
          "properties": {
            "foo": {
              "type": "number"
            },
            "bar": {
              "type": "number"
            }
          }
        },
        {
          "properties": {
            "foo": {
              "type": "string"
            },
            "bar": {
              "type": "string"
            }
          }
        }
      ]
    }
  ]
}

@kylef
Copy link
Member

kylef commented Jun 19, 2020

Please sustitute string for boolean in my above examples, I misread.

@jqian33
Copy link
Author

jqian33 commented Jun 19, 2020

@kylef, thank you for providing the examples! This has solved my problem.

@jqian33 jqian33 closed this as completed Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants