DEV Community

Leonora Der
Leonora Der

Posted on

molecules

So recently I've started a project. It is a library of useful Java classes with built-in validations.

Sorry, what?

Think about a person. Now try to make a Person class. I guess you would craft something like this:

public class Person {
    private String firstName;
    private String lastName;
    private Date birthDate;
    private String email;    
}
Enter fullscreen mode Exit fullscreen mode

Can you see that we have these properties: name and email address, but we have to map them to types like String, Date, etc. And do not forget about validating this data in the service or controller layer...

So instead of these, why can't we have Name or Email types? And these types should know what is a valid email address or what is the maximum length of a name.

By using molecules, your Person class could look something like this:

public class Person {
    private Name firstName;
    private Name lastName;
    private BirthDate birthDate;
    private Email email;
}
Enter fullscreen mode Exit fullscreen mode

I see. What now?

Do you like the idea? Check out all the molecules or help! :)

At the moment I am at the early stages, so any help and/or advice is appreciated! :)

Check it here: https://github.com/floppylab/molecules

Top comments (1)

Collapse
 
belenot profile image
belenot • Edited

You can use json. If it not so straightforward in some cases, what about URI or URL? You can define your own prefixes with corresponding processors so that you won't have to create a huge pile of types. Instead of creating types you create processors. It will has an advantage, when user just need to define instance, not process it, and he does not be constraint with dependencies.