testing java script online example , including payload slitly modify ,

java script as below. 

https://www.programiz.com/javascript/online-compiler/

payload as constant. as shown below.

---

note:   // context.setVariable("validRequest2", false);

or /vice versa.

 console.log("validRequest2:", isValid);

----

catch (e) { // Handle JSON parsing errors or other unexpected errors console.log("Error:", e.message);

help extract error.

----

try {

  // Retrieve the incoming request content as a string

  var requestContent = `

  {

    "despatchStock": {

      "companyCode": "",

      "idKey": "3PLWMSSTABLEID",

      "sourceType": "PLANT",

      "source": {

        "location": "RSE",

        "name1": " ",

        "name2": " ",

        "houseNumber": " ",

        "street1": " ",

        "street2": " ",

        "postalCode": " ",

        "poBox": " ",

        "city": " ",

        "state": " ",

        "country": " ",

        "phone": " ",

        "globalLocationNumber": " "

      },

      "destinationType": "CUSTOMER",

      "destination": {

        "location": " ",

        "name1": " ",

        "name2": " ",

        "houseNumber": " ",

        "street1": " ",

        "street2": " ",

        "postalCode": " ",

        "poBox": " ",

        "city": " ",

        "state": " ",

        "country": " ",

        "phone": " ",

        "globalLocationNumber": " "

      },

      "billOfLading": " ",

      "despatchDeliveryId": "PSO2400029",

      "postingDate": "2024-01-09T10:20:12UTC+00:00",

      "referenceDocumentNumber": " ",

      "documentText": " ",

      "externalUniqueReference": " ",

      "actualGrossWeight": "1234.5",

      "actualWeightUom": " KGM",

      "actualGrossVolume": "1234 ",

      "actualVolumeUom": "MTQ",

      "containerSealNumber1": " ",

      "containerSealNumber2": " ",

      "standardCarrierAlphaCode": " ",

      "carrierId": " ",

      "containerID": " ",

      "items": {

        "item": [

          {

            "ssccNumber": "388096074513191698 ",

            "packagingMaterial": " ",

            "externalHU2": " ",

            "itemPosition": "10000",

            "materialReference": "2990010011 ",

            "eanOrUpcReference": "3182550793735",

            "batch": "347C1RGY05",

            "vendorBatch": " ",

            "quantityInSuom": "1",

            "uomCode": "EA",

            "quantityInBuom": "1",

            "baseUom": "EA",

            "conversionFactorToBuom": " ",

            "storageLocation": " ",

            "plant": " RSE",

            "stockType": " ",

            "lotNumber": " ",

            "wmsQualityStatus": "AVAILABLE",

            "qualityStatus": "Released",

            "positiveRelease": "Released",

            "positiveReleaseDate": "2023-11-26T10:20:12UTC+00",

            "productionDate": "2023-11-22T10:20:12UTC+00",

            "bestBeforeDate": "2025-05-22T10:20:12UTC+00",

            "materialType": "FG",

            "itemDescription": " ",

            "localPalletId": " ",

            "localReferenceNumber": " ",

            "transactionReason": " ",

            "remainingQuantity": " ",

            "transactionReference": " ",

            "customerPurchaseOrderNumber": "ABCD",

            "shipToPurchaseOrderNumber": " ",

            "parentSsccs": {

              "parentSscc": [

                {

                  "parentHUNumber": " ",

                  "parentHuPickedQuantity": " ",

                  "localPalletId": " "

                }

              ]

            }

          },

          {

            "ssccNumber": "388096074513340263",

            "packagingMaterial": " ",

            "externalHU2": " ",

            "itemPosition": "20000",

            "materialReference": "2522020012 ",

            "eanOrUpcReference": "3182550793736",

            "batch": "350B1RGY02",

            "vendorBatch": " ",

            "quantityInAuom": " ",

            "uomCode": " ",

            "quantityInBuom": "2",

            "baseUom": "EA",

            "conversionFactorToBuom": " ",

            "storageLocation": " ",

            "plant": " RSE",

            "stockType": " ",

            "lotNumber": " ",

            "wmsQualityStatus": " AVAILABLE",

            "qualityStatus": "Released",

            "positiveRelease": "",

            "positiveReleaseDate": "2023-12-16T10:20:12UTC+00",

            "productionDate": "2023-12-12T10:20:12UTC+00",

            "bestBeforeDate": "2025-06-11T10:20:12UTC+00",

            "materialType": " ",

            "itemDescription": " ",

            "localPalletId": " ",

            "localReferenceNumber": " ",

            "customerPurchaseOrderNumber": "ABCD",

            "shipToPurchaseOrderNumber": " ",

            "transactionReason": " ",

            "remainingQuantity": " ",

            "transactionReference": " ",

            "parentSsccs": {

              "parentSscc": [

                {

                  "parentHUNumber": " ",

                  "parentHuPickedQuantity": " ",

                  "localPalletId": " "

                }

              ]

            }

          },

          {

            "ssccNumber": "388096074513191698",

            "packagingMaterial": " ",

            "externalHU2": " ",

            "itemPosition": "20000",

            "materialReference": "2522020012 ",

            "eanOrUpcReference": "3182550793736",

            "batch": "348C1RGY05",

            "vendorBatch": " ",

            "quantityInAuom": " ",

            "uomCode": " ",

            "quantityInBuom": "1",

            "baseUom": "EA",

            "conversionFactorToBuom": " ",

            "storageLocation": " ",

            "plant": " RSE ",

            "stockType": " ",

            "lotNumber": " ",

            "wmsQualityStatus": " AVAILABLE",

            "qualityStatus": "Released",

            "positiveRelease": "Released",

            "positiveReleaseDate": "2023-11-27T10:20:12UTC+00",

            "productionDate": "2023-11-23T10:20:12UTC+00",

            "bestBeforeDate": "2025-05-23T10:20:12UTC+00",

            "materialType": "FG",

            "itemDescription": " ",

            "localPalletId": " ",

            "localReferenceNumber": " ",

            "transactionReason": " ",

            "remainingQuantity": " ",

            "transactionReference": " ",

            "customerPurchaseOrderNumber": "ABCD",

            "shipToPurchaseOrderNumber": " ",

            "parentSsccs": {

              "parentSscc": [

                {

                  "parentHUNumber": " ",

                  "parentHuPickedQuantity": " ",

                  "localPalletId": " "

                }

              ]

            }

          }

        ]

      }

    }

  }

  `;

// Retrieve the incoming request content as a string

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



  // Parse the JSON payload into a JavaScript object

  var request = JSON.parse(requestContent);


  // Define allowed fields for the main object

  var allowedFields = [

    "despatchStock"

  ];


  // 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 'despatchStock' object if present

  if (isValid && request.despatchStock) {

    isValid = validateObject(request.despatchStock, [

      "companyCode", "idKey", "sourceType", "source", "destinationType", "destination",

      "billOfLading", "despatchDeliveryId", "postingDate", "referenceDocumentNumber", "documentText",

      "externalUniqueReference", "actualGrossWeight", "actualWeightUom", "actualGrossVolume",

      "actualVolumeUom", "containerSealNumber1", "containerSealNumber2", "standardCarrierAlphaCode",

      "carrierId", "containerID", "items"

    ]);


    // Validate nested 'source' object if present

    if (isValid && request.despatchStock.source) {

      isValid = validateObject(request.despatchStock.source, [

        "location", "name1", "name2", "houseNumber", "street1", "street2", "postalCode", "poBox",

        "city", "state", "country", "phone", "globalLocationNumber"

      ]);

    }


    // Validate nested 'destination' object if present

    if (isValid && request.despatchStock.destination) {

      isValid = validateObject(request.despatchStock.destination, [

        "location", "name1", "name2", "houseNumber", "street1", "street2", "postalCode", "poBox",

        "city", "state", "country", "phone", "globalLocationNumber"

      ]);

    }


    // Validate nested 'items' object if present

    if (isValid && request.despatchStock.items) {

      isValid = validateObject(request.despatchStock.items, [

        "item"

      ]);


      // Validate 'item' array if present within items

      if (isValid && request.despatchStock.items.item) {

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

          var item = request.despatchStock.items.item[i];

          isValid = validateObject(item, [

            "ssccNumber", "packagingMaterial", "externalHU2", "itemPosition", "materialReference",

            "eanOrUpcReference", "batch", "vendorBatch", "quantityInSuom", "uomCode", "quantityInBuom",

            "baseUom", "conversionFactorToBuom", "storageLocation", "plant", "stockType", "lotNumber",

            "wmsQualityStatus", "qualityStatus", "positiveRelease", "positiveReleaseDate",

            "productionDate", "bestBeforeDate", "materialType", "itemDescription", "localPalletId",

            "localReferenceNumber", "transactionReason", "remainingQuantity", "transactionReference",

            "customerPurchaseOrderNumber", "shipToPurchaseOrderNumber", "parentSsccs"

          ]);


          if (!isValid) break;


          // Validate 'parentSsccs' object if present within item

          if (item.parentSsccs) {

            isValid = validateObject(item.parentSsccs, [

              "parentSscc"

            ]);


            // Validate 'parentSscc' array if present within parentSsccs

            if (isValid && item.parentSsccs.parentSscc) {

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

                var parentSsc = item.parentSsccs.parentSscc[j];

                isValid = validateObject(parentSsc, [

                  "parentHUNumber", "parentHuPickedQuantity", "localPalletId"

                ]);


                if (!isValid) break;

              }

            }

          }

        }

      }

    }

  }

// Set the validation result in a context variable  //commont

  //context.setVariable("validRequest2", isValid); //disabled



  // Set the validation result in a context variable

  console.log("validRequest2:", isValid);

} catch (e) {

 // Handle JSON parsing errors or other unexpected errors

 // context.setVariable("validRequest2", false);


    console.log("Error:", e);

  // Handle JSON parsing errors or other unexpected errors

  console.log("validRequest2:", false);

   console.log("Validating despatchStock:", isValid);

console.log("Validating source:", request.despatchStock.source);

console.log("Validating destination:", request.despatchStock.destination);

console.log("Validating items:", request.despatchStock.items);

}

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