Microservice design - validation

We have a product where 4 microservices are communicating with each other.
Let's say entry point receives a payload as below:
{
"Employees": [
{
"userId": "rirani",
"jobTitleName": "Developer",
"firstName": "Romin",
"lastName": "Irani",
"preferredFullName": "Romin Irani",
"employeeCode": "E1",
"region": "CA",
"phoneNumber": "408-1234567",
"emailAddress": "romin.k.irani#gmail.com"
}
]
}
The 1st microservice does validation on the payload "employeeCode" it allows only employee code which. starts with "E" else returns 400 status code. The Same payload is forwarded to other componet as well if all validation passes.
Now my question is tomorrow there could be fair chance that external service consumes our any of the components to process their request. Do we need to have the validation in all the components?
Now there is no external client which uses our components but in the future maybe.
It's a more of a design question. Do we need to make our validation as a jar or we should not duplicate the validation itself? Its client responsibility to send the correct data if they are calling any other component except Microservice-1.
Thanks, in advance.

Related

How mention a group of teams in a flow of power automate?

I create a group for question in Microsoft teams, the name for this group is "Information Comunity". In this group, I put a Microsoft forms to capture the question.
Then, I create a Flow with Microsoft power Automate. This flow send send a message in "information Comunity" group, like this
I need to mention a other group in this message, the name of the other group is "Gerencia de information", this group will answer the question. But I donĀ“t know how to capture the name of thisgroup or how i get the id of the other group.
Can you help me please.
Thanks for your help.
1 The Flow will use a manual trigger.
2.After that the Flow initializes three variables as below:
3.First make sure that you invoke the HTTP request action connection using https://graph.microsoft.com as the Base Resource URL and Azure AD Resource URI values like in the screenshot below:
4.Then invoke the HTTP request
Body of the request:
{
"body": {
"content": "This is Flow mention channel test, <at id=\"0\">RA Experts</at>",
"contentType": "html"
},
"mentions": [
{
"id": 0,
"mentionText": "RA Experts",
"mentioned": {
"conversation": {
"id": "19:ac7b9c53a099498f9e08679e58f1f7fc#thread.tacv2",
"displayName": "RA Experts",
"conversationIdentityType": "channel"
}
}
}
]
}
Result:

What is the proper format to expect when using an api to create a resource that references another resource

