-
Hi, I'm just getting started with schema validation. Nice concept, thanks! I know I'm doing this wrong, but what would be the correct way of doing this? I want a property resource_url
This is my attempt, it doesn't work: {
"$id": "https://test.com/optional_value.json",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"description": "Test",
"properties": {
"resource_url": {
"description": "An optional string, should match the uri format if supplied"
}
},
"type": "object",
"anyOf": [
{
"if": {
"properties": {
"resource_url": {
"type": "string",
"minLength": 1
}
}
},
"then": {
"properties": {
"resource_url": {
"format": "uri"
}
}
}
},
{
"if": {
"properties": {
"resource_url": {
"type": "string",
"maxLength": 0
}
}
},
"then": {
"properties": {
"resource_url": {
"maxLength": 0
}
}
}
},
{
"if": {
"properties": {
"resource_url": {
"type": "null"
}
}
},
"then": {
"properties": {
"resource_url": {
"type": "null"
}
}
}
}
]
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @careri , I think you can simplify your schema a lot by making use of {
"$id": "https://test.com/optional_value.json",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"description": "Test",
"properties": {
"resource_url": {
"description": "An optional string, should match the uri format if supplied",
"anyOf": [
{ "type": "null" },
{ "const": "" },
{ "type": "string", "minLength": 1, "format": "uri" },
]
}
}
} Though keep in mind that not all JSON Schema implementations validate |
Beta Was this translation helpful? Give feedback.
Hi @careri , I think you can simplify your schema a lot by making use of
anyOf
instead ofif
/then
/else
. For example:Though keep in mind that not all JSON Schema implementations validate
format
by default (some only report it as metadata, which we call "annotations" in JSON Schema),…