How to validate a JSON request based on a schema using Javascript

546 views Asked by At

I am trying to build out an API endpoint within Workato using a Javascript plugin. Part of the requirement is to validate if the JSON request is valid based on the schema that I defined.

Specifically, I need to throw a 400 Bad Request error if a required field is not provided or if the provided value does not match the expected data type (e.g. string when expecting number).

My original thought was to define a "checkRequest" function that will intake the JSON request object with every possible data point and essentially run through a huge list of if/else if /else conditions to test every value for "null" and use "type of" to test for data type and then return if I encounter an error.

However, note that the schema is HUGE with about 100 different data points and consists of nested objects within objects so I was hoping to find the most optimized way to iterate rather than relying on a bulk of if statements.

See my code below and a snippet of a request based on the schema (can't share full schema):

My code:

function checkRequest({
  partner_id,
  merchant_reference_id,
  optional_string,
}) {
  let response = {
    validRequest: true,
    responseStatus: "",
    errorTitle: "",
    errorDetail: "",
    errorCode: "",
  };

  let badRequestTitle = "Bad Request";
  let requiredFieldErrorDetail = (field) => {
    return field + " is a required field but was not provided in request.";
  };
  let wrongFormatErrorDetailDetail = (field, inputDataType, expectedDataType) =>
    field +
    " expected data type is " +
    expectedDataType +
    ", but " +
    inputDataType +
    " was received.";

  //partner_id
  if (partner_id == "" || partner_id == null) {
    response.validRequest = false;
    response.errorDetail = requiredFieldErrorDetail("partnerID");
    console.log(response);
    return response;
  } else if (typeof partner_id != "string") {
    response.validRequest = false;
    response.responseStatus = "400";
    response.errorCode = "40001";
    response.errorTitle = badRequestTitle;
    response.errorDetail = wrongFormatErrorDetail(
      "partnerID",
      typeof partner_id,
      "string"
    );
    console.log(response);
    return response;
  }

  //merchant_reference_ID
  else if (merchant_reference_id == "" || merchant_reference_id == null) {
    response.validRequest = false;
    response.errorDetail = requiredFieldErrorDetail("partnerID");
    console.log(response);
    return response;
  } else if (typeof merchant_reference_id != "string") {
    response.validRequest = false;
    response.responseStatus = "400";
    response.errorCode = "40001";
    response.errorTitle = badRequestTitle;
    response.errorDetail = wrongFormatErrorDetail(
      "partnerID",
      typeof merchant_reference_id,
      "string"
    );
    console.log(response);
    return response;
  }
  //else
  else {
    console.log(response);
    return response;
  }
}

let requestBody = {
  partner_id: "",
  merchant_reference_id: "aa",
  optional_string: "3",
};

checkRequest(requestBody);

Sample request snippet (assume that some fields are required and others are optional):

{
  "application": {
       "partnerReferral": {
            "partnerID": "mg3e09f8-a8dd-44e6-bb06-55293b799318",
            "merchantReferenceID": "mg3e09f8a8dd44e6bb06-55293b799318"
       },
       "businessInformation": {
            "identity": [
                 {
                      "identifier": "EMPLOYER_IDENTIFICATION_NUMBER",
                      "value": "77-1122333"
                 }
            ],
            "businessType": "ASSOCIATION",
            "classification": {
                 "code": "SIC",
                 "value": "string"
            }
    }
  }
}

Hoping to rely on vanilla Javascript to accomplish this is the best way possible, but open to plugins if necessary.

0

There are 0 answers