I am adding a feature that allows users to select from a list of people of a certain type, Type1 and Type2. A type would be chosen from a dropdown, and the data from the API would look like
{
"id": 1,
"name": "TYPE1",
"desc": "Type 1 Person"
}
I am creating a POST endpoint that allows an admin user to insert more people into the list, but I'm unsure on the best way for the admin to include the person's type. In other languages/frameworks, I would do something like this:
{
"first_name": "John",
"last_name": "Doe",
"type_id": 1
}
then handle adding the entry in my own SQL. In Spring though, I'm trying to leverage an object being created from the data automatically. For this to be successful, I've need to send the data as:
{
"first_name": "John",
"last_name": "Doe",
"type": {
"id": 1,
"name": "TYPE1",
"desc": "Type 1 Person"
}
}
My question is in two parts.
In Spring, is there anything I can leverage that would allow me to just pass an identifier for person type when creating a new person entry? (I've looked into DTOs, but I've never used them, so I don't know if that is the proper solution)
In REST in general, how much data should be required when adding a resource that references another resource?

How to remove hypermedia elements from representations produced by Spring Data REST?

When using Spring Data for my REST API, the responses returned currently include the _links field:
{
"_embedded": {
"users": [
{
"imageUrl": "some_image_url",
"name": "some name",
"id": "57420b2a0d31bb6cef4ee8e9",
"_links": {
"self": {
"href": "http://localhost:8080/users/57420b2a0d31bb6cef4ee8e9"
},
"user": {
"href": "http://localhost:8080/users/57420b2a0d31bb6cef4ee8e9{?projection}",
"templated": true
}
}
},
...
Is there a way to produce output, such that the _links field is hidden? e.g.:
{
"_embedded": {
"users": [
{
"imageUrl": "some_image_url",
"name": "some name",
"id": "57420b2a0d31bb6cef4ee8e9",
},
...
I find that because I am exposing the id field, _links are not really necessary, and mostly just clutter up my responses.
There isn't. Hypermedia is a fundamental trait of REST APIs and Spring Data REST heavily uses it to allow you to build clients that can use the links present in the responses to navigate to related resources.
Of course you can dumb down your clients to not make use of that information but that will lead to a much tighter coupling (as you can't change the URIs on the server side anymore, your clients expects to talk to a dedicated server whereas with hypermedia you can just point it to a different server etc.).
In contrast to a lot of other self-proclaimed REST frameworks out there, one of the key aspects of the framework's design is to respect the fundamental principles in REST and explicitly leverage them. Or at least, don't create incentives to easily break them. This is clearly expressed in the reference documentation and on the project website. Find out more about key design decisions in this presentation on Spring Data REST, and this one on Domain-Driven Design & REST.

ember-data - best strategy for loading complex data structures

I am using ember-data 2.0. I want to load all data from the server at startup. Then the app is really fast after the initial loading. The application is mobile, and may have varying bandwidth. I want to load the data as fast as possible. I want to use JSON API to comply with the newest ways of using ember-data.
Each object is small, just a handful of attributes. But there may be 200 such objects to load.
Is it best to do many small ajax calls or one big one? I imagine it is faster to use one big call that contains all my objects, but is that possible with the JSON API and the out-of-the-box adapters?
Of course, I could just use findAll() but that will only load all objects of one type. I have several object types in my model that are connected with hasMany and belongsTo relationships.
If I use the RestAdapter out of the box, that will result in many ajax calls, right?
What is the best strategy to use and how to implement that with ember-data and the adapters?
You can load different types to the store from single payload out of the box with Ember Data. By default JSON serializer assumes that each root node in payload corresponds to a type.
Let's say you have models:
/models/post.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
body: DS.attr('string'),
comments: DS.hasMany('comment');
});
/models/comment.js
import DS from 'ember-data';
export default DS.Model.extend({
comment: DS.attr('string'),
post: DS.belongsTo('post');
});
And you have an endpoint /posts which responds with payload like this:
{
"posts": [
{
"id": 1,
"title": "Post 1",
"body": "Post 1 body",
"comment_ids": [1, 2]
},
{
"id": 2,
"title": "Post 2",
"body": "Post 2 body",
"comment_ids": [3]
}
],
"comments": [
{
"id": 1,
"comment": "Comment 1",
"post_id": 1
},
{
"id": 2,
"comment": "Comment 2",
"post_id": 1
},
{
"id": 3,
"comment": "Comment 3",
"post_id": 2
}
]
}
So when you do store.findAll('post'), Ember Data will request /posts endpoint, recieve the payload and push both posts and comments to the store. And all relations will be resolved properly.
But there are some notes:
if you define model relation with {async: true}, Ember data will always hit the server to fetch relation records.
Everywhere in the app you would need to use store.peek() and store.peekAll() instead of store.find() and store.findAll() in order to get records from the store but not from server.

How to handle (failed) validation when creating multiple entities with a REST API

Let's say I have an endpoint user/1/results and I want to upload multiple results at a time.
So I send it JSON like:
{
"data": [
{
"date": "2014-02-14 03:15:41",
"score": 18649,
"time": 42892
},
{
"date": "2013-11-18 09:21:46",
"score": 7856,
"time": 23568.8
}]
}
Let's say time needs to be an integer, so the second entity fails validation.
What's the best thing to do:
Fail both, nothing saves, respond with error message..
Save first entity, respond with error message.
In either case, what would an error message look like? i.e. how/does it specify that it's the second entity that fails validation.
I think you should fail both and respond with an error message because it might be cumbersome again to track the remaining results.
Error message should give the details of failing location. for example if it fails at the second one then specify it in json response.

Resources