Giter VIP home page Giter VIP logo

Comments (5)

kylef avatar kylef commented on June 21, 2024

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.

from drafter.

jqian33 avatar jqian33 commented on June 21, 2024

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

from drafter.

kylef avatar kylef commented on June 21, 2024

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"
            }
          }
        }
      ]
    }
  ]
}

from drafter.

kylef avatar kylef commented on June 21, 2024

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

from drafter.

jqian33 avatar jqian33 commented on June 21, 2024

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

from drafter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.