Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft **2020-12**: Error while parsing the schema #164

Open
rajsenthil opened this issue Sep 14, 2023 · 1 comment
Open

Draft **2020-12**: Error while parsing the schema #164

rajsenthil opened this issue Sep 14, 2023 · 1 comment

Comments

@rajsenthil
Copy link

I have this below json schema drafted with version 2020-12. I tested online here and also using this golang package. The schema is successfully parsed and could validate the json.
Using schemasafe I am getting error Error: Unexpected keywords mixed with const or enum: ["maxLength","minLength"] at #/properties/productType

Can you please check and see if there is any fix for this?

Jsonschema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$ref": "#/$defs/Product",
  "$defs": {
    "Product": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "productType": {
          "type": "string",
          "enum": [
            "Simple",
            "Configurable",
            "Grouped",
            "Virtual",
            "Bundle"
          ],
          "maxLength": 20,
          "minLength": 1,
          "title": "product type",
          "description": "product type",
          "default": "Simple",
          "examples": [
            "Bundle",
            "Configurable"
          ]
        },
        "product_date": {
          "type": "string",
          "format": "date-time",
          "description": "Product date must be valid"
        },
        "productPrice": {
          "$ref": "#/$defs/ProductPrice"
        },
        "tags": {
          "type": "object",
          "a": "b",
          "foo": [
            "bar",
            "bar1"
          ]
        }
      },
      "additionalProperties": false,
      "type": "object",
      "required": [
        "id",
        "productType",
        "product_date",
        "productPrice"
      ]
    },
    "ProductPrice": {
      "properties": {
        "regularPrice": {
          "type": "number"
        },
        "markedPrice": {
          "type": "number"
        },
        "discount": {
          "type": "integer"
        }
      },
      "additionalProperties": false,
      "type": "object",
      "required": [
        "regularPrice",
        "markedPrice"
      ]
    }
  }
}

I am getting error while parsing this jsonschema
Error: Unexpected keywords mixed with const or enum: ["maxLength","minLength"] at #/properties/productType

@ChALkeR
Copy link
Contributor

ChALkeR commented Oct 10, 2023

This is a coherence check, as keywords adjacent to enum are not effective there and can cause confusion: they either disallow some value listed in enum, or are not doing anything at all.

This can be fixed by either removing unused maxLength/minLength keywords that are adjacent to enum from the schema (recommended), or by using the allowUnusedKeywords: true option (not recommended).

That said, this is much less dangerous than other types of unused keywords, just something that can cause confusion in the schema. So perhaps a separate option is needed to relax this separately from allowUnusedKeywords -- I'll think about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants