java script for validating unknow fileds coming from source. throw error.

 java script :

validate incoming payload fields.

-------------------

worked:

try {

    // Retrieve the incoming request content as a string

    var requestContent = context.getVariable("request.content");

    

    // Parse the JSON payload into a JavaScript object

    var request = JSON.parse(requestContent);

    

    // Define allowed fields for the main object

    var allowedFields = [

        "actualGrossWeight", "actualVolumeUom", "actualVolumeWeight", "actualWeightUom",

        "billOfLading", "carrierId", "companyCode", "containerID", "containerSealNumber1",

        "containerSealNumber2", "despatchDeliveryId", "destination", "destinationType",

        "documentText", "externalUniqueReference", "idKey", "items", "postingDate",

        "referenceDocumentNumber", "source", "sourceType", "standardCarrierAlphaCode"

    ];


    // Function to validate fields of an object

    function validateObject(obj, allowedFields) {

        for (var field in obj) {

            if (obj.hasOwnProperty(field) && allowedFields.indexOf(field) === -1) {

                return false; // Unknown field found

            }

        }

        return true; // All fields are valid

    }


    // Initialize validation status

    var isValid = validateObject(request, allowedFields);


    // Validate nested 'destination' object if present

    if (isValid && request.destination) {

        isValid = validateObject(request.destination, [

            "city", "country", "globalLocationNumber", "houseNumber", "location", "name1", "name2",

            "phone", "poBox", "postalCode", "state", "street1", "street2"

        ]);

    }


    // Validate nested 'source' object if present

    if (isValid && request.source) {

        isValid = validateObject(request.source, [

            "city", "country", "globalLocationNumber", "houseNumber", "location", "name1", "name2",

            "phone", "poBox", "postalCode", "state", "street1", "street2"

        ]);

    }


    // Validate 'items' array if present

    if (isValid && request.items) {

        for (var i = 0; i < request.items.length; i++) {

            var item = request.items[i];

            isValid = validateObject(item, [

                "baseUom", "batch", "bestBeforeDate", "conversionFactorToBuom", "externalHU2",

                "itemDescription", "itemPosition", "localPalletId", "localReferenceNumber", "lotNumber",

                "materialReference", "materialType", "packagingMaterial", "parentSsccs", "plant",

                "positiveRelease", "positiveReleaseDate", "productionDate", "qualityStatus", "quantityInAuom",

                "quantityInBuom", "remainingQuantity", "ssccNumber", "stockType", "storageLocation",

                "transactionReason", "transactionReference", "uomCode", "vendorBatch", "wmsQualityStatus"

            ]);

            

            if (!isValid) break;


            // Validate 'parentSsccs' array if present within item

            if (item.parentSsccs) {

                for (var j = 0; j < item.parentSsccs.length; j++) {

                    var parentSsc = item.parentSsccs[j];

                    isValid = validateObject(parentSsc, [

                        "parentSsccs", "localPalletId", "parentHUNumber", "parentHuPickedQuantity"

                    ]);


                    if (!isValid) break;


                    // Validate nested 'parentSsccs' array within parentSsc

                    if (parentSsc.parentSsccs) {

                        for (var k = 0; k < parentSsc.parentSsccs.length; k++) {

                            var nestedParentSsc = parentSsc.parentSsccs[k];

                            isValid = validateObject(nestedParentSsc, [

                                "localPalletId", "parentHUNumber", "parentHuPickedQuantity"

                            ]);


                            if (!isValid) break;

                        }

                    }

                }

            }

        }

    }


    // Set the validation result in a context variable

    context.setVariable("validRequest", isValid);

} catch (e) {

    // Handle JSON parsing errors or other unexpected errors

    context.setVariable("validRequest", false);

}

