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 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.
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:
If we don’t provide any input to the customer endpoint, the person key not found error will be given as output.
If we provide a person as a blank object, it will give a name and mobile key not found error.
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.
Input will be accepted only if the name is with five characters and mobile with 10 characters.
— By Manish Prabhu