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.

Schema validation in custom transactions can easily be broken

See original GitHub issue

There is an AJV bug, that they consider as a feature. See here. Especially check the latest comment written by wigy.

As you initiate AJV with removeAdditional: true, it means if our custom transaction schema is similar to the one in the issue I linked, than it will easily fail if we use anyOf with additionalProperties: false in the items.

Example:

...
return schemas.extend(schemas.transactionBaseSchema, {
  $id: this.key,
  required: ['asset','type','typeGroup'],
  properties: {
    type: { transactionType: this.type, },
    typeGroup: { const: this.typeGroup, },
    amount: { bignumber: { minimum: 0, maximum: 0 } },
    asset: {
      required: ['operationAttempts'],
      additionalProperties: false,
      properties: {
        operationAttempts: {
          type: 'array',
          items: {
            anyOf: [
              { required: ['propA'], additionalProperties: false, properties: { propA: { type: 'string', const: 'A' } } },
              { required: ['propB'], additionalProperties: false, properties: { propA: { type: 'string', const: 'B' } } }
            ]
          }
        }
      }
    },
  },
});
...

Transaction:

{
...
operationAttempts: [{ propB: 'B' }]
...
}

If we send in a transaction (see above) where operationAttempts must mach on the second schema from anyOf, it will fail, as we the AJV is initialized with removeAdditional: true and we declared additionalProperties: false at all level. It will fail against the first schema, it will remove then the not wanted property (propB), hence it will fail against the second schema as well, because the object now does not have propB which is not the expected behavior.

Expected Behavior

I would expect that properties on data will not be removed during schema validation iteration.

Current Behavior

See above.

Possible Solution

Initiate AJV without removeAdditional: true.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
air1onecommented, Dec 11, 2019

Hey thanks @mudlee for pointing out this issue. Indeed I wasn’t expecting AJV to remove properties before checking against all possibilities defined in anyOf.

I checked for core repository and all schemas look “safe”. Now it would be nice to remove removeAdditional: true as you suggested, but it can have big impact, like blocks or transactions not being accepted anymore. So we need to be very careful before doing this, I will check with the team see what we do.

0reactions
wigy-opensource-developercommented, Dec 12, 2019

Yeah, I somewhat expected this outcome, this decision makes complete sense. We will try to contribute to the documentation to save other devs the 3-4 hours we spent on narrowing down the validation issue we had.

In Ajv with this configuration if an asset has a heterogenous collection (something that needs anyOf) that needs a discriminator on each item, our current understanding is to use a key rather than a value for discriminating among the cases and hide all properties specific to that case under the key.

Something like this:

{
  "modes": [
    { "ftp": { "account": "blah" }},
    { "email": { "mailingList": "foobar" }}
  ]
}

Rather than this:

{
  "modes": [
    {
      "mode": "ftp",
      "account": "blah"
    },
    {
      "mode": "email",
      "mailingList": "foobar"
    }
  ]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Correct JSON Schema for an array of items of different type
I don't want to specify an order, just validate the objects within the array, regardless of order or number of objects. From the...
Read more >
Multiple array items type validation with oneOf, anyOf #134
Hi everyone, I've encountered a problem to validate multiple items type in ... Schema validation in custom transactions can easily be broken ......
Read more >
Validate JSON Documents in Python using Pydantic
This article shows you how to validate your JSON documents against a specified schema using the popular Python library pydantic.
Read more >
How To Use Joi for Node API Schema Validation - DigitalOcean
In this tutorial, you have created schemas for validating a collection of data using Joi and handled request data validation using a custom...
Read more >
Custom Connector - Schema Validation Error
Hi, Hoping someone can help me here with a custom connector. Going bananas! The call itself works and is returning data when testing, ......
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