omissis/go-jsonschema

Handling of `required` properties in objects

Henkoglobin opened this issue · 1 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.

I have fixed this in my own fork for now: https://github.com/klippa-app/go-jsonschema/blob/feature/required-fix/pkg/generator/validator.go#L38

I did not add the actual null check there because I have no need for it.