Skip to main content

I have two fields in a JSON request and from those two fields at least one should be in the incoming request.

 

requirement is that requests should not enter into the API. I am confused about how to restrict/block the request without entering in the API

3 answers
  1. Anurag Sharma (Nagarro) Forum Ambassador
    Mar 8, 2022, 5:49 AM

    Hello,

     

    You can do it By RAML validations (if you dont have Any idea about RAML),

     

    Please use this links - https://docs.mulesoft.com/api-manager/1.x/tutorial-design-an-api

    https://docs.mulesoft.com/api-manager/1.x/design-raml-api-task

     

    request will not enter to you main flows of API , api kit router will throw the error after RAMlL validation if it didnt pass.

     

    use this link for apikit router based API's- https://docs.mulesoft.com/apikit/4.x/apikit-4-raml-flow-concept

     

    Now, create a JSON schema in RAML and appply a validation rule there

     

    let's suppose you have the request as below , create a JSON file with Name (requestValidation.json )

     

    {

    field1: "",

    field2: ""

    }

    Then for this create a schema validaiton file (requstValidation.json)as below

    {

    "$schema": "http://json-schema.org/draft-04/schema#",

    "type": "object",

    "properties": {

    "field1": {

    "type": "string"

    },

    "field2": {

    "type": "string"

    }

    },

    "anyOf": [{

    "required" : ["field1"]

    }, {

    "required" : ["field2"]

    }]

    }

     

    you can validate this schema using this - https://www.jsonschemavalidator.net/

     

    test it by tweaking your input.After this include schmea file in you resource as below example

    /account:

    post:

    body:

    application/json:

    schema: !include requestValidation.json

    example: !include requestValidation.json

    for more understanding on desiginig the RAML go thorugh these links also

     

    https://github.com/raml-apis/Uber/blob/staging/api.raml

    https://github.com/mulesoft/raml-tutorial-200/blob/master/jukebox-api.raml

     

    I have attached a zip file that you can use to undersatnd RAML designing kind of best practices. (I created this for other user who asked to design a RAML).it doesnt have JSON schema validation but you will get some knowledge.

     

    Happy Learning !!

     

    Regards,

    Anurag Sharma

0/9000