I am using Mule 4.4 community edition I have a GET endpoint with 4 query params:
empId ( mandatory )
collegeId ( mandatory )
subject ( either subject or score is required )
score ( either subject or score is required )
what is the best way to be doing these validations ?
Here is what I have tried but am not satisfied with the outcome ... Validating 'empId' and 'collegeId' through Open API spec ( required attribute )
parameters:
- name: empId
in: query
description: The web user account
required: true
schema:
type: string
- name: collegeId
in: query
description: The web organisation
required: true
However if I simply pass an empty string against these , no validation is triggered .... Tried using pattern such as :
pattern: "^[A-Za-z0-9]+$"
while this does prevent empty / null strings the error message is not satisfactory :
Invalid value ' ' for uri parameter empId. string [ ] does not match pattern ^[A-Za-z0-9]+$
Question: Can I override the error message in Open API spec to make it something easier on the eyes ?
Now on to 'subject' and 'score' , atleast one of these should be non null or empty Did not find anything except this link here How do we enforce atleast one of the two has a non null / empty value ( via Open api spec 3.0.1 )
So I thought I will hand craft validation , using validators but they seem quite clunky ex - for a simple check like 'not null or empty' have to use two validators .....
<flow name="get:employee-search" >
<validation:any doc:name="Any" >
<validation:is-not-null doc:name="Is subject not null" value="#[attributes.queryParams['subject']]" message="${validation.subject}" />
<validation:is-not-null doc:name="Is score not null" value="#[attributes.queryParams['score']]" message="${validation.score}" />
<validation:is-not-blank-string doc:name="Is subject not blank string" value="#[attributes.queryParams['subject']]" message="${validation.subject}"/>
<validation:is-not-blank-string doc:name="Is score not blank string" value="#[attributes.queryParams['score']]" message="${validation.score}"/>
</validation:any>
also if both fields are not sent then the error message for both validators shows up
Question: should I write a groovy script to perform these simple validations Just wanted to know if that is the best approach or something better ?