JSON Validation Framework

Finatra integrates with the util-validator ScalaValidator for Scala case class field validation. For more information on the util-validator library, please see the documentation.

MixIn Annotations

With Jackson MixIn Annotations support it is possible to specify field-level constraint annotations for case classes that are not under your control.

For an example on how to use MixIn Annotations with validations, see the documentation here.

Important

Note this integration will only work for field-level constraint annotations since method validations using @MethodValidation annotation must be specified as a method of the case class instance.

Validation Errors

By default, validation errors are returned alphabetically sorted by validation error message (for determinism when testing). See the CaseClassMappingException.

Eg.,

{
  "errors" : [
    "location.lat: [9999.0] is not between -85 and 85",
    "location.long: field is required",
    "message: size [0] is not between 1 and 140",
    "nsfw: 'abc' is not a valid boolean"
  ]
}

Bypassing Validation

You may wish to execute validation for case classes in certain scenarios, but bypass validation in others. For example, you may want to validate a POST request on the write path and store the JSON results somewhere, but bypass validating that same JSON for a GET request on the read path.

In your custom ScalaObjectMapperModule

 import com.twitter.finatra.jackson.modules.ScalaObjectMapperModule

 object NoValidationJacksonModule extends ScalaObjectMapperModule {
   override val validation: Boolean = false
 }

See the Modules Configuration in Servers or the HTTP Server Framework Modules for more information on how to make use of any custom ScalaObjectMapperModule.