Comments (10)
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.
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.
Yes. You should use oneOf keyword for or behaviour
from jsonschema.
Ah, you mean instead of | in patternProperties? I guess if I got you right that would make it easier indeed. :)
from jsonschema.
{ "oneOf":[
{"properties": { "x": { "type": "string" } }},
{"patternProperties": { "x*": { "type": "number" } } }}
]}
from jsonschema.
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.
You are right. It does not seem straight forward
from jsonschema.
Thanks anyway, I know why its not working and should be able to find a solution now :)
from jsonschema.
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.
Cool thanks will check it out.. on my phone atm. Thanks again :)
from jsonschema.
Related Issues (20)
- Link in README for YAML playground example is broken HOT 1
- Issue with custom Regex Library HOT 10
- `Compiler.AddResource` for adding pre-compiled schemas HOT 6
- Feature: Adding Context to Validate Method HOT 2
- implementing json.Marshaler interface HOT 9
- Create JSONSchema from Compile()'s result HOT 2
- Wrong unevaluatedProperties validation errors HOT 6
- compiler.go bug in 171
- Invalid JSON schema does not error out HOT 2
- [bug] Not reading from stdin HOT 2
- Missing document file path in validation output when validating multiple documents HOT 5
- Error messages are revealing absolute file paths HOT 1
- YouValidateMe JSON Schema library server project built on this library
- ECMA-262 as the default regex in this library? HOT 1
- jsonschema.Compiler has no field or method CompileRegex HOT 7
- Question: parsing Schema to string HOT 1
- error is missing instanceLocation HOT 1
- Cannot install older version than `@latest` HOT 3
- The latest release doesn't build HOT 2
- Extended the implementation of a new custom validation traversal logic(I'm not sure if there are any other side effects of this)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonschema.