Validation enable for some fields only on yml configuration based in spring - spring

I have one requirement on validation of fields in Spring.
Basically we want to validate some fields only if configuration in application.yml file is enabled.
Ex:
#NotBlank private String name;
In application.yml, we will configure one boolean, if true then validate else dont run this validation. But important point here is, we want this feature for only some fields.
Other fields validation should happen irrespective of configuration.

Related

Automatically Ignore #OneToMany, #ManyToOne

I'm successfully logging entities with Javers, but for every entity I have to put #DiffIgnore and #ShallowReference to ignore fields mapped with #OneToMany, #ManyToOne, etc.
I'm trying to configure on JaversBuilder, but without success.
Is there a way to configure on Javers to automatically ignore properties mapped with these annotation? Or only take properties with #Column annotation?
I read these, and tried to follow some of the answers, but it didn't worked either.
Javers - DiffIgnore on bidirectional OneToMany
Javers default ignore instead of default include
There is no way to order JaVers to ignore a field when it has a specific annotation. JaVers doesn't read all annotations in your classes.
You can implement a annotation scanner on your side and then register your EntityDefinitions in JaversBuilder:
public JaversBuilder registerEntity(EntityDefinition entityDefinition)

is it possible to have conditional #Transient field?

Let's say if I have an Entity named person with lots of information including SSN. When other user query this person, I want to show a 'lite' version of person Entity. I could've done so by annotating SSN with #Transient, but that means the person himself would not get this field too. Is it possible to reuse the same Entity but return two different json to client? I'm using spring boot.
First of all #Transient just means that the value, the SSN in your case, won't be persisted to the database.
As for your problem annotations are static and cannot be applied dynamically.
You have 2 Options:
Define a new View class for your user.
Look at JacksonJsonViews

Can hibernate validator be used to validate a field depending on a field value from another class

I have a scenario where I need to validate some fields of a class depending on the value of a field from another class.
For ex: Street, City, Postal Code of Address class should not be null if the Employee has registered for a Group Retirement Plan.
Can this scenario be handled using Custom Validator of Hibernate Validator? Is this a good candidate for Hibernate Validator ? If so, could anyone provide me with some examples.
I have already gone through cross field validation examples like:
Cross field validation with Hibernate Validator (JSR 303)
If I have a boolean indicator to indicate whether a user is registered for group retirement plan in Address domain class, I know I can come up with Custom Validator to verify the fields. But, I want to avoid adding indicator to Address class. As well, Address is not a field in Employee class to do nested validation.
The Frameworks I am using are Spring MVC and Mybatis for Data access. The validation is happening at Spring Rest API call with #Valid annotation.
I guess a custom class level constraint might work, but it depends on your actual domain classes. It is currently not clear how for example Employee, Group Retirement Plan and these other entities are connected.
Whether Hibernate Validator is a good fit is whether you have other validations as well or which other frameworks are involved. For example, if you want validation to take place at JPA live cycle events, then Bean Validation is a good fit, since it integrates per specification with JPA. If you really only have this one validation, writing some custom code might be all you need.

How to change which field is used as _id without annotations in Spring data MongoDB

I have some classes which are being generated from a WSDL using the CXF wsdl2java tool.
I would like to store instances of these classes in a MongoDB database, using Spring data MongoDB.
The default mapping is acceptable for this, except for one thing:
I would like to change which field is used as _id.
Normally this is done with a annotation like #Id.
But because these classes are generated, I would like to do this without an annotation.
Is there a (correct?) way to do this?
So my generated class is:
class Simple {
String businessId;
String otherfield1;
.
String otherfield999;
}
And I would like Spring data MongoDB to use 'businessId' as the '_id' field in MongoDB, without changing the 'Simple' class by adding an annotation.
Thanks!
That's currently not supported. The property either needs to be id, _id or annotated with #Id.

JSR 303 - Validate a field if it is not null

I have a form which is filled in, and some of the fields are options. I want to apply validations on the information that was filled in but the optional fields should be validated only if there was something filled in, so if theses are not null. Did any of you do something similar?
Or do you have any suggestion?
Bean Validation constraints usually accept null as valid value (with the exception of #NotNull of course). Depending on your UI framework you might retrieve empty strings instead of null for fields without user input. If you're are working with JSF 2, you can set the context parameter javax.faces.VALIDATE_EMPTY_FIELDS to false to avoid a validation of empty fields.

Resources