Way to validate a parent/child relation


I have a rest endpoint for example /user/{userId}/message/{messageId}

I have the possibility to create both ID’s as I want.

How can I create messageId and know the message is belong to this user, without checking in database or any service?

I think about hashing userId and add the hash as prefix of messageId.