-------------
not worked but given by chatgpt
try {
  var request = JSON.parse(context.getVariable("request.content"));

  // Allowed fields for the main object
  const allowedFields = [
    "actualGrossWeight", "actualVolumeUom", "actualVolumeWeight", "actualWeightUom",
    "billOfLading", "carrierId", "companyCode", "containerID", "containerSealNumber1",
    "containerSealNumber2", "despatchDeliveryId", "destination", "destinationType",
    "documentText", "externalUniqueReference", "idKey", "items", "postingDate",
    "referenceDocumentNumber", "source", "sourceType", "standardCarrierAlphaCode"
  ];

  // Initialize validation flag
  var isValid = validateObject(request, allowedFields);

  // Validate nested 'destination' and 'source' objects if present
  if (isValid && request.destination) {
    isValid = validateObject(request.destination, [
      "city", "country", "globalLocationNumber", "houseNumber", "location", "name1", "name2",
      "phone", "poBox", "postalCode", "state", "street1", "street2"
    ]);
  }

  if (isValid && request.source) {
    isValid = validateObject(request.source, [
      "city", "country", "globalLocationNumber", "houseNumber", "location", "name1", "name2",
      "phone", "poBox", "postalCode", "state", "street1", "street2"
    ]);
  }

  // Validate 'items' array if present
  if (isValid && request.items) {
    for (const item of request.items) {
      isValid = validateObject(item, [
        "baseUom", "batch", "bestBeforeDate", "conversionFactorToBuom", "externalHU2",
        "itemDescription", "itemPosition", "localPalletId", "localReferenceNumber", "lotNumber",
        "materialReference", "materialType", "packagingMaterial", "parentSsccs", "plant",
        "positiveRelease", "positiveReleaseDate", "productionDate", "qualityStatus", "quantityInAuom",
        "quantityInBuom", "remainingQuantity", "ssccNumber", "stockType", "storageLocation",
        "transactionReason", "transactionReference", "uomCode", "vendorBatch", "wmsQualityStatus"
      ]);

      if (!isValid) break;

      // Validate 'parentSsccs' array if present
      if (item.parentSsccs) {
        for (const parentSsc of item.parentSsccs) {
          isValid = validateObject(parentSsc, [
            "parentSsccs", "localPalletId", "parentHUNumber", "parentHuPickedQuantity"
          ]);

          if (!isValid) break;

          // Validate nested 'parentSsccs' array
          if (parentSsc.parentSsccs) {
            for (const nestedParentSsc of parentSsc.parentSsccs) {
              isValid = validateObject(nestedParentSsc, [
                "localPalletId", "parentHUNumber", "parentHuPickedQuantity"
              ]);

              if (!isValid) break;
            }
          }
        }
      }
    }
  }

  context.setVariable("validRequest", isValid);
} catch (error) {
  context.setVariable("validRequest", false);
}

// Helper function to validate object fields
function validateObject(obj, allowedFields) {
  for (const field in obj) {
    if (!allowedFields.includes(field)) {
      return false; // Unknown field found
    }
  }
  return true; // All fields are valid
}
---------------
try {
  var request = JSON.parse(context.getVariable("request.content"));

  // Allowed fields for the main object
  const allowedFields = [
    "actualGrossWeight",
    "actualVolumeUom",
    "actualVolumeWeight",
    "actualWeightUom",
    "billOfLading",
    "carrierId",
    "companyCode",
    "containerID",
    "containerSealNumber1",
    "containerSealNumber2",
    "despatchDeliveryId",
    "destination",
    "destinationType",
    "documentText",
    "externalUniqueReference",
    "idKey",
    "items",
    "postingDate",
    "referenceDocumentNumber",
    "source",
    "sourceType",
    "standardCarrierAlphaCode"
  ];

  var isValid = true; // Flag to track validation status

  // Validate the main object fields
  if (!validateObject(request, allowedFields)) {
    isValid = false;
  }

  // Validate nested objects
  if (isValid && request.destination) {
    if (!validateObject(request.destination, [
      "city",
      "country",
      "globalLocationNumber",
      "houseNumber",
      "location",
      "name1",
      "name2",
      "phone",
      "poBox",
      "postalCode",
      "state",
      "street1",
      "street2"
    ])) {
      isValid = false;
    }
  }

  if (isValid && request.source) {
    if (!validateObject(request.source, [
      "city",
      "country",
      "globalLocationNumber",
      "houseNumber",
      "location",
      "name1",
      "name2",
      "phone",
      "poBox",
      "postalCode",
      "state",
      "street1",
      "street2"
    ])) {
      isValid = false;
    }
  }

  if (isValid && request.items) {
    for (const item of request.items) {
      if (!validateObject(item, [
        "baseUom",
        "batch",
        "bestBeforeDate",
        "conversionFactorToBuom",
        "externalHU2",
        "itemDescription",
        "itemPosition",
        "localPalletId",
        "localReferenceNumber",
        "lotNumber",
        "materialReference",
        "materialType",
        "packagingMaterial",
        "parentSsccs",
        "plant",
        "positiveRelease",
        "positiveReleaseDate",
        "productionDate",
        "qualityStatus",
        "quantityInAuom",
        "quantityInBuom",
        "remainingQuantity",
        "ssccNumber",
        "stockType",
        "storageLocation",
        "transactionReason",
        "transactionReference",
        "uomCode",
        "vendorBatch",
        "wmsQualityStatus"
      ])) {
        isValid = false;
        break; // Exit loop if validation fails
      }

      if (isValid && item.parentSsccs) {
        for (const parentSsc of item.parentSsccs) {
          if (!validateObject(parentSsc, [
            "parentSsccs",
            "localPalletId",
            "parentHUNumber",
            "parentHuPickedQuantity"
          ])) {
            isValid = false;
            break; // Exit loop if validation fails
          }

          if (isValid && parentSsc.parentSsccs) {
            for (const nestedParentSsc of parentSsc.parentSsccs) {
              if (!validateObject(nestedParentSsc, [
                "localPalletId",
                "parentHUNumber",
                "parentHuPickedQuantity"
              ])) {
                isValid = false;
                break; // Exit loop if validation fails
              }
            }
          }
        }
      }

      if (!isValid) break; // Exit outer loop if validation fails
    }
  }

  context.setVariable("validRequest", isValid);
} catch (error) {
  context.setVariable("validRequest", false);
}

