Closed
Description
Been using json-schema for a while now and I'm currently looking for ways to set dynamic "minimum"
and "maximum"
properties. My use case is that the values of one property can never be lower than a value of another property. Something like this
"minValue": {
"$id": "#minValue",
"type": "number",
"minimum": 0,
"maximum": { "$ref": "#maxValue" }
},
"maxValue": {
"$id": "#maxValue",
"type": "number",
"maximum": 100,
"minimum": { "$ref": "#minValue" }
}
This does not seem to be supported currently by draft-07 as i found during testing and some more resources here
is there any plan to include a feature like this for upcoming drafts?
Thanks
Activity
[-]Maximum and minimum: { $ref }[/-][+][Question / FeatureRequest] Maximum and minimum: { $ref }[/+]handrews commentedon Jun 25, 2020
See #855 json-schema-org/json-schema-vocabularies#26 and json-schema-org/json-schema-vocabularies#25
oliviassss commentedon Feb 13, 2024
@handrews @mansdahlstrom1, hi I have the same question, and kind of getting lost in the discuss threads...
Is the cross-field validation supported by json schema?
I'm new to json schema, for my use case, I have 3 fields, "minCount", "maxCount" and "specifiedCount". I want specifiedCount to be btw the minCount and maxCount, and neither of the 3 fields are required, not sure if it's supported, and how to achieve that? Thanks
gregsdennis commentedon Feb 14, 2024
@oliviassss this can't be done with JSON Schema without some help.
As I answered here, I have a vocabulary that can do what you're looking for, but that's the current extent of support.
oliviassss commentedon Feb 15, 2024
@gregsdennis, thank you. I'm new to json, how can I adapt to the
$data
vocabulary?gregsdennis commentedon Feb 15, 2024
A couple things:
First, I need to see what you're actually trying to do.
Second, the keyword is
data
, not$data
(no$
). There was a$data
proposal (which some implementations support), but it results in invalid schemas, so I've designed mine differently.oliviassss commentedon Feb 15, 2024
@gregsdennis, thank you. This is my schema
The
autoScaling.maxReplicas
has no default, so it could be none. I want to make sureautoScaling.minReplicas <= autoScaling.maxReplicas
whenautoScaling.maxReplicas
is integer, and replicaCount should be in the range [autoScaling.minReplicas, autoScaling.maxReplicas]gregsdennis commentedon Feb 15, 2024
Okay. First, here's the schema, but there are a few things to note:
$defs
for drafts 2019-09 and 2020-12), but nothing is referencing that definition. Definitions on their own do nothing. I've moved the subschema to be the root so that validation can actually occur.minReplicas
andmaxReplicas
aren't relative to those properties' positions in the instance; they're relative to the root of the instance. (Relative JSON Pointer is supported, though, if you want that.)maxReplicas
may not have a value, you want to useoptionalData
instead ofdata
. The difference is thatoptionalData
won't throw an error when it can't resolve the reference, butdata
will throw an error./autoScaling/maxReplicas
to be used as the value for themaximum
keyword, that value MUST be a number. So allowingmaxReplicas
to be null will fail. Instead of allowing it to be null, just prefer that it be missing.This is working at https://json-everything.net/json-schema.