Skip to content

Commit

Permalink
Fix validation issue on DeviceID when creating a new LoRa Device (#917)
Browse files Browse the repository at this point in the history
* Modification validator

* Display validation errors in corresponding fields

Co-authored-by: crib <christophe.ribeiro@cgi.com>
Co-authored-by: Audrey Serra <audrey.serra@cgi.com>
  • Loading branch information
3 people authored Jul 18, 2022
1 parent bae9886 commit 373d45c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)" />
}
Expand All @@ -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" />
}
</MudItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<DeviceDetails>.CreateWithOptions((DeviceDetails)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
}
5 changes: 5 additions & 0 deletions src/AzureIoTHub.Portal/Shared/Models/v1.0/DeviceDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,10 @@ public class DeviceDetails
/// List of custom device tags and their values.
/// </summary>
public Dictionary<string, string> Tags { get; set; } = new();

/// <summary>
/// <c>true</c> if this instance is lorawan; otherwise, <c>false</c>.
/// </summary>
public virtual bool IsLoraWan { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ public class LoRaDeviceDetails : LoRaDeviceBase
/// The Device Current RXDelay.
/// </summary>
public string ReportedRXDelay { get; set; }

public override bool IsLoraWan => true;
}
}

0 comments on commit 373d45c

Please sign in to comment.