json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

Validate nullable enhancement

Open jiachen1120 opened this issue 6 years ago • 5 comments

There are two concerns related to nullable validation:

[1] When verifying the response, if the value corresponding to a field is null and the nullable is not set in specification. The verification will not pass. Even this field is not required.

For example, assuming that we want get a pet object from the response and validating the response by using the following schema

Pet: 
  type: object
  required:
    - name
  properties:
    id:
      type: string

And the response we got is {"name": "dog", "id": null}, the validation would fail due to the nullable default value is false. If we want to pass the validation, the specification should be correct in this way:

Pet: 
  type: object
  required:
    - name
  properties:
    id:
      type: string
      nullable: true

This may not be very reasonable. When setting required, the user needs to set nullable to true at the same time if they would receive a null field. This may increase the complexity of the specification. So I wonder if we can set the default value of nullable to true?

[2] Nullable cannot be recognized when using following specification:

  properties:
    id:
      $ref: '#/components/schema/id'
      nullable: true

The reason is that when using JsonOverlay.toJson() to get JsonNode, the nullable would be erased if this field need to refer others. One easy way is to suggest user to use following specification

 properties:
   id:
     oneOf:
     - $ref: '#/components/schema/id'
     - nullable: true

Or, we need to modify JsonOverlay

Look forward to your ideas!

jiachen1120 avatar Mar 27 '19 19:03 jiachen1120

@jiachen1120 Is this issue only related to the response validation? What about request validation?

stevehu avatar Mar 27 '19 20:03 stevehu

@stevehu Hey, I just test the request validator. These also happen when validating request. Since both of them are using typeValidator to handle the nullable.

jiachen1120 avatar Mar 27 '19 20:03 jiachen1120

Based on my understanding after reading the definition of reference object the "$ref" should be standalone, shouldn't add extra properties to it. so I think the second is more appropriate:

 properties:
   id:
     oneOf:
     - $ref: '#/components/schema/id'
     - nullable: true

also I noticed there is a property named: additionalProperties, maybe we can also try if this one can add extra property to a ref schema

{
  "type": "object",
  "additionalProperties": {
    "$ref": "#/components/schemas/ComplexModel"
  }
}

BalloonWen avatar Apr 02 '19 17:04 BalloonWen

Hello,

I think there should be an option to allow null value on non required type as valid, there is many tools/schema which consider non required field as equivalent to null value allowed for the field.

ylemoigne avatar Aug 05 '19 21:08 ylemoigne

I am open to modifying the JsonOverlay if it is necessary. At the moment, we are trying to satisfy both OpenAPI 3.0 and JSON Schema specifications and I would say we stick to JSON Schema by default; however, we can optionally implement extensions that can be enabled by configuration. Although the library was started with JSON Schema v4, we have implemented a lot of specification for v4-v8 (pending release). So when we use specifications as references, we can check multiple versions instead of only one.

stevehu avatar Aug 05 '19 23:08 stevehu