hibernate-validator-enum

Hibernate Enum Validator

License

License

Categories

Categories

Hibernate Data ORM
GroupId

GroupId

dev.fuxing
ArtifactId

ArtifactId

hibernate-validator-enum
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

hibernate-validator-enum
Hibernate Enum Validator
Project URL

Project URL

https://github.com/fuxingloh/hibernate-validator-enum
Source Code Management

Source Code Management

https://github.com/fuxingloh/hibernate-validator-enum.git

Download hibernate-validator-enum

How to add to project

<!-- https://jarcasting.com/artifacts/dev.fuxing/hibernate-validator-enum/ -->
<dependency>
    <groupId>dev.fuxing</groupId>
    <artifactId>hibernate-validator-enum</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/dev.fuxing/hibernate-validator-enum/
implementation 'dev.fuxing:hibernate-validator-enum:1.0.0'
// https://jarcasting.com/artifacts/dev.fuxing/hibernate-validator-enum/
implementation ("dev.fuxing:hibernate-validator-enum:1.0.0")
'dev.fuxing:hibernate-validator-enum:jar:1.0.0'
<dependency org="dev.fuxing" name="hibernate-validator-enum" rev="1.0.0">
  <artifact name="hibernate-validator-enum" type="jar" />
</dependency>
@Grapes(
@Grab(group='dev.fuxing', module='hibernate-validator-enum', version='1.0.0')
)
libraryDependencies += "dev.fuxing" % "hibernate-validator-enum" % "1.0.0"
[dev.fuxing/hibernate-validator-enum "1.0.0"]

Dependencies

test (5)

Group / Artifact Type Version
org.hibernate.validator : hibernate-validator jar 6.1.3.Final
org.junit.jupiter : junit-jupiter-api jar 5.6.2
org.junit.jupiter : junit-jupiter-params jar 5.6.2
com.fasterxml.jackson.core : jackson-databind jar 2.10.3
org.glassfish : javax.el jar 3.0.1-b11

Project Modules

There are no modules declared in this project.

Hibernate Validator Enum

So, null & unknown value handling for enum has always been an issue for me when converting from JSON to Java.

I saw an implementation on Amazon Java SDK packages. Really liked the design, I expanded on it to support JSON using the Jackson packages. I have also created @ValidEnum constraints to support using Hibernate Validator library.

This project consist of

  • IntelliJ Live Template
  • ValidEnum annotation
  • JSON, Jackson support
  • Enum null, unknown value handling.

Examples

Creating the enum with live template

Using the live template to create enum, my keyword is: UNKNOWN_TO_SDK_VERSION

public enum Status {
    ALLOWED("ALLOWED"),
    UNKNOWN_TO_SDK_VERSION(null);

    private final String value;

    Status(String value) {
        this.value = value;
    }

    @Override
    @JsonValue
    public String toString() {
        return String.valueOf(value);
    }

    /**
     * Use this in place of valueOf to convert the raw string returned by the service into the enum value.
     *
     * @param value real value
     * @return Status corresponding to the value
     */
    @JsonCreator
    public static Status fromValue(String value) {
        if (value == null) {
            return null;
        }
        return Stream.of(Status.values()).filter(e -> e.toString().equals(value)).findFirst()
                .orElse(UNKNOWN_TO_SDK_VERSION);
    }

    public static Set<Status> knownValues() {
        return Stream.of(values()).filter(v -> v != UNKNOWN_TO_SDK_VERSION).collect(Collectors.toSet());
    }
}

Adding bean validation

public class Bean {
    @ValidEnum
    public Status status;
}

Download

Maven

<dependency>
  <groupId>dev.fuxing</groupId>
  <artifactId>hibernate-validator-enum</artifactId>
  <version>1.0.0</version>
</dependency>

Gradle

compile group: 'dev.fuxing', name: 'hibernate-validator-enum', version: '1.0.0'

Versions

Version
1.0.0