What would be the best practice of handling input validation in microservice? Especially for the duplicated data?
To give context, say I have 3 services:
User
Typical user service with User
object with a lot of details (~40 fields in the object)
Asset
Has Asset
object like
{
id,
name,
companyId,
descripton
}
News
News service has a Feed
object that have reference to both User
and Asset
however for News it only care subset of both User
and Asset
fields (ie. only need 10 of User
field)
{
id,
title,
description,
asset,
user
I'm aware of the concept that News should have its own view of User and Asset and data consistency can be done through message hub. So the question now becomes if I have an http post request for new feed so the request body looks something like
{
"title": "title",
"description":"description",
"asset": {
"id": "asset1",
"name": "phone",
"companyId": "company-1"
},
"user": {
"id: "user-1",
"name": "name",
"comapnyId": "company-1",
"roles": ["reporter"]
}
}
How should I perform the input validation for both user and asset field in News service? Say the input validation for user is something like:
- user role can't be mix of reporter and manager (either one)
- name < 120 char
- companyId must be null if user role is a client, otherwise it must be set
Should I then have this input validation sits in both News and User service? Using a common/shared object with validation seems simpler but I always thought it's kind of antipattern or am I missing something here?
PS - I tried to avoid direct service to service call (also client can't perform multiple requests due to limited network bandwidth) so just storing assetId and userId in the Feed won't work
PSS - the backend code is written in Go so OOP approach may not be best suited
user-1
is a reporter onnews-1
and a manager onnews-2
)? What should happen if the User service knowsuser-1
by the name "John Doe" and the News service gets the request as you showed in the question (so a different name)? What if I use different names for the same user-id in requests to the News service?