'validating multiple input query params in Mule 4

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 ?



Solution 1:[1]

I do not think you can customize the error message solely via Open API Specs, However when you generate the flows in your application, it will create error handlers too for APIKIT validations. You can write a transform message within the bad request error handler to modify the response by checking the error message.

Now for the Validation part, you do not need to check for validation:is-not-null if you are checking validation:is-not-blank-string as the later will also check if the string is Null. Also you will have to merge the remaining two validators as well, otherwise, it will fail if the subject is empty even though the score is passed. For handling this you can use either validation:is-false or validation:all Scope.

With validation:all you will wrap the two validation:is-not-blank-string in a single scope, and it will fail only if both of the validation fails.

With the validation:is-false you can write custom weave expression in the module, that returns true if both the fields are blank. This will fail this validation (since it is not false) and you can customize the error message. Something like this

<validation:is-false doc:name="Is false"
    doc:id="c62a27a4-d030-4f72-9610-eacbb74dd593"
    expression="#[isBlank(attributes.queryParams.score) and isBlank(attributes.queryParams.subject)]"
    message="Both Score and Subject can not be empty" />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harshank Bansal