0

I have custom simple endpoint that returns some object (record in my case). I would like to validate correctness of returned output data (i.e., output DTO does have all fields set to non null values for example).

Where is the best place to perform such a validation? Is it possible to correct returned values in validator (i.e. changing value null for field "last access of resource" to "Resource was not accessed yet" for example)

Sample illustrative code:

public record SomeDTO(String nameOfUser, String lastAccessedInfo, List<SomeDTO> recursiveIsFun) {
}

@GetMapping(value = "/somethingEndpoint", produces = MediaType.APPLICATION_JSON_VALUE)
public SomeDTO getSomething(HttpServletRequest request) throws IOException, InterruptedException {
    final String baseUrl = ServletUriComponentsBuilder.fromRequestUri(request)
            .replacePath(null)
            .build()
            .toUriString();
    return new SomeDTO("user accessed at " + baseUrl, null, Collections.emptyList());
}
2
  • "Validate the returned output data" sounds like a nonsense. "Validation" is typically related to input. Normally your endpoint calls your internal service and this service is responsible for returning the correct data. If you just whish to ensure that the SomeDTO object is consistent, create it via a builder and modify accordingly its build() method. Commented Apr 29, 2022 at 9:30
  • I have a fairly complex code generating immutable response. It does involve also some external input, which I do not validate. I would like to check for any problems (invalid input data || bug in data processing) and check output of my processing code. I would like to keep business code as simple and readable, as possible and any checks put somewhere aside. Commented Apr 30, 2022 at 10:40

1 Answer 1

1

If it should have default values whenever it's null, then I would prefer to do it in the object itself, like:

@GetMapping(value = "/somethingEndpoint", produces = MediaType.APPLICATION_JSON_VALUE)
public SomeDTO getSomething(HttpServletRequest request) throws IOException, InterruptedException {
    final String baseUrl = ServletUriComponentsBuilder.fromRequestUri(request)
            .replacePath(null)
        .build()
        .toUriString();
    return new SomeDTO("user accessed at " + baseUrl, null, Collections.emptyList())
               .handleNullValues();
}

public record SomeDTO(String nameOfUser, String lastAccessedInfo, List<SomeDTO> recursiveIsFun) {
    public SomeDTO handleNullValues(){
      if(lastAccessedInfo == null){
         lastAccessedInfo = "default value";
      }

      return this;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Seems doable and it does separate business code of generating SomeDTO and code which validates (and here also as bonus, sanitizes) output. I will mark as accepted, if nothing better comes in a few days. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.