go-jsonschema icon indicating copy to clipboard operation
go-jsonschema copied to clipboard

Handling of `required` properties in objects

Open Henkoglobin opened this issue 2 years ago • 0 comments

According to §6.5.3 of the JSON Schema Validation spec, required properties need merely be present in the input JSON, they are not required to be non-null.

As such, the validation generated by go-jsonschema is too strict, as it checks for presence (which is correct) but also validates that the value is not nil.

For example, with the following schema:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "RequiredObjectPropertiesTest",
    "properties": {
        "requiredNillableProp": {
            "type": [
                "string",
                "null"
            ]
        }
    },
    "required": [
        "requiredNillableProp"
    ]
}

This input is valid:

{
  "requiredNillableProp": null	
}

But an empty JSON object {} is not.

As such, properties marked as required should generate without omitempty and should be checked for presence, but not for non-nil.

Henkoglobin avatar Jul 14 '23 11:07 Henkoglobin