Is there a Reactive way to Validate Form Fields in Spring WebFlux? - spring-mvc

I have a Spring Boot/Webflux project and would like to set a validation error if a username already exists, with a friendly message. However, if I make a non-blocking call to the database in the validate(Object target, Errors errors) method then it's not clear what to do with the returned Mono. The only workaround I found was to use the non-reactive database driver to check for existence, which at least doesn't require calling block() directly. Any other suggestions?

Related

If I am following the Clean Architecture, in which layer should I put the logic for checking if a foreign key exists?

I am building a web application in .NET Core following the Clean Architecture. I have an use case class for creating an user. Each user has a role which is passed in as an id. The use case will call to the User Repository for creating the user.
Currently, I am using FluentValidation for validating the request object. It is just pure checking such as length, not empty, etc..
My problem is that I don't know where to put the validation logic for validating the existence of the provided role. The validator is currently in the Application layer. I am going with putting the validation logic in the User repository but that doesn't sound right to me.
Do you have any ideas?
Thank you in advance.
You should add it in the validator using Fluent Validation. There are options to make custom validator and to make them async with Fluent Validation.
Creating a method in the repository for checking for the role is not necessarily wrong. But it should defently be called in validation layer along with other checks like not empty etc. etc.
For performance sake make sure that you call the check once all the other validations have passed.

Where should Meteor.methods() be defined?

http://docs.meteor.com/#meteor_methods
I have tried it in publish.js in my server folder.
I am successfully calling Meteor.apply and attempting the server call from the client. I always get an undefined response.
Calling Meteor.methods on the server is correct. That will define remote methods that run in the privileged environment and return results to the client. To return a normal result, just call return from your method function with some JSON value. To signal an error, throw a Meteor.Error.
On the client, Meteor.apply always returns undefined, because the method call is asynchronous. If you want the return value of the method, the last argument to apply should be a callback, which will be passed two arguments: error and result, in the typical async callback style.
Is your server code actually getting called? You can check that by updating the DB in the method and seeing if the client's cache gets the new data, or calling console.log from inside the method body and looking at the output of the "meteor" process in your terminal.
There are several places I can define my Meteor.methods() (with pro's and con's):
On the server only - when the client calls the method, it'll have to wait for the server to respond before anything changes on the client-side
On the server, and uses a stub on the client - when the client calls the method, it will execute the stub method on the client-side, which can quickly return a (predicted) response. When the server comes back with the 'actual' response, it will replace the response generated by the stub and update other elements according.
The same method on both client and server - commonly used for methods dealing with collections, where the method is actually a stub on the client-side, but this stub is the same as the server-side function, and uses the client's cached collections instead of the server's. So it will still appear to update instantly, like the stub, but I guess it's a bit more accurate in its guessing.
I've uploaded a short example here, should you need a working example of this: https://gist.github.com/2387816
I hope some will find use of this addition, and this doesn't cloud the issue that methods are primarily intended to run on the server as debergalis has explained.
Using Meteor.methods() on the client is useful too. (look for "stub" in the Meteor.call() section too...)
This allows the client to (synchronously) simulate the expected effect of the server call.
As mentioned in the docs:
You use methods all the time, because the database mutators (insert,
update, remove) are implemented as methods. (...)
A separate section explaining use of stubs on the client might ease the understanding of methods calls on the server.

Issue with BizTalk orchestration, generate no error but doesn't do anything

I have BizTalk orchestration where it receives file and send file. I have expression shape calling .net class (method) to read xml file and upload file to SharePoint. My expression shape has the following:
XmlReader.readXml();
It's not passing any parameter nor return any value.
My Xml file gets send to send location and generate no error on window logs. However, it's not uploading file to sharepoint. If I run XmlRead.readXml() by itself, I don't have any problem uploading to sharepoint. How do I know if my .net is calling the method and executing?
OK, first thing. If there are no errors in the applications event log and BizTalk does not suspend throwing an exception then you know that your orchestration is working.
The fact that you are not getting the required behavior from it is not down to any biztalk weirdness.
Can you post the code from your expression shape and then it may be easier to find out what your problem is?
I am a little confused by your post because you say you are calling a .net class from an expression shape, but then you have a call to XmlReader in your expression shape. Is XmlReader your own class? If so then you do not seem to be passing any data into your readXml(). This may be why your call does nothing. If you need to pass a biztalk message into a method call from an orchestration you need to use the XLANGMessage type which can be found in the MicroSoft.XLANGs.BaseTypes assembly.
Hope this helps.

How to return errors to view from controller not tied to a specific model property

Curious what the best practice is for returning errors to a view from a controller where the error isn't really a validation error, but more like "user not found" or "service timeout" type of errors. (in ASP.NET MVC2 framework)
I've been adding them to the ModelState's model errors, but that doesn't seem appropriate. (although easy to implement and maintain)
Example - A user tries to log in, and their credentials do not match a known user.
I believe you need to be slightly more clear with regards to what youre calling an error.
Exceptions (timed out, database error, etc.) should be handled using specific exception handlers (not least because you often don't want to show the error message to the end user).
In this scenario, look at overloading the OnException() method on the controller. (or having all your controllers inherit from a common ControllerBase which has that method overriden to avoid repetition)
You may also want to check the value of filterContext.HttpContext.IsCustomErrorEnabled when handling exceptions to determine whether to expose detailed exception information or to obfuscate it.
Remember that exceptions should be for exceptional circumstances - "normal" operation should never result in an exception
The other type of error you're mentioning is more like "Unable to process request due to business rules / invalid input" (or similar) in which case, adding error messages to the ViewModel seems appropriate. They may not be direct validation errors but they are most likely as a result of the user input
I've just researched this, and I think that for network-related errors and so on, which are bound to happen, it's OK to use TempData (but don't put the Exception inside but the exception message only, for security reasons).
http://forums.asp.net/p/1460169/3353779.aspx

Extend the exception thrown from ASP.NET when calling a Webservice from JQuery

I'm using JQuery to load controls dynamically in an ASP.NET development environment using JSON and WebServices. Within this solution I have a business logic layer which has a built in validation mechanism (i.e. validating properties and business rules similar to that of CSLA)
When requesting a new control to be loaded dynamically using JQuery and an ASP.NET WebService, I would like to validate the input from the current control against the business logic validation mechanism (i.e. server side validation) and notify the user if there was any problems.
I managed to achieve this, however, when validation fails in the web service I would like to throw a customer exception containing the validation field id's and associated error messages.
In JQuery, I test for this specific ExceptionType and would like to apply the error messages dynamically to the controls listed in the exception type properties. This is where my problem comes in. Even though I created a custom exception with custom properties the exception that is passed to JQuery in JSON format from the WebService is still a standard exception with none of the additional properties listed. I could simply create a JSON formatted string of values in the exception's message property but would ultimately prefer something a little more elegant. Does anyone know how you can override the serialized exception created by ASP.NET for situations such as this...
Thank you in advance...
G
I ran into something very similar a couple days ago - basically there's no way to make ASP.NET generate custom exceptions. This is by design, since returning a specific type of exceptions would
[...] expose implementation
details/bugs to the clients. We could
do something with special exception
type that we let pass through, but its
too late for this release [...]
You could always return different HTTP status codes, and have the browser handle them as custom exceptions - for example, a 500 error would mean one thing, a 401 something else, etc. I think the best solution is to make your method return a string with the exception stack - not elegant, but at least this way the client has all the exception details.
Dave Ward also has info on ASP.NET AJAX service errors.

Resources