Closed RafaelRfs closed 1 month 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).
Hi @CarstenWickner, thanks for the answer for this issue, i'll try the methods that you said.
Sincerely,
Rafael F. S,
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:
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