// Helper function to validate object fields
function validateObject(obj, allowedFields) {
  for (const field in obj) {
    if (!allowedFields.includes(field)) {
      return false; // Return false if an unknown field is found
    }
  }
  return true; // Return true if all fields are valid
}

----
try {
  var request = JSON.parse(context.getVariable("request.content"));

  // Allowed fields for the main object
  const allowedFields = [
    "actualGrossWeight",
    "actualVolumeUom",
    "actualVolumeWeight",
    "actualWeightUom",
    "billOfLading",
    "carrierId",
    "companyCode",
    "containerID",
    "containerSealNumber1",
    "containerSealNumber2",
    "despatchDeliveryId",
    "destination",
    "destinationType",
    "documentText",
    "externalUniqueReference",
    "idKey",
    "items",
    "postingDate",
    "referenceDocumentNumber",
    "source",
    "sourceType",
    "standardCarrierAlphaCode"
  ];

  // Validate the main object fields
  if (!validateObject(request, allowedFields)) {
    context.setVariable("validRequest", false);
    return;
  }

  // Validate nested objects
  if (request.destination && !validateObject(request.destination, [
    "city",
    "country",
    "globalLocationNumber",
    "houseNumber",
    "location",
    "name1",
    "name2",
    "phone",
    "poBox",
    "postalCode",
    "state",
    "street1",
    "street2"
  ])) {
    context.setVariable("validRequest", false);
    return;
  }

  if (request.source && !validateObject(request.source, [
    "city",
    "country",
    "globalLocationNumber",
    "houseNumber",
    "location",
    "name1",
    "name2",
    "phone",
    "poBox",
    "postalCode",
    "state",
    "street1",
    "street2"
  ])) {
    context.setVariable("validRequest", false);
    return;
  }

  if (request.items) {
    for (const item of request.items) {
      if (!validateObject(item, [
        "baseUom",
        "batch",
        "bestBeforeDate",
        "conversionFactorToBuom",
        "externalHU2",
        "itemDescription",
        "itemPosition",
        "localPalletId",
        "localReferenceNumber",
        "lotNumber",
        "materialReference",
        "materialType",
        "packagingMaterial",
        "parentSsccs",
        "plant",
        "positiveRelease",
        "positiveReleaseDate",
        "productionDate",
        "qualityStatus",
        "quantityInAuom",
        "quantityInBuom",
        "remainingQuantity",
        "ssccNumber",
        "stockType",
        "storageLocation",
        "transactionReason",
        "transactionReference",
        "uomCode",
        "vendorBatch",
        "wmsQualityStatus"
      ])) {
        context.setVariable("validRequest", false);
        return;
      }

      if (item.parentSsccs) {
        for (const parentSsc of item.parentSsccs) {
          if (!validateObject(parentSsc, [
            "parentSsccs",
            "localPalletId",
            "parentHUNumber",
            "parentHuPickedQuantity"
          ])) {
            context.setVariable("validRequest", false);
            return;
          }

          if (parentSsc.parentSsccs) {
            for (const nestedParentSsc of parentSsc.parentSsccs) {
              if (!validateObject(nestedParentSsc, [
                "localPalletId",
                "parentHUNumber",
                "parentHuPickedQuantity"
              ])) {
                context.setVariable("validRequest", false);
                return;
              }
            }
          }
        }
      }
    }
  }

  context.setVariable("validRequest", true);
} catch (error) {
  context.setVariable("validRequest", false);
}

