r/javahelp • • Apr 22 '23

Should we prefer validation in the constructor over external validation?

This is more of a "discussion"/"asking for opinions" post, but since /r/java is so strict on what they allow, I'd thought I'd better ask here.

In a typical Java web application, we treat validation of incoming objects (for example, deserialized JSON) as an external step. By "external" I mean that the validation logic is a separate step to creating the object. For example, using the Jakarta validation API:

class User {
  private firstName;

  @NotNull @NotBlank
  private lastName;

  // getters/setters omitted
}

We could also make the class immutable and place the validation annotations in the constructor, but the point is that I can create an object while bypassing these validations.

Not a problem in a REST controller (unless you forgot the @Valid annotation and have no tests), but for example in test code you can freely create objects that violate these requirements. This has some problems:

  • the tests are less documenting. A new ISBN("I don't care") doesn't show me what a real-world ISBN number would look like.

  • Adding perfectly valid application code relying on validated properties can make tests fail that use arbitrary data. Let's say I expect an ISBN to have exactly 10 or 13 digits, and add code that does a substring() on that without a range check. If I create an object that violates this property in my test, the new code may fail.

And if you were as paranoid as the Secure Coding Guidelines for Java, each public method in each layer in your application would have to perform the validation again.

With Java putting more and more emphasis on integrity and strong encapsulation, and examples about adding validation logic to constructors like in the record JEP, I was wondering whether our approach to validation is flawed. Not having a blank "lastName" is an invariant of User, something that we should be able to rely on in every place where an instance of User is used.

So why not put validation into the constructor? For example, using a validation API like in this made-up example by ChatGPT:

public record Person(String name, int age) {

    public static final Validation<String> NAME_VALIDATION = Validation.of(new NotEmpty("Name cannot be empty."));
    public static final Validation<Integer> AGE_VALIDATION = Validation.of(new Min(0, "Age cannot be negative."));

    public Person {
       NAME_VALIDATION.validate(name).throwIfInvalid(IllegalArgumentException::new);
       AGE_VALIDATION.validate(age).throwIfInvalid(IllegalArgumentException::new);
    }
}

This prevents me from ever creating a new Person("John", -42). (The example API ignores concerns like localizing error messages, capturing the name of the field, etc. – it's just an example.)

So, what do you think?

Drawbacks I can see with this approach:

  • when adding a new constraint, old, unmigrated data in the database would not be mappable to the object. This can be solved by having different classes at API, core, and persistence layer, but you'd have to duplicate validations (or don't do them in all places).

  • you can only use self-contained validations in the constructor. If I want to check for example if a country code is in a list of codes I retrieved from a code dictionary service, I would either have to do this externally, or pass the list of valid codes to the object on construction, for example via a static factory method, which doesn't sound like clean code.

  • Theoretical performance impact by repeating validation when creating a copy, but I trust the JIT compiler in the JVM to inline as much as possible and optimize using predictive branching.

16 Upvotes

14 comments sorted by

•

u/AutoModerator Apr 22 '23

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

4

u/xRageNugget Apr 22 '23

Without thinking to much: How about making the constructor private or effectively private, and use the builder pattern to set properties, which then do the validation.

Although I am not sure if you can directly unmarshal json into such an object, or you'd need a intermediate object to catch all data, an then create your validated object from that. Sounds fishy :/

3

u/pronuntiator Apr 22 '23

Yes, validation in a static factory or builder also came to my mind. Unfortunately you would have to ditch records in this case, because a record's constructor cannot be made less visible than the record itself. This is to honor the "transparent" part of "transparent carriers of immutable data".

At least for Jackson, you can deserialize using builders: https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/annotation/JsonPOJOBuilder.html

4

u/temporarybunnehs Apr 22 '23

It's an interesting question. Here is some stuff off the top of my head:

  • My initial thought is, the reason we don't see more of this is due to separation of concerns. You are essentially putting business logic into the object itself. Like you said earlier, there are limits to the validations you can run, so now you have your validations in two places. Also, what happens if you extend the object? This can muddy up the code a bit.

  • The Java Record object, in my opinion, is more a specialized use case, rather than a pattern to be followed in all classes. So in that sense, if you define a special object that has a lot of internal constructor validations, that's probably fine, but it doesn't mean you should necessarily apply it to every single one of your POJOs.

  • Another thought is, the pojo creation time may be at any time in the flow of things. Maybe you want to validate up front whether a json request is good before running a bunch of other code and then finally finding out that the info is bad when you call new Object(); Seems to be a bit simpler in that sense to know up front.

  • The fourth thought is probably just because a lot of people don't do it. You get a new job, go into the code base and start adding these patterns that no one is familiar with and that can disrupt things more than it helps.

