marshmallow-jsonapi icon indicating copy to clipboard operation
marshmallow-jsonapi copied to clipboard

Dasherized relationship names are marked as an attribute for validation errors.

Open scottwernervt opened this issue 7 years ago • 0 comments

Validation errors, e.g. validate=[<marshmallow-available-valdiator>, <raise ValidationError>] for a relationship sets the error source pointer path as an attribute instead of relationship. This issue occurs when you use inflect = dasherize.

Bug {'pointer': '/data/attributes/article-author'}

Expected {'pointer': '/data/relationships/article-author/data'

Workaround For the relationship field name, use a single word without any underscores: article_author to author.

Repeat Issue

from marshmallow.validate import OneOf
from marshmallow_jsonapi import Schema
from marshmallow_jsonapi.fields import Relationship, Str

AUTHOR_CHOICES = ['2']


def dasherize(text):
    return text.replace('_', '-')


class AuthorSchema(Schema):
    id = Str()
    first_name = Str()
    last_name = Str()

    class Meta:
        type_ = 'authors'
        inflect = dasherize


class ArticleSchema(Schema):
    id = Str()
    title = Str()

    article_author = Relationship(
        type_='authors',
        schema='AuthorSchema',
        allow_none=True,
        validate=[OneOf(choices=AUTHOR_CHOICES, error='Not a choice')],
    )

    class Meta:
        type_ = 'articles'
        inflect = dasherize


input_data = {
    'data': {
        'type': 'articles',
        'attributes': {'title': 'Article One'},
        'relationships': {
            'article-author': {
                'data': {'type': 'authors', 'id': '1'}
            }
        },
    }
}

data, errs = ArticleSchema().load(input_data)
print(data)
>>> {'title': 'Article One'}
print(errs)
 >>> {'errors': [{'detail': 'Not a choice', 'source': {'pointer': '/data/attributes/article-author'}}]}

scottwernervt avatar Jul 13 '16 19:07 scottwernervt