everit-org/json-schema

Feature request: support `prefixItems`

counter2015 opened this issue · 1 comments

schema file

{
  "type": "array",
  "prefixItems": [
    {
      "type": "number"
    },
    {
      "type": "string"
    },
    {
      "enum": [
        "Street",
        "Avenue",
        "Boulevard"
      ]
    },
    {
      "enum": [
        "NW",
        "NE",
        "SW",
        "SE"
      ]
    }
  ]
}

code

import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.IOException;

public class Feature {

    static void check() {
        try (var inputStream = Feature.class.getResourceAsStream("schema.json")) {
            var rawSchema = new JSONObject(new JSONTokener(inputStream));
            var schema = SchemaLoader.builder().schemaJson(rawSchema).draftV7Support().build().load().build();
            schema.validate(new JSONArray("[24, \"Sussex\", \"Drive\"]\n"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        check();
    }
}

it should failed check since “Drive” is not one of the acceptable street types:

see also: https://json-schema.org/understanding-json-schema/reference/array.html#:~:text=prefixItems%20is%20an%20array%2C%20where,of%20the%20input%20array%2C%20etc

erosb commented

Hello @counter2015 , I released a new validator which supports json schema draft2020-12, including the "prefixItems" keyword.