Doing partial PATCH with Type System


#1

Hello there, how are you doing?

So, I want to have a PATCH endpoint and be able to change the resource partially using theTypes.

Here’s my Type:

class User(types.Type):
    id = validators.Integer()
    username = validators.String(max_length=100)

Here’s my endpoint (kept it more brief):

def edit_user(username: str, user: User) -> User:
    user_to_edit = get_user(username)
    if not user_to_edit:
        raise exceptions.NotFound()

    user_to_edit.username = user.username

    return user_to_edit

The problem is that the id field is required, so if I send just the username to be changed, the validation system fails before.

Is there a way to solve this?

Thanks!


#2

Id = Integer(allow_null=True)


#3

But I’d like to have this required on a POST, for example, using the same Type. Is that possible?


#4

I am also struggling with this. On v0.3.9 I had a:

class UserPatch(typesystem.Object):
    properties = {
        'id': typesystem.Integer,
        'username': typesystem.String,
    }

class User(UserPatch):
    required = UserPatch.properites

But in v0.4.0+ I had to resort to much code duplication for now, and hope to find some better solution later.