Search in sources :

Example 46 with ValidationResult

use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.

the class ActionArgumentBuilder method extractRequestData.

@Override
public RestLiRequestData extractRequestData(RoutingResult routingResult, DataMap data) {
    ResourceMethodDescriptor resourceMethodDescriptor = routingResult.getResourceMethod();
    if (data == null) {
        data = new DataMap();
    }
    DynamicRecordTemplate template = new DynamicRecordTemplate(data, resourceMethodDescriptor.getRequestDataSchema());
    ValidationResult result = ValidateDataAgainstSchema.validate(data, template.schema(), new ValidationOptions(RequiredMode.IGNORE, CoercionMode.NORMAL));
    if (!result.isValid()) {
        throw new RoutingException("Parameters of method '" + resourceMethodDescriptor.getActionName() + "' failed validation with error '" + result.getMessages() + "'", HttpStatus.S_400_BAD_REQUEST.getCode());
    }
    return new RestLiRequestDataImpl.Builder().entity(template).build();
}
Also used : DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) RoutingException(com.linkedin.restli.server.RoutingException) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) DataMap(com.linkedin.data.DataMap)

Aggregations

ValidationResult (com.linkedin.data.schema.validation.ValidationResult)46 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)20 DataMap (com.linkedin.data.DataMap)18 Test (org.testng.annotations.Test)18 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)12 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 DataSchema (com.linkedin.data.schema.DataSchema)8 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)8 RootBuilderWrapper (com.linkedin.restli.test.util.RootBuilderWrapper)8 Map (java.util.Map)8 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)7 Message (com.linkedin.data.message.Message)5 RecordTemplate (com.linkedin.data.template.RecordTemplate)4 ResourceMethod (com.linkedin.restli.common.ResourceMethod)4 List (java.util.List)4 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)3 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)3 DataElement (com.linkedin.data.element.DataElement)3 SimpleDataElement (com.linkedin.data.element.SimpleDataElement)3