victools / jsonschema-generator

Java JSON Schema Generator – creating JSON Schema (Draft 6, Draft 7, Draft 2019-09, or Draft 2020-12) from Java classes
https://victools.github.io/jsonschema-generator
Apache License 2.0
398 stars 58 forks source link

Please provide a example with optional mandatory dependencies, only mandatory if the on field in the object is a determined value #470

Closed RafaelRfs closed 1 month ago

RafaelRfs commented 2 months ago

Hi, how are you? I would like to thank you about the awesome job that you are doing.

I recently was trying to implement your dependency in the professional application that i doing the customer and one issue was appeared, i'm trying to create a json schema by the generator like this:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "user": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                },
                "age": {
                    "type": "integer",
                    "minumum": 0
                },
                "email": {
                    "type": "string"
                },
                "function": {
                    "type": "string",
                    "enum": [
                        "ADMIN",
                        "CUSTOMER",
                        "DEVELOPER"
                    ]
                },
                "address": {
                    "type": "string"
                }
            }
        }
    },
    "dependencies": {
        "user": {
            "properties": {
                "function": {
                    "const": "ADMIN"
                }
            },
            "required": [
                "address"
            ]
        }
    }
}

But I am having to generate the dependencies of the optional field function on the user, the address is only mandatory when the user is "ADMIN", otherwise the address is not mandatory, can you provide an example with a similar configuration with dependencies please ?

Sincerely

Rafael

CarstenWickner commented 2 months ago

Hi @RafaelRfs,

You're very welcome.

Your use of "dependencies" doesn't seem quite right. Perhaps you're after something like the following (inside the "user" schema)?

    "if": {
        "properties": {
            "function": {
                "const": "ADMIN"
            }
        },
        "required": ["function"]
    },
    "then": {
        "required": ["address"]
    }

Remember to include "required": ["function"] in order only to enforce the mandatory address when the "function" property is present (and has the value "ADMIN").

That said, a very similar question had been raised on https://github.com/victools/jsonschema-generator/issues/121. There, I avoided the "if"/"then" keywords, but I suppose they are appropriate here. In response to https://github.com/victools/jsonschema-generator/discussions/328 I already introduced an example showcasing if´ /then/else` utilizing custom annotations in your code – thereby also answering the question of how this kind of condition should be captured in the code, to not hard-code these only in the schema generator's configuration.

I suggest you look at the latter IfThenElseExample and adapt it to your needs (replacing the @SchemaProperty.pattern with an indication of the "required" properties).

RafaelRfs commented 1 month ago

Hi @CarstenWickner, thanks for the answer for this issue, i'll try the methods that you said.

Sincerely,

Rafael F. S,