Home > Software engineering >  Good Practice for validation json in Quarkus
Good Practice for validation json in Quarkus

Time:06-30

I am working with Quarkus and doing a REST Services, in my organization it has a work scheme that I would like to improve, currently we have the following format for Request:

{
    "FindPukCodeBS": {
        "Header": {
            "country": "VE",
            "lang": "ES",
            "entity": "TMVE",
            "system": 97,
            "subsystem": "APP",
            "originator": "VE:TMVE:97:APP",
            "userId": "04142985219",
            "operation": "FindPukCode",
            "destination": "VE:TMVE:93:RSB",
            "timestamp": "2021-01-20T10:23:23.233-04:00",
            "msgType": "REQUEST"
        },
        "Body": {
            "findPukCodeBSRequest": {
                "iccid": "895804120018888888"
            }
        }
    }
}

Currently to validate this Request I use a class of the following type:

package com.tmve.subscriber.until;


import com.tmve.subscriber.domain.request.HeaderRequest;
import com.tmve.subscriber.domain.request.FindPukCodeBSInput;

public class Validator {

    public static String validateHeader(HeaderRequest headerRequest) {
        String valid = "false";
        if (headerRequest != null && headerRequest.getCountry() != null && headerRequest.getCountry().length() > 0
                && headerRequest.getLang() != null && headerRequest.getLang().length() > 0
                && headerRequest.getEntity() != null && headerRequest.getEntity().length() > 0
                && headerRequest.getSystem() != 0 && headerRequest.getTimestamp() != null
                && headerRequest.getSubsystem() != null && headerRequest.getSubsystem().length() > 0
                && headerRequest.getOriginator() != null && headerRequest.getOriginator().length() > 0
                && headerRequest.getUserId() != null && headerRequest.getUserId().length() > 0
                && headerRequest.getMsgType() != null && headerRequest.getMsgType().length() > 0
                && headerRequest.getOperation() != null && headerRequest.getOperation().length() > 0
                && headerRequest.getDestination() != null && headerRequest.getDestination().length() > 0
                && headerRequest.getMsgType() != null && headerRequest.getMsgType().length() > 0) {
            valid = "true";
        }
        return valid;
    }

    public static String validateRequest(FindPukCodeBSInput request) {
        String valid = "false";
        try {
            if (request != null && request.getFindPukCode() != null
                    && request.getFindPukCode().getBody() != null
                    && request.getFindPukCode().getBody().getFindPukCodeBSRequest() != null
                    && request.getFindPukCode().getBody().getFindPukCodeBSRequest().getIccid() != null
                    && request.getFindPukCode().getBody().getFindPukCodeBSRequest().getIccid()
                    .length() > 0
                    && request.getFindPukCode().getBody().getFindPukCodeBSRequest().getIccid() != ""
                    ) {
                valid = "true";
            }else{
                if( request == null
                        || request.getFindPukCode() == null
                        || request.getFindPukCode().getBody() == null
                        || request.getFindPukCode().getBody().getFindPukCodeBSRequest() == null){
                        return "iccid";
                }else if(request.getFindPukCode().getBody().getFindPukCodeBSRequest().getIccid() == ""
                        || request.getFindPukCode().getBody().getFindPukCodeBSRequest().getIccid() == null
                        || request.getFindPukCode().getBody().getFindPukCodeBSRequest().getIccid().length() == 0){
                        return "iccid";
                }
            }
        } catch (Exception e) {
            return valid;
        }
        return valid;
    }
}

And I have the following classes that make up the Request:

FindPukCodeBSInput

package com.tmve.subscriber.domain.request;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class FindPukCodeBSInput {
    @JsonProperty("FindPukCodeBS")
    FindPukCode findPukCode;
}

FindPukCode

package com.tmve.subscriber.domain.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

@Setter
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class FindPukCode {
    @JsonProperty("Header")
    HeaderRequest header;
    @JsonProperty("Body")
    BodyRequest body;
}

Header Request

package com.tmve.subscriber.domain.request;
import lombok.*;

@Setter
@Getter
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HeaderRequest {
    private String country;
    private String lang;
    private String entity;
    private int system;
    private String subsystem;
    private String originator;
    private String userId;
    private String operation;
    private String destination;
    private String timestamp;
    private String msgType;
}

BodyRequest

package com.tmve.subscriber.domain.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Data
public class BodyRequest {
    @JsonProperty("findPukCodeBSRequest")
    FindPukCodeBSRequest findPukCodeBSRequest;
}

FindPukCodeBSRequest

package com.tmve.subscriber.domain.request;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Data
public class FindPukCodeBSRequest {
    private String iccid;
}

Is there a good practice that can improve this code?

The validation basically is to validate that the field is not null, that the size of the field is greater than 0 and in some cases that the field does not exceed a certain number of characters

CodePudding user response:

Check bean validation. Then you can annotate the REST parameters with @Valid and the framework will check that all validations (also expressed with annotations) hold. More advanced scenarios are also supported. The dependency you need is quarkus-hibernate-validator.

  • Related