ExodusMovement / schemasafe

A reasonably safe JSON Schema validator with draft-04/06/07/2019-09/2020-12 support.
https://npmjs.com/@exodus/schemasafe
MIT License
155 stars 12 forks source link

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

Open rajsenthil opened 1 year ago

rajsenthil commented 1 year ago

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 commented 11 months ago

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.