1EdTech / openbadges-discussion

A no-code repository for having discussions related to the general technical issues of openbadges.
10 stars 3 forks source link

Schema Definition for Array Property #15

Closed kayaelle closed 7 years ago

kayaelle commented 10 years ago

How do you define and validate against an array of values? enum seems to imply that only value can be valid at a time.

What if you want to define an array of strings and more than one can be valid. For instance if we were to define age groupings something like: "3-5", "6-11", "12-18", "19-40", "40-65", "65-122"

and want to allow multiple values, how to best define that?

@andrewhayward ? :)

Thnx

andrewhayward commented 10 years ago

@kayaelle - I think something like this is what you're after...

...
"properties": {
  ...,
  "age": {
    "type": "array",
    "items": {
      "type": "string",
      "enum": ["3-5", "6-11", "12-18", "19-40", "40-65", "65-122"]
    },
    "uniqueItems": true
  },
  ...
},
...
kayaelle commented 10 years ago

ok - I'll look into that. Thanks! :+1:

andrewhayward commented 10 years ago

@kayaelle - no problem. I've just confirmed that this (slightly more complex) schema

{
  "type": "object",
  "properties": {
    "age": {
      "oneOf": [
        {
          "type": "string",
          "enum": ["3-5", "6-11", "12-18", "19-40", "40-65", "65-122"]
        },
        {
          "type": "array",
          "uniqueItems": true,
          "minItems": 1,
          "items": {
            "$ref": "#/properties/age/oneOf/0"
          }
        }
      ]
    }
  }
}

will validate both

{age: "3-5"}

and

{age: ["3-5", "6-11"]}
kayaelle commented 10 years ago

This is great @andrewhayward & really helpful. Thank you for validating it. I think we'll find that there will be schemas that will find this useful/necessary.

ottonomy commented 10 years ago

@andrewhayward is a JSON-schema wizard. I'll consider making a badge for that.

andrewhayward commented 10 years ago

I'm a what?

iamjessklein commented 10 years ago

:zap: wizard :

timothyfcook commented 7 years ago

Moving to archive.