pboettch / json-schema-validator

JSON schema validator for JSON for Modern C++
Other
466 stars 134 forks source link

Default value lost via references to schema in other files #93

Closed DonKult closed 4 years ago

DonKult commented 4 years ago

Hi, (thanks for this project! I found it a few days ago and already love it)

The recently added patching in of default values is nice, even better if "foo": { "$ref": "#foobar", "default": "bar" } would work but I guess I have to wait for the next draft to be implemented (were it is allowed, while draft7 forbids it).

What doesn't seem to work is referencing a schema from an other file and having the default value as specified in that schema respected. It does seem to work with definitions in the same file though as I couldn't manage to adapt the issue-25 test to fail.

I think it is due to schema_ref::defaultValue() missing a return in the if-branch resulting in always returning the empty value, but I could be wrong & don't know the implications this might have on the rest.

Thanks & Best regards

pboettch commented 4 years ago

@Finkman could you take a look, please?

Finkman commented 4 years ago

@Finkman could you take a look, please?

Yes, I do. There is a bit struggling with the lockdown in my area, but as soon as I am prepared I'll get into it.

pboettch commented 4 years ago

Thanks, take your time, there is no hurry. I'm locked-down as well. In my case, it's an OK situation, but for others, it is a hard time.

Finkman commented 4 years ago

Can someone provide a small example how to use the validator with "schema from another file"? I need to know what to set up to test and develop against, but have no glue about the use case.

DonKult commented 4 years ago

Sure, my (heavily condensed) use case is as follows: blueprints.json: [{"name":"player","renderable":{"fg":"White"}}] blueprints.schema.json: {"type":"array","items":{"type":"object","properties":{"renderable":{"$ref":"/components.schema.json#/Renderable"}}}} components.schema.json: {"Renderable":{"type":"object","properties":{"fg":{"$ref":"/types/color.schema.json"},"bg":{"$ref":"/types/color.schema.json"}}}} types/color.schema.json: {"type":"string","default":"Black"}

~/path/to/json-schema-validate blueprints.schema.json < blueprints.json says the document is valid, but the patch (if modified to print it) is null. If I add the return as mentioned above I get the patch I was expecting: [{"op":"add","path":"/0/renderable/bg","value":"Black"}] – or am I expecting too much? (as said, ideally I would like to have the default setable in fg and bg as that makes more sense but that isn't legal in draft-7, only in the next draft).

(I am in quasi-lockdown here, too, so no worries, I am not in a hurry & take good care of yourself and others important to you! Silly internet people like myself with their issue reports will still be there after everything important is taken care of. :wink: Stay safe & healthy everyone!)

pboettch commented 4 years ago

I was curious and created a test for this issue and I can confirm that it is probably related to the missing return in the if-branch.

I even found a second missing return in the class root_schema.

pboettch commented 4 years ago

I made the fix and added a test-case. Please feel free to review, thanks for reporting.

DonKult commented 4 years ago

Can confirm that the branch works for me. Thanks for taking a look at this you two!

Finkman commented 4 years ago

I've never earned so much thankfulness for so less effort. :smile: Thank you guys, too.