My conclusion is that it could be helpful in some cases, but I would say, not a general rule to apply to all your POJOs.

1

u/pronuntiator Apr 22 '23

Thank you for your thoughts.

> Like you said earlier, there are limits to the validations you can run, so now you have your validations in two places.

Yeah, that's my major concern. Chatting a bit with GPT in the meantime it also pointed me to the problem of validation groups. I've used them in a project before and that would also not be possible here.

About separation of concerns, isn't it an object's concern to be valid for the thing it represents? I cannot assign an int to a String, and I cannot create a `LocalDate.of(2023, 04, 35)`. Or do you think because businesss requirements change more frequently than the Gregorian calendar, it should not be part of it?

2

u/temporarybunnehs Apr 22 '23 edited Apr 24 '23

Thats a good point. In that case, I guess it depends where you draw the line of concerns. Is your object's concern about validity (per whatever your use case defines as valid), or is it just there to define the model and relationships between entities. I can see a case for both. EDIT: I just remembered the term for this. Single Responsibility.

I think the trickiness comes in when defining what should be the Object's responsibility vs some ObjectValidator. Stuff like, you can't put a string into an int or April 35th is not a real date makes sense that the Object would catch that during creation as these things are sort of inherent to the definition of those parameters. Other things like, your first name can't have more than 20 chars gets a little more blurry. I think the frequency of change does influence where you put things too. I would prefer if the domain model shouldn't change frequently as a lot of stuff usually relies on it and then you can end up with unintended cascading effects.

Another interesting thought I had just now was, for some APIs i've implemented, it has been important to return to the caller what exactly was wrong with their request. Using an external validator, this was trivial in the sense that I just parsed the fields and added to a list whenever something triggered a rule. If all my rules were in an Object's constructor (or even spread across multiple ones), it would make that task much more complicated.

So in the end, it just depends on your use case.

1

u/dr-maniac Apr 22 '23

Records can't be extended, and the purpose is also that you create always complete object, you must set all values (of curse you can set it to null but then you have set it), so immutable object ist often a better way to avoid issues because you have cerated an object and forget to set some fields.

3

u/severoon pro barista Apr 23 '23 edited Apr 23 '23

I think there's no answer to your question as posed, because the concept of validity is more complicated than that.

