json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

Please add support for minContains/maxContains of 2019-09 draft

Open sedovalx opened this issue 3 years ago • 1 comments

The "contains" restriction was enhanced in 2019-09, please see https://json-schema.org/understanding-json-schema/reference/array.html#mincontains-maxcontains

I can try to add this support myself and prepare a PR but please point me in the right direction.

sedovalx avatar Feb 11 '22 13:02 sedovalx

@sedovalx From the specification, I can see the minContains and maxContains are very similar to the minItems and maxItems in the array keyword.

https://github.com/networknt/json-schema-validator/blob/master/src/main/java/com/networknt/schema/MaxItemsValidator.java

We just need to create two new validators and wire them into the 2019-09 in https://github.com/networknt/json-schema-validator/blob/master/src/main/java/com/networknt/schema/ValidatorTypeCode.java

Let me know if you have any specific questions. Thanks a lot for the effort.

stevehu avatar Feb 15 '22 01:02 stevehu

Do you have any news ? @sedovalx Did you manage to implement this?

tripple-v avatar Feb 08 '23 14:02 tripple-v

No, got distracted as usual. Sorry :)

sedovalx avatar Feb 08 '23 15:02 sedovalx

Hi 👋 I would be interested in the feature as well. Are you working on it actively? If so: Is there an estimate on when this might be included? Thank you for your effort!

KochTobi avatar Feb 13 '23 15:02 KochTobi

This was resolved by PR #702

fdutton avatar Apr 20 '23 18:04 fdutton

Hello, Thanks to you @fdutton for the fix. I encountered some strange behavior with it. I have tested the minContains with a very simple schema, but the error is a "contains" error and not a "minContains" error.

This is the sample schema :

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "schemas/samples/sample.mincontains.schema.json",
  "properties": {
    "myArray": {
      "type": "array",
      "minContains": 2,
      "contains": {"properties": {"itemType": {"const": "type A"}}
      },
      "items": {"properties": {"itemType": {"type": "string"}}}
    }
  }
}

And this is the document to validate with this schema :

{
  "$schema": "schemas/samples/sample.mincontains.schema.json",
  "myArray": [{"itemType": "type A"}]
}

This is the com.networknt.schema.ValidationMessage I have as result:

Attribute Value
type "contains"
code "1043"
path "$.myArray"
schemaPath "#/properties/myArray/contains"
arguments ["2", "{"properties":{..."]
details null

The ValidationMessage should have "minContains" as type value I have tested the "normal" behavior on https://www.jsonschemavalidator.net/s/4KTe05B9

tripple-v avatar May 15 '23 10:05 tripple-v