Basic Field Validation using RAML

Author: Manish Prabhu

We can provide basic validation in Mule using RAML. For example, we can provide a specification like whether a field is compulsory or not, whether a field accepts null value or not, what should be the minimum length of the field for e.g. if we have mobile number as a field then it must have minimum and maximum length as 10 digits.

Implementation of basic field validation using RAML:

From above RAML, we can understand that we have only one resource i.e. customer with POST request which will accept a json object person which is having 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 will not accept null value. A mobile property will accept a string with minimum length 10. This field is a compulsory field. This field will not accept null value.

If we do not provide the person object with the above conditions, then the error will be generated by APIKIT router as APIKIT:BAD_REQUEST.

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

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

Suppose we do not provide any input to the customer endpoint, then person key not found error will be given as output.

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

If we provide only a name with five characters & do not provide mobile as a parameter, then it will give a mobile key expected with length 10 error. 

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

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.