There are two kinds of validity rules to consider. A validity rule can be intrinsic or extrinsic to the object. The kind of rule that rejects a negative age for a Person object is intrinsic because there is no context in which that object can exist where that age would be considered valid. (I mean, you could contrive an application where it would be extrinsic like a birth date prediction market, where the people you're modeling are future people, but for most systems this would be an intrinsic rule.)

But consider you have two "valid" Person objects with positive ages, and some part of the system in some far-flung corner creates and stores a mother-child relationship between these two objects. If the age recorded in the Person object that represents the mother is less than that of her child, it might be that the relationship is invalid … but, it could also be that the relationship is valid, it's one or both of the ages that are invalid.

Here we have an example of a validation rule that is extrinsic to the objects because it is conferred upon them by the context of the outside system. There is no way to look at either object, or even both, and know that anything is invalid without also looking at that other bit of data about the relationship.

So what? What's the point?

The point is that for a system to provide any kind of rigor when it comes to validation, it's not enough to design objects that have strong encapsulation, we also have to be very clear about what exactly they are encapsulating.

Consider two classes, Person1 and Person2. They are both immutable, present the same API, and they enforce the same validation about what a proper name is, no negative age, etc. Most software engineers would look no further and say they are identical types. If the system puts no additional restrictions around Person1 other than the validation rules it itself enforces, but only allows Person2 objects to be created at the same time as all other family members and cross-checks the ages, these are actually two different types! They each represent different things in the system. Person1 captures certain properties of a person, while Person2 captures those properties in the context of that person's family members.

Even though there are no artifacts of that difference evident in a Person2 object, it nonetheless encapsulates different information than Person1 when it is created even though all the bits are the same. The way they are created changes what they mean in the system…the set of all Person1 instances that can exist is a superset of all Person2 instances that can exist because some Person2 objects place constraints that prevent the creation of others, while Person1 objects are independent of one another. Despite looking identical under a microscope, they are different types because they model different things.

Now in a real system you wouldn't just allow Person2 objects to roam around independently, you'd probably represent them as members of a Family object that strongly encapsulates them. In that case, you could properly say that the validation rules that enforce ages across family members may be extrinsic to the Person object, but they are intrinsic to the Family object. That's right! Ideally, you would design a system where all such rules are intrinsic to something that has a representation, and then you get the rigor you're after.

In a real system, though, you'd have to design a system that gives a representation to everything required to give every validation rule a home. Theoretically that may be possible, but it's certainly not something you could do in practice for any system of any real complexity, and we're not even taking into account yet the fact that real systems have requirements added on. Even if you rebuild an entire system from the ground up every time each new requirement came in the door, it would not be reasonable to expect to get this right. In the end, you're going to have objects like Person1 and Person2 with no Family types.

All is not lost, though. I think you can still bring rigor to such a system, even without a perfect home for every validation. The trick is just to recognize what your types actually represent. Your system can work with objects of type Person1, if it understands and expects no more of this type than what it actually provides. That means accepting that it is only valid in certain ways and not in others, and where those other validity rules matter, the design of the system must apply them.

So, to directly answer your question, in short: Some validity rules should be applied in the constructor, some in a builder pattern, some in the context of an encapsulating object's builder or constructor, and some in yet some other ways. It all depends on what the different objects actually model.

This is, by the way, the same kind of thinking that applies when designing a data model for a database. A good schema design should only model intrinsic relationships between data. A common mistake is to try to directly model relationships between data that only exist in the context of certain use cases that are implemented in business logic higher up the stack. The problem with this is that when those use cases change, as they will do as the product evolves, you now have a data model that represents relationships that are no longer relevant to the system.

Since data in a DB is durable, it pays to only capture the intrinsic relationships because those are invariant in the case of changing business requirements. It's not a big deal to change code, but change schema requires doing a data migration, which requires migrating all dependencies on that data from the old schema to the new. This can be tantamount to just building a new stack altogether from scratch.

1

u/pronuntiator Apr 23 '23

Thank you for this well-written reply! The family relation is a great example. You're right, you could encapsulate this in an object, but you wouldn't want to pass around the Family object everywhere you go, plus now it is no longer just a data carrier, as you would need to have behavior to add and remove members. Kinda like what object relational mappers like Hibernate try to solve, with mixed success (as you can still manipulate relationships in memory that are not reflected in every entity by accident).

A common mistake is to try to directly model relationships between data that only exist in the context of certain use cases that are implemented in business logic higher up the stack.

Do you have an example for that (in the context of relational DBs)? When I have such a relationship, it is normally wrapped in some natural representation (a receipt, an order, ...) with explicit foreign key references. Indeed, changing the schema requires data migration, but it ensures that your data always adheres to the rules. For zero-downtime deployment I typically introduce the relationship as optional and migrate over time, so there is a phase in which the code has to work with both "worlds".

Do you say these relationships should be "soft"?

2

u/severoon pro barista Apr 23 '23 edited Apr 23 '23

you could encapsulate this in an object, but you wouldn't want to pass around the Family object everywhere you go

You might, depending on the system you're designing.

The more significant point here is that, even in the case where you would want to do that, there are plenty of examples in real software where in v1 of the system, the Family type wasn't created and there are a million places where Person objects are being passed through various APIs and it's impossible to change.

It's not reasonable to always say, "This new requirement that just showed up requires that we replace Person with Family everywhere," even if that is the "theoretically right thing" (whatever that means).

The evolution of software is something that I wish they'd spend more time on when teaching software engineering. In school, the emphasis is often on learning mechanics. They want to teach how Java generics works so you do a project, crystallize this perfect answer to a problem limited in scope, and hand it in. This is great for learning generics, but does nothing to teach software design.

In real software, the stuff we build has to be careful to build hard dependencies only upon the invariants wrt the dependency. That means if you are designing thing A and it's near the top of the stack where not a lot depends on it, this isn't so important because it's easy to change those things. If you're building thing A and it has tons of dependencies on it, you want to make sure that thing A is presenting an API that truly models parts of the system that will not change as long as thing A is around, regardless of what new requirements come in. This means that thing A has to have a really clear idea of what the intrinsic bits of it are, and not try to model extrinsic bits.

It also means that designing thing A to be be all things to all clients current and future is almost always the wrong approach. A much better approach is to design thing A to be very limited in scope such that when an expected invariant does change, everyone is compelled to throw away thing A entirely and replace it with a new thing that takes the same approach. Young software engineers don't want to do this, they want to design APIs at every level, from the individual class on up, that address a large problem domain. The best answer to that that the industry has come up with is an obsession with testing. The thinking is that if we at least force the engineer to actually write real code that exercises that entire problem domain, there won't be enough time in the day for every component to solve every problem b/c the crushing weight of writing all those tests rules out that path. (In actuality, what ends up happening is those APIs still get designed, they just go mostly untested. Oops!)

Do you have an example for that (in the context of relational DBs)?

Sure, let's say that you are designing a database for SpaceX and the engineers come to you and say, "We store the mass of all the different parts of the rocket so that we can build different assemblies in the simulator, and then we add them all up and calculate the weight so we can simulate takeoff from the launchpad. We have all these complicated database queries that we're doing everything we can to simplify, so we want you to design the schema to store the weight directly instead of mass. It's a small thing, but all these optimizations in the design add up to much simpler, easier-to-debug queries."

Okay, you think, no problem. It's literally the same type, it's just a different label on that column, so you change the name of the column from mass to weight, update the doc saying what this column will store, and move on with life. The entire system gets built on this with all these dependencies on this column, the queries are simpler, everything is good.

The problem: The mass of the assembled rocket isn't stored anywhere. Mass is an intrinsic property of the thing being recorded—it doesn't matter where in the universe the rocket is, it has the same mass. The weight of these bits, however, are only that when at rest on some reference launchpad. (The geoid might be different for different launchpads and the weight of the rocket sitting on it slightly different.) The lunar module that Neil and Buzz used to get off the surface of the moon had a different weight when launching from the moon surface than it did launching from the Earth surface.

This is a contrived example, of course, and I could've chosen something else that talks about relationships using foreign keys, but this is an even more instructive example because there are no foreign keys, there are no visible artifacts of this dependency in the schema itself, all you did was relabel a column and store a different number in it. But in doing so, now all clients that query that number have a dependency on a particular use case: the rocket taking off from some reference launchpad. You've built this dependency on this particular use case right into the schema, and dependencies transit.

Because of DRY, there will be artifacts in the system somewhere (because you don't want every client to reimplement the same logic over and over, that's a haven for bugs). Every client that queries these numbers now has to send them off to some library that back-calculates the mass, the number the other use cases actually needed from the DB. And that library has a dependency on the launchpad use case, and dependencies are transitive, so now all use cases depend on that first one. And, of course, that first one has more requirements coming in all the time, so who knows how many of those changes will result in ripples down the dependency chain?

Again, this is not an actual example of something any SpaceX rocket scientist would do, yet if I think back to a lot of the systems I've worked on in my career, they are comprised mostly of this type of code…these modules that tie together things which in principle should be independent.

The reason all of this happens is that if you compare software engineering to, say, civil engineering, a civil engineer can't just design a bridge however they want. There are limits in place, physics is a harsh mistress that makes no exceptions. You have to look at a beam or a bolt and know what the invariants are when evaluating whether it will hold. In software, though, how do you know what the "intrinsic properties" of an ObjectRelationalBindingManager are? All the things are made up, and there is no "physics" of the system…you can do whatever you want. You can even make these decisions consciously and get all of this right, but how do you communicate it to everyone else that's working on the system so they don't confuse Person1 with Person2? Another developer is likely to get angry with you if you start telling them they can't use Person2 that way, they need to create their own new type that looks exactly the same. Doesn't that violate DRY!, they'll say. (Unlike the reference launchpad library, this is an example that actually would not violate DRY because they are different types.)

This is why I often feel like calling what we do "software engineering" is being awfully generous; if other engineering disciplines worked by the same rules and produced the work products of the same average quality as most software, none of us would ever drive over a bridge again. Most of what I see out in the world is software development, not engineering. (And I've worked on medical systems, life and death stuff.) This lack of rigor is so ingrained in software that it's not even possible to raise these issues and be taken seriously, because most others in the organization don't have the tools to evaluate the claims and separate them from simple politics and other motivations for wanting things to be a certain way. But consider what happens if the girder engineer sees the cable engineer doing something bad and raises a concern…I mean, I obviously don't know anything about building bridges, but I do know that such things are evaluated in a problem-focused (as opposed to politics-focused) way to see if there's an actual problem there. That's actual engineering.

1

u/TakAnnix Apr 22 '23

I think what you're referring to is the "parse, don't validate" approach. I think it works great with Records and leads to more maintainable code.

This approach has been picked up by various validation libraries like Zod (for Typescript).

1

u/TehBrian not very good at java Apr 22 '23

I don’t have any advice because I’ve never run into this decision, but I just wanted to say that this is a really well-written question, and I appreciate how much thought you’ve put into the problem and how nicely you’ve laid it out for the people wanting to help.

1

u/[deleted] Apr 23 '23

[removed] — view removed comment

2

u/pronuntiator Apr 23 '23

I knew I should have picked a different example :)

Yeah, I've worked in the public sector before, and in addition to cultural differences, we also processed data of people with no given name at all, no known birthdate, etc.

It's like with valid email addresses, most of the time it's best not to make too many assumptions.