Search code examples
marshmallow

Is there a way for testing for uniqueness of a field in a collection of records in marshmallow


Is there a standard way to validate the uniqueness of a field in a record upon loading?

For example, if validating for a unique id in the list below, the last two records in this structure would trigger a failure.

[
  {
    "id": 1,
    "stuff": "abc"
  },
  {
    "id": 2,
    "stuff": "abc"
  },
  {
    "id": 2,
    "stuff": "abc"
  }
]

I found a comment on an issue from 2016 suggesting that it is not, has anything changed in that time?

Related question: Validate list in marshmallow

https://github.com/marshmallow-code/marshmallow/issues/541


Solution

  • There is no standard way as of today. The closest to a conversation about this would be in this PR: https://github.com/marshmallow-code/marshmallow/pull/1793.

    It is not that hard to implement in your own code but we haven't been able yet to come up with a generic enough implementation.