Code Monkey home page Code Monkey logo

Comments (10)

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

Your LSP is giving wrong result. it should give error. I tested your example with online validate at https://www.jsonschemavalidator.net. it also gives the same result.

to explain the error, I constructed following simplified example:

schema.json

{
  "properties": {
    "x": {
      "type": "string"
    }
  },
  "patternProperties": {
      "x*": {
        "type": "number"
      }
    }
}

instance.json

{
  "x": "y"
}

the above json gives following error:

[I#] [S#] doesn't validate with file:///Users/santhosh/gh/santhosh-tekuri/jsonschema/cmd/jv/sch.json#
  [I#/x] [S#/patternProperties/x%2A/type] expected number, but got string

if you change json to following:

{
  "x": 12
}

you get following error:

[I#] [S#] doesn't validate with file:///Users/santhosh/gh/santhosh-tekuri/jsonschema/cmd/jv/sch.json#
  [I#/x] [S#/properties/x/type] expected string, but got number

you can see that it fails in both cases where x value is string and number

as per the schema the property x matches two schemas /properties/x and patternProperties/x*. so the property value must satisfy both schemas.

/properties/x says value must be string.
patternProperties/x* says value must be number

thus it is impossible to add property x with valid value

from jsonschema.

Tloe avatar Tloe commented on July 30, 2024

Thank you for quick response!

Right, So I would need to not match on Env in patternProperties then or on x in your example.

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

Yes. You should use oneOf keyword for or behaviour

from jsonschema.

Tloe avatar Tloe commented on July 30, 2024

Ah, you mean instead of | in patternProperties? I guess if I got you right that would make it easier indeed. :)

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

{ "oneOf":[
{"properties": { "x": { "type": "string" } }},
{"patternProperties": { "x*": { "type": "number" } } }}
]}

from jsonschema.

Tloe avatar Tloe commented on July 30, 2024

I'm not sure if that will work in my example though? As I need both Env and aname0 to be valid

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "patternProperties": {
    "(^{{[ ]+\\.[A-Za-z][A-Za-z0-9_-]+[ ]+}}$)|(^[A-Za-z][A-Za-z0-9_-]+$)": {
      "oneOf": [
        {
          "properties": {
            "Env": {
              "patternProperties": {
                "^[A-Za-z][A-Za-z0-9_-]+$": {
                  "type": [
                    "boolean",
                    "number",
                    "string"
                  ]
                }
              },
              "type": "object"
            }
          }
        },
        {
          "patternProperties": {
            "(^{{[ ]+\\.[A-Za-z][A-Za-z0-9_-]+[ ]+}}$)|(^[A-Za-z][A-Za-z0-9_-]+$)": {
              "properties": {
                "Default": {
                  "type": "string",
                  "enum": [
                    "ALLOW",
                    "DENY"
                  ]
                }
              },
              "patternProperties": {
                "(^{{[ ]+\\.[A-Za-z][A-Za-z0-9_-]+[ ]+}}$)|(^[A-Za-z][A-Za-z0-9_-]+$)": {
                  "type": "string",
                  "title": "Policy"
                }
              },
              "additionalProperties": false,
              "type": [
                "object",
                "null"
              ]
            }
          }
        }
      ],
      "additionalProperties": false,
      "type": [
        "object",
        "null"
      ]
    }
  },
  "type": "object",
  "title": "Resourcename"
}

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

You are right. It does not seem straight forward

from jsonschema.

Tloe avatar Tloe commented on July 30, 2024

Thanks anyway, I know why its not working and should be able to find a solution now :)

from jsonschema.

santhosh-tekuri avatar santhosh-tekuri commented on July 30, 2024

I found the solution using propertyNames.

below is the working schema:

{
   "$schema":"https://json-schema.org/draft/2020-12/schema",
   "patternProperties":{
      "(^{{[ ]+\\.[A-Za-z][A-Za-z0-9_-]+[ ]+}}$)|(^[A-Za-z][A-Za-z0-9_-]+$)":{
         "propertyNames":{
            "pattern":"(^{{[ ]+\\.[A-Za-z][A-Za-z0-9_-]+[ ]+}}$)|(^[A-Za-z][A-Za-z0-9_-]+$)"
         },
         "properties":{
            "Env":{
               "patternProperties":{
                  "^[A-Za-z][A-Za-z0-9_-]+$":{
                     "type":[
                        "boolean",
                        "number",
                        "string"
                     ]
                  }
               },
               "type":"object"
            }
         },
         "additionalProperties":{
            "propertyNames":{
               "pattern":"(^{{[ ]+\\.[A-Za-z][A-Za-z0-9_-]+[ ]+}}$)|(^[A-Za-z][A-Za-z0-9_-]+$)"
            },
            "properties":{
               "Default":{
                  "type":"string",
                  "enum":[
                     "ALLOW",
                     "DENY"
                  ]
               }
            },
            "additionalProperties":{
               "type":"string",
               "title":"Policy"
            },
            "type":[
               "object",
               "null"
            ]
         },
         "type":[
            "object",
            "null"
         ]
      }
   },
   "type":"object",
   "title":"Resourcename"
}

from jsonschema.

Tloe avatar Tloe commented on July 30, 2024

Cool thanks will check it out.. on my phone atm. Thanks again :)

from jsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.