From c8b264cef6b684504b08e2f492821a66db5983be Mon Sep 17 00:00:00 2001 From: TekH Date: Fri, 27 Mar 2026 09:40:33 +0100 Subject: [PATCH] Improve null safety in InsertObjectProcedureValidator Updated validation logic to use null-forgiving operators and added null checks for nested properties in When clauses. This ensures rules are only applied when parent objects are not null, preventing possible null reference errors and improving overall robustness. --- .../InsertObjectProcedureValidator.cs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/ReC.Application/Common/Validations/InsertObjectProcedureValidator.cs b/src/ReC.Application/Common/Validations/InsertObjectProcedureValidator.cs index 153136a..f205ea7 100644 --- a/src/ReC.Application/Common/Validations/InsertObjectProcedureValidator.cs +++ b/src/ReC.Application/Common/Validations/InsertObjectProcedureValidator.cs @@ -14,59 +14,59 @@ public class InsertObjectProcedureValidator : AbstractValidator x.Entity == "ACTION", () => + When(x => x.Action != null, () => { - RuleFor(x => x.Action.ProfileId) + RuleFor(x => x.Action!.ProfileId) .NotNull() .WithMessage("ACTION requires ActionProfileId (maps to @pACTION_PROFILE_ID)."); - RuleFor(x => x.Action.EndpointId) + RuleFor(x => x.Action!.EndpointId) .NotNull() .WithMessage("ACTION requires ActionEndpointId (maps to @pACTION_ENDPOINT_ID)."); }); // ENDPOINT validation - When(x => x.Entity == "ENDPOINT", () => + When(x => x.Endpoint != null, () => { - RuleFor(x => x.Endpoint.Uri) + RuleFor(x => x.Endpoint!.Uri) .NotEmpty() .WithMessage("ENDPOINT requires EndpointUri (maps to @pENDPOINT_URI).") .MaximumLength(2000); }); // PROFILE validation - When(x => x.Entity == "PROFILE", () => + When(x => x.Profile != null, () => { - RuleFor(x => x.Profile.Name) + RuleFor(x => x.Profile!.Name) .NotEmpty() .WithMessage("PROFILE requires ProfileName (maps to @pPROFILE_NAME).") .MaximumLength(50); - RuleFor(x => x.Profile.Mandantor) + RuleFor(x => x.Profile!.Mandantor) .MaximumLength(50) - .When(x => x.Profile.Mandantor != null); + .When(x => x.Profile!.Mandantor != null); - RuleFor(x => x.Profile.Description) + RuleFor(x => x.Profile!.Description) .MaximumLength(250) - .When(x => x.Profile.Description != null); + .When(x => x.Profile!.Description != null); }); // RESULT validation - When(x => x.Entity == "RESULT", () => + When(x => x.Result != null, () => { - RuleFor(x => x.Result.ActionId) + RuleFor(x => x.Result!.ActionId) .NotNull() .WithMessage("RESULT requires ResultActionId (maps to @pRESULT_ACTION_ID)."); - RuleFor(x => x.Result.StatusId) + RuleFor(x => x.Result!.StatusId) .NotNull() .WithMessage("RESULT requires ResultStatusId (maps to @pRESULT_STATUS_ID)."); }); // ENDPOINT_PARAMS validation - When(x => x.Entity == "ENDPOINT_PARAMS", () => + When(x => x.EndpointParams != null, () => { - RuleFor(x => x.EndpointParams.GroupId) + RuleFor(x => x.EndpointParams!.GroupId) .NotNull() .WithMessage("ENDPOINT_PARAMS requires EndpointParamsGroupId (maps to @pENDPOINT_PARAMS_GROUP_ID)."); }); @@ -76,12 +76,12 @@ public class InsertObjectProcedureValidator : AbstractValidator x.AddedWho != null); - RuleFor(x => x.Endpoint.Description) + RuleFor(x => x.Endpoint!.Description) .MaximumLength(250) - .When(x => x.Endpoint.Description != null); + .When(x => x.Endpoint is { Description: not null }); - RuleFor(x => x.EndpointAuth.Description) + RuleFor(x => x.EndpointAuth!.Description) .MaximumLength(250) - .When(x => x.EndpointAuth.Description != null); + .When(x => x.EndpointAuth is { Description: not null }); } } \ No newline at end of file