// Helper function to validate object fields
function validateObject(obj, allowedFields) {
  for (const field in obj) {
    if (!allowedFields.includes(field)) {
      return false; // Return false if an unknown field is found
    }
  }
  return true; // Return true if all fields are valid
}
-----------------
by vertex.
try {
    // Retrieve the incoming request content as a string
    var requestContent = context.getVariable("request.content");
    
    // Parse the JSON payload into a JavaScript object
    var request = JSON.parse(requestContent);
    
    // Define allowed fields for the main object
    var allowedFields = [
        "actualGrossWeight", "actualVolumeUom", "actualVolumeWeight", "actualWeightUom",
        "billOfLading", "carrierId", "companyCode", "containerID", "containerSealNumber1",
        "containerSealNumber2", "despatchDeliveryId", "destination", "destinationType",
        "documentText", "externalUniqueReference", "idKey", "items", "postingDate",
        "referenceDocumentNumber", "source", "sourceType", "standardCarrierAlphaCode"
    ];

    // Function to validate fields of an object
    function validateObject(obj, allowedFields) {
        for (var field in obj) {
            if (obj.hasOwnProperty(field) && allowedFields.indexOf(field) === -1) {
                return false; // Unknown field found
            }
        }
        return true; // All fields are valid
    }

    // Initialize validation status
    var isValid = validateObject(request, allowedFields);

    // Validate nested 'destination' object if present
    if (isValid && request.destination) {
        isValid = validateObject(request.destination, [
            "city", "country", "globalLocationNumber", "houseNumber", "location", "name1", "name2",
            "phone", "poBox", "postalCode", "state", "street1", "street2"
        ]);
    }

    // Validate nested 'source' object if present
    if (isValid && request.source) {
        isValid = validateObject(request.source, [
            "city", "country", "globalLocationNumber", "houseNumber", "location", "name1", "name2",
            "phone", "poBox", "postalCode", "state", "street1", "street2"
        ]);
    }

    // Validate 'items' array if present
    if (isValid && request.items) {
        for (var i = 0; i < request.items.length; i++) {
            var item = request.items[i];
            isValid = validateObject(item, [
                "baseUom", "batch", "bestBeforeDate", "conversionFactorToBuom", "externalHU2",
                "itemDescription", "itemPosition", "localPalletId", "localReferenceNumber", "lotNumber",
                "materialReference", "materialType", "packagingMaterial", "parentSsccs", "plant",
                "positiveRelease", "positiveReleaseDate", "productionDate", "qualityStatus", "quantityInAuom",
                "quantityInBuom", "remainingQuantity", "ssccNumber", "stockType", "storageLocation",
                "transactionReason", "transactionReference", "uomCode", "vendorBatch", "wmsQualityStatus"
            ]);
            
            if (!isValid) break;

            // Validate 'parentSsccs' array if present within item
            if (item.parentSsccs) {
                for (var j = 0; j < item.parentSsccs.length; j++) {
                    var parentSsc = item.parentSsccs[j];
                    isValid = validateObject(parentSsc, [
                        "parentSsccs", "localPalletId", "parentHUNumber", "parentHuPickedQuantity"
                    ]);

                    if (!isValid) break;

                    // Validate nested 'parentSsccs' array within parentSsc
                    if (parentSsc.parentSsccs) {
                        for (var k = 0; k < parentSsc.parentSsccs.length; k++) {
                            var nestedParentSsc = parentSsc.parentSsccs[k];
                            isValid = validateObject(nestedParentSsc, [
                                "localPalletId", "parentHUNumber", "parentHuPickedQuantity"
                            ]);

                            if (!isValid) break;
                        }
                    }
                }
            }
        }
    }

    // Set the validation result in a context variable
    context.setVariable("validRequest", isValid);
} catch (e) {
    // Handle JSON parsing errors or other unexpected errors
    context.setVariable("validRequest", false);
}
------------

Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024