Tuple Validation

We want to display the address of a person. The address is represented as an array of four elements: [number, street_name, street_type, direction]

1{
2  "name": "John Doe",
3  "age": 25,
4  "address": [123, "Main St", "Avenue", "NW"]
5}

each element of the address array have constraints of their own:

  • number should be an integer
  • street_name should be a string
  • street_type can have only one value from the list: ["Street", "Avenue", "Boulevard"]
  • direction can have only one value from the list: ["NW", "NE", "SE", "SW"]

Schema definition

In such cases, you can use the prefixItems keyword to define the schema for each element of the tuple array.

Example

1{
2  "type": "array",
3  "prefixItems": [
4    {"type": "integer"},
5    {"type": "string"},
6  ]
7}

The above schema defines an array in which, the first element should be an integer and the second element should be a string.

Now, try to modify the address property on the side editor, such that it follows the constraints mentioned above.

Loading...
Output
Please click the button or use
Shift+Enter
to view the output