Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validation issue on DeviceID when creating a new LoRa Device #917

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}