Operator "missing" not working properly in JsonLogic

420 views Asked by At

I am using JsonLogic to validate my input payload with the rules defined using JsonLogic. I am able to test the rules using "Play with it" tool and my rules work perfectly fine against my input data.

But when I run the same rules through my .net Core application by passing payload from Postman the rules always return the else condition even when it should the error from if condition.

{
"if": [
{
  "missing": [
     "ProposedProjectDetails.IsFreezone",
    "ProposedProjectDetails.InterestedToLeaseFrom",
    "ProposedProjectDetails.IndustryType",
    "ProposedProjectDetails.OtherType",
    "ProposedProjectDetails.ProjectDescription",
    "ProposedProjectDetails.OutputofFacility",
    "ProposedProjectDetails.ProductionCapacity",
    "ProposedProjectDetails.ProductionCapacityVolume",
    "ProposedProjectDetails.MainRawMaterials",
    "ProposedProjectDetails.RawMaterialQuantity",
    "ProposedProjectDetails.RawMaterialEstimatedCost",
    "ProposedProjectDetails.RawMaterialEstimatedTotInvestment",
    "ProposedProjectDetails.AnnualSalesRevenue",
    "ProposedProjectDetails.ConstructionStartDate",
    "ProposedProjectDetails.Skilledjobs",
    "ProposedProjectDetails.TotalAccomodationRequired",
    "ProposedProjectDetails.TotalWorkerSalary",
    "ProposedProjectDetails.EBITDA",
    "ProposedProjectDetails.PortCargoImports",
  ]
},
"Missing mandatory inputs",
"all good"
]
}

Sample input payload is

{
"companyId": "string",
"serviceCode": "IPA",
"serviceType": "string",
"serviceName": "string",
"crmApplicationId": "string",
"crmReferenceNumber": "string",
"portalReferenceNumber": "string",
"data": {
    "proposedProjectDetails": {
        "outputofFacility": 2,
        "productionCapacity": 0,
        "productionCapacityVolume": 0,
        "others": "string",
        "shiftsPerDay": 0
     }
   }
}

My .Net code which is evaluating this is

  public ValidationResponse Validate(JObject validation, JObject data)
    {
        var evaluator = new JsonLogicEvaluator(EvaluateOperators.Default);
        var result = evaluator.Apply(validation, data);

        return new ValidationResponse
        {
            IsValid = (string.Equals("All Fine", result.ToString(), StringComparison.OrdinalIgnoreCase)),
            Message = result
        };
    }

When I run above code with JsonRules and above Payload, I always get all good in the response. But since I am missing required data in payload, it should get the error Missing mandatory inputs which I do get in JsonLogic "Play with it" tool.

1

There are 1 answers

0
sarath On

Try keeping the names as json mapping fields camel case once like below

"if": [
   {
      "missing": [
                  "proposedProjectDetails.isFreezone",
                  "proposedProjectDetails.interestedToLeaseFrom",
                 ]
   },
   "Missing mandatory inputs",
   "all good"
]

}