Basic field validation using RESTful API Modeling Language (RAML)

  • May 18, 2020

We can provide basic validation in Mule using RESTful API Modeling Language (RAML). For example, we can specify whether a field is compulsory, whether a field accepts null value and the minimum length of the field. For example, if we have a mobile number as a field, it must have a minimum length and a maximum length of 10 digits.

Implementation of basic field validation using RAML:

Basic Field Validation using RAML1

Basic Field Validation using RAMLFrom above RAML, we can understand that we have only one resource i.e. customer with POST request that will accept a json object person, which has two properties namely name and mobile. A name property will accept a string with minimum length 5. This field is a compulsory field. This field won’t accept null value. A mobile property will accept a string with minimum length 10. This field is a compulsory field. This field won’t accept null value.

If we don’t provide the person object with the above conditions, the error will be generated by APIKIT router as APIKIT:BAD_REQUEST.

Basic Field Validation using RAML2

The transform message component in the on error propagate scope of type APIKIT:BAD_REQUEST is configured as:

Basic Field Validation using RAML3

The api-kit-module dependency should be present in pom.xml file:

Basic Field Validation using RAML4

If we don’t provide any input to the customer endpoint, the person key not found error will be given as output.

Basic Field Validation using RAML5

If we provide a person as a blank object, it will give a name and mobile key not found error.

Basic Field Validation using RAML6
Basic Field Validation using RAML7

If we provide only a name with five characters and don’t provide mobile as a parameter, it will give a mobile key expected with length 10 error.

Basic Field Validation using RAML8
Basic Field Validation using RAML9

Input will be accepted only if the name is with five characters and mobile with 10 characters.

Basic Field Validation using RAML10

— By Manish Prabhu