Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question / FeatureRequest] Maximum and minimum: { $ref } #943

Closed
mansdahlstrom1 opened this issue Jun 10, 2020 · 7 comments
Closed

[Question / FeatureRequest] Maximum and minimum: { $ref } #943

mansdahlstrom1 opened this issue Jun 10, 2020 · 7 comments

Comments

@mansdahlstrom1
Copy link

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

@mansdahlstrom1 mansdahlstrom1 changed the title Maximum and minimum: { $ref } [Question / FeatureRequest] Maximum and minimum: { $ref } Jun 10, 2020
@handrews
Copy link
Contributor

@oliviassss
Copy link

@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
Copy link
Member

@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
Copy link

@gregsdennis, thank you. I'm new to json, how can I adapt to the $data vocabulary?

@gregsdennis
Copy link
Member

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
Copy link

@gregsdennis, thank you. This is my schema

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "definitions": {
      "my-obj": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
              "replicaCount": {
                  "type": "integer",
                  "default": 2,
                  "data": {"minimum": "/autoScaling/minReplicas",  "maximum": "/autoScaling/maxReplicas" }
              },
              "autoScaling": {
                  "type": "object",
                  "properties": {
                      "enabled": {
                          "type": "boolean",
                          "default": false
                      },
                      "minReplicas": {
                          "type": "integer",
                          "minimum": 0,
                          "default": 2,
                          "data": { "maximum": "/maxReplicas" }
                      },
                      "maxReplicas": {
                        "anyOf": [
                          {
                            "type": "integer",
                            "data": { "minimum": "/minReplicas" }
                        },
                        {
                            "type": "null"
                        }
                        ]
                      }
                  },
                  "description": "Auto scaling config"
              }
          },
          "title": "my-obj"
      }
  }
}

The autoScaling.maxReplicas has no default, so it could be none. I want to make sure autoScaling.minReplicas <= autoScaling.maxReplicas when autoScaling.maxReplicas is integer, and replicaCount should be in the range [autoScaling.minReplicas, autoScaling.maxReplicas]

@gregsdennis
Copy link
Member

gregsdennis commented Feb 15, 2024

Okay. First, here's the schema, but there are a few things to note:

{
  "$schema": "https://json-everything.net/meta/data-2023",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "replicaCount": {
      "type": "integer",
      "default": 2,
      "data": {"minimum": "/autoScaling/minReplicas"},
      "optionalData": {"maximum": "/autoScaling/maxReplicas" }
    },
    "autoScaling": {
      "type": "object",
      "properties": {
        "enabled": {
          "type": "boolean",
          "default": false
        },
        "minReplicas": {
          "type": "integer",
          "minimum": 0,
          "default": 2,
          "optionalData": { "maximum": "/autoScaling/maxReplicas" }
        },
        "maxReplicas": {
          "anyOf": [
            {
              "type": "integer",
              "data": { "minimum": "/autoScaling/minReplicas" }
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "description": "Auto scaling config"
    }
  },
  "title": "my-obj"
}
  1. Vocabs are only defined for drafts 2019-09 and 2020-12. I don't think they'll work for draft 6 (which is what you posted).
  2. Your schema doesn't actually do anything. It contains a definition ($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.
  3. The JSON Pointer references that you're using in the schemas for minReplicas and maxReplicas 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.)
  4. Since maxReplicas may not have a value, you want to use optionalData instead of data. The difference is that optionalData won't throw an error when it can't resolve the reference, but data will throw an error.
  5. Since you're referencing the value in /autoScaling/maxReplicas to be used as the value for the maximum keyword, that value MUST be a number. So allowing maxReplicas 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants