question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using `allOf` and `nullable` in combination yields unexpected validation errors

See original GitHub issue

What version of Ajv are you using? Does the issue happen if you use the latest version? I am using oas3-chow-chow which is using version ^6.12.3

Ajv options object oas3-chow-chow defines the AJV options as seen here.

const options: Ajv.Options = {
  /**
   * Ignore following formats for now because they are not supported by AJV by default.
   * TODO: Add custom format supports for following formats.
   */
  unknownFormats: [
    'int32',
    'int64',
    'float',
    'double',
    'byte',
    'binary',
    'password',
  ],
  nullable: true,
  jsonPointers: true,
};

export default function ajv(opts: Ajv.Options = {}) {
  return new Ajv({
    ...options,
    ...opts,
  });
}

JSON Schema Unfortunately I’m locked into using OAS 3.0.x and have to use the "nullable": true declaration

"MyArrayOfReferences": {
    "type": "array",
    "example": [
      "prefix/14691d5de8f424c75558dd7375",
      "prefix/6591827d39d443e983199e1cb7",
      "prefix/ec403e0c81074d8db34a6c9431"
    ],
    "items": { "type": "string" }
},
"UpdateMyArrayOfReferences": {
    "properties": {
        "myArrayOfReferences": {
          "description": "Group settings. Set to `null` to remove.",
          "allOf": [
            {
              "$ref": "#/components/schemas/MyArrayOfReferences"
            }
          ],
          "nullable": true
        }
    }
}

Sample data

{
    "myArrayOfReferences": null
}

Validation result, data AFTER validation, error messages

"dataPath": "/myArrayOfReferences",
"error message": "RequestValidationError: Schema validation error: type should be array",

What results did you expect? I expect to be able to provide a null value without producing a validation error.

It seems related to this issue #983 because if I remove the $ref and make everything inline, then the null value is accepted.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
crizo23commented, Aug 11, 2021

Helping out @denver-HJS here.

Here is a runkit link: https://runkit.com/crizo23/611437b007a03a001a5b80e7 I couldn’t import AJV v6 though, so it won’t compile. I pasted the output I get on line 104

0reactions
epoberezkincommented, Nov 13, 2022

I believe this is by design, “nullable” was defined as an extension of type keyword, not as an extension of the schema. If you have a schema that doesn’t allow null and you want to accept null you can just do:

{
  anyOf: [
    {$ref: "some_schema"},
    {const: null}
  ]
}

Below is not supposed to work:

{
  $ref: "some_schema",
  nullable: true
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Schema puts null values when validation fails on ...
When I use this online schema validator the validation fails as expected when the response doesn't match one of the schemas provided in...
Read more >
Modifying data during validation - Ajv JSON schema validator
Unexpected results when modifying data. While pure schema validation produces the results independent of the keywords and subschema order, enabling any ...
Read more >
Solved: Parse JSON - validation failed for null values - h...
Solved: I have a PowerApps instant cloud flow which receives a JSON array as a string I am attempting to parse the JSON,...
Read more >
gMock Cookbook | GoogleTest
You can build complex matchers from existing ones using AllOf() ... An unexpected call is always an error, as the code under test...
Read more >
OpenAPI Specification - Version 2.0 - Swagger
Swagger allows combining and extending model definitions using the allOf property of JSON Schema, in effect offering model composition. allOf takes in an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found