From 373d45c90dad3f4e94785fa4d069a369034dc8de Mon Sep 17 00:00:00 2001 From: ChristopheRib63 <83826573+ChristopheRib63@users.noreply.github.com> Date: Mon, 18 Jul 2022 15:05:17 +0200 Subject: [PATCH] Fix validation issue on DeviceID when creating a new LoRa Device (#917) * Modification validator * Display validation errors in corresponding fields Co-authored-by: crib Co-authored-by: Audrey Serra --- .../Pages/Devices/CreateDevicePage.razor | 4 ++-- .../Validators/DeviceDetailsValidator.cs | 24 ++++++++++++++++++- .../Validators/LoRaDeviceDetailsValidator.cs | 6 ----- .../Shared/Models/v1.0/DeviceDetails.cs | 5 ++++ .../Models/v1.0/LoRaWAN/LoRaDeviceDetails.cs | 2 ++ 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/AzureIoTHub.Portal/Client/Pages/Devices/CreateDevicePage.razor b/src/AzureIoTHub.Portal/Client/Pages/Devices/CreateDevicePage.razor index b4c8b16b1..e56d065a5 100644 --- a/src/AzureIoTHub.Portal/Client/Pages/Devices/CreateDevicePage.razor +++ b/src/AzureIoTHub.Portal/Client/Pages/Devices/CreateDevicePage.razor @@ -79,8 +79,8 @@ id=@nameof(DeviceDetails.DeviceID) Label="Device ID / DevEUI" Variant="Variant.Outlined" + Validation=@(standardValidator.ValidateValue) For="@(()=> Device.DeviceID)" - Required="true" Mask="@maskLoRaDeviceID" HelperText="DeviceID must contain 16 hexadecimal characters (numbers from 0 to 9 and/or letters from A to F)" /> } @@ -90,8 +90,8 @@ id=@nameof(DeviceDetails.DeviceID) Label="Device ID" Variant="Variant.Outlined" + Validation=@(standardValidator.ValidateValue) For="@(()=> Device.DeviceID)" - Required="true" HelperText="The device identifier should be of ASCII 7-bit alphanumeric characters plus certain special characters" /> } diff --git a/src/AzureIoTHub.Portal/Client/Validators/DeviceDetailsValidator.cs b/src/AzureIoTHub.Portal/Client/Validators/DeviceDetailsValidator.cs index 25faede43..dd2c10d41 100644 --- a/src/AzureIoTHub.Portal/Client/Validators/DeviceDetailsValidator.cs +++ b/src/AzureIoTHub.Portal/Client/Validators/DeviceDetailsValidator.cs @@ -3,6 +3,10 @@ namespace AzureIoTHub.Portal.Client.Validators { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; using AzureIoTHub.Portal.Models.v10; using FluentValidation; @@ -18,12 +22,30 @@ public DeviceDetailsValidator() .NotEmpty() .WithMessage("ModelId is required."); - _ = RuleFor(x => x.DeviceID) + When(x => x.IsLoraWan, () => + { + _ = RuleFor(x => x.DeviceID) + .NotEmpty() + .Length(1, 128) + .Matches("[A-Z0-9]{16}") + .WithMessage("DeviceID is required. It should be a 16 bit hex string."); + }).Otherwise(() => + { + _ = RuleFor(x => x.DeviceID) .NotEmpty() .WithMessage("DeviceID is required.") .Length(1, 128) .Matches("[a-zA-Z0-9\\-.+%_#*?!(),:=@$']") .WithMessage("DeviceID is required. It should be a case-sensitive string (up to 128 characters long) of ASCII 7-bit alphanumeric characters plus certain special characters: - . + % _ # * ? ! ( ) , : = @ $ '."); + }); } + + public Func>> ValidateValue => async (model, propertyName) => + { + var result = await ValidateAsync(ValidationContext.CreateWithOptions((DeviceDetails)model, x => x.IncludeProperties(propertyName))); + if (result.IsValid) + return Array.Empty(); + return result.Errors.Select(e => e.ErrorMessage); + }; } } diff --git a/src/AzureIoTHub.Portal/Client/Validators/LoRaDeviceDetailsValidator.cs b/src/AzureIoTHub.Portal/Client/Validators/LoRaDeviceDetailsValidator.cs index 6b85bfd6b..20b429ce7 100644 --- a/src/AzureIoTHub.Portal/Client/Validators/LoRaDeviceDetailsValidator.cs +++ b/src/AzureIoTHub.Portal/Client/Validators/LoRaDeviceDetailsValidator.cs @@ -42,12 +42,6 @@ public LoRaDeviceDetailsValidator() .NotEmpty() .When(x => !x.UseOTAA) .WithMessage("DevAddr is required."); - - _ = RuleFor(x => x.DeviceID) - .NotEmpty() - .Length(1, 128) - .Matches("[A-Z0-9]{16}") - .WithMessage("DeviceID is required. It should be a 16 bit hex string."); } } } diff --git a/src/AzureIoTHub.Portal/Shared/Models/v1.0/DeviceDetails.cs b/src/AzureIoTHub.Portal/Shared/Models/v1.0/DeviceDetails.cs index 2de7ffb73..9ad9c5a3a 100644 --- a/src/AzureIoTHub.Portal/Shared/Models/v1.0/DeviceDetails.cs +++ b/src/AzureIoTHub.Portal/Shared/Models/v1.0/DeviceDetails.cs @@ -56,5 +56,10 @@ public class DeviceDetails /// List of custom device tags and their values. /// public Dictionary Tags { get; set; } = new(); + + /// + /// true if this instance is lorawan; otherwise, false. + /// + public virtual bool IsLoraWan { get; private set; } } } diff --git a/src/AzureIoTHub.Portal/Shared/Models/v1.0/LoRaWAN/LoRaDeviceDetails.cs b/src/AzureIoTHub.Portal/Shared/Models/v1.0/LoRaWAN/LoRaDeviceDetails.cs index f02e85327..ace9fc887 100644 --- a/src/AzureIoTHub.Portal/Shared/Models/v1.0/LoRaWAN/LoRaDeviceDetails.cs +++ b/src/AzureIoTHub.Portal/Shared/Models/v1.0/LoRaWAN/LoRaDeviceDetails.cs @@ -68,5 +68,7 @@ public class LoRaDeviceDetails : LoRaDeviceBase /// The Device Current RXDelay. /// public string ReportedRXDelay { get; set; } + + public override bool IsLoraWan => true; } }