fix(uns): enforce the UNS segment rule on every authoring path (#535)
v2-ci / build (push) Successful in 4m30s
v2-ci / unit-tests (push) Failing after 16m11s

An area or line could be named "Packaging" or "Line 1", save cleanly, and then
fail the NEXT deployment with UnsSegmentInvalid. Because a deployment snapshots
the whole database, one badly-named area blocked EVERY cluster's deploy - and
whoever hit that was usually not whoever typed the name. Only the equipment form
carried the rule; the deploy gate held its own private copy.

The rule now lives once, on Configuration.Validation.UnsSegment, and both ends
call it. DraftValidator no longer restates it.

Enforced in UnsTreeService, not just on the forms:
- Create/Update Area, Line and Equipment reject an invalid name before touching
  the row.
- The CSV import reports a bad name as an ERROR, not a skip - a skip means "you
  already have this row", which would tell the operator the equipment exists
  when it does not. Good rows in the same batch still insert.

A form attribute could never have covered the import, and with no bUnit in this
repo a rule only a form enforces is also a rule no test can reach. The forms
keep an attribute for the immediate message, but it now references the shared
pattern rather than restating it - a second copy is how these drifted apart.

One behaviour change beyond the fix: UnsSegment.AttributePattern admits
"_default", which the equipment form's hand-written literal rejected. The form
was stricter than the gate it was mirroring, so a _default row was uneditable
through the AdminUI.

Tests: 41, falsified with three production mutations - dropping the CreateArea
guard reds exactly the CreateArea rejections, turning the import error back into
a skip reds only the import test, and narrowing AttributePattern reds only the
attribute/IsValid agreement. Every rejection is paired with a name that must
still be accepted, or a guard that refused everything would pass.

Live-verified on docker-dev: the area modal now refuses "Packaging" in place
with the rule spelled out, "packaging-b" saves, and a CSV import of two rows
reports Inserted 1 / Skipped 0 / Errors 1 naming the offending row - the path no
form attribute reaches. Rig cleaned up and a further deployment sealed.

Claude-Session: https://claude.ai/code/session_015p7wGqy3YpZNCpDzTpGMKo
This commit is contained in:
Joseph Doherty
2026-08-01 11:53:58 -04:00
parent 9303289d05
commit 1a1dbc3f00
8 changed files with 418 additions and 20 deletions
@@ -16,8 +16,6 @@ namespace ZB.MOM.WW.OtOpcUa.Configuration.Validation;
/// </summary>
public static class DraftValidator
{
private static readonly Regex UnsSegment = new(@"^[a-z0-9-]{1,32}$", RegexOptions.Compiled);
private const string UnsDefaultSegment = "_default";
private const int MaxPathLength = 200;
/// <summary>
@@ -598,28 +596,21 @@ public static class DraftValidator
}
}
private static bool IsValidSegment(string? s) =>
s is not null && (UnsSegment.IsMatch(s) || s == UnsDefaultSegment);
// The rule itself lives on UnsSegment, which the AdminUI's authoring guards also call — a second
// copy here is exactly how the two drifted apart (Gitea #535). Only the per-level wording is local.
private static void ValidateUnsSegments(DraftSnapshot draft, List<ValidationError> errors)
{
foreach (var a in draft.UnsAreas)
if (!IsValidSegment(a.Name))
errors.Add(new("UnsSegmentInvalid",
$"UnsArea.Name '{a.Name}' does not match [a-z0-9-]{{1,32}} or '_default'",
a.UnsAreaId));
if (!UnsSegment.IsValid(a.Name))
errors.Add(new("UnsSegmentInvalid", UnsSegment.Describe("UnsArea.Name", a.Name), a.UnsAreaId));
foreach (var l in draft.UnsLines)
if (!IsValidSegment(l.Name))
errors.Add(new("UnsSegmentInvalid",
$"UnsLine.Name '{l.Name}' does not match [a-z0-9-]{{1,32}} or '_default'",
l.UnsLineId));
if (!UnsSegment.IsValid(l.Name))
errors.Add(new("UnsSegmentInvalid", UnsSegment.Describe("UnsLine.Name", l.Name), l.UnsLineId));
foreach (var e in draft.Equipment)
if (!IsValidSegment(e.Name))
errors.Add(new("UnsSegmentInvalid",
$"Equipment.Name '{e.Name}' does not match [a-z0-9-]{{1,32}} or '_default'",
e.EquipmentId));
if (!UnsSegment.IsValid(e.Name))
errors.Add(new("UnsSegmentInvalid", UnsSegment.Describe("Equipment.Name", e.Name), e.EquipmentId));
}
/// <summary>Cluster.Enterprise + Site + area + line + equipment + 4 slashes ≤ 200 chars.</summary>
@@ -0,0 +1,73 @@
using System.Text.RegularExpressions;
namespace ZB.MOM.WW.OtOpcUa.Configuration.Validation;
/// <summary>
/// The one authority on what a UNS path segment — a <c>UnsArea.Name</c>, <c>UnsLine.Name</c> or
/// <c>Equipment.Name</c> — may be.
/// </summary>
/// <remarks>
/// <para>
/// A segment becomes a level of the UNS NodeId (<c>Area/Line/Equipment/EffectiveName</c>), so the
/// rule is not cosmetic: it is what keeps a NodeId parseable and stable.
/// </para>
/// <para>
/// ⚠️ <b>This exists because the rule was duplicated and drifted (Gitea #535).</b> The deploy gate
/// held it privately in <c>DraftValidator</c> and the AdminUI restated it on the equipment form
/// alone, so an area or line could be named <c>Packaging</c> or <c>Line 1</c>, saved, and then
/// fail the deploy with <c>UnsSegmentInvalid</c>. Because a deployment snapshots the whole
/// database, one badly-named area blocked <b>every</b> cluster's deploy. Anything that validates
/// a UNS name must reference this type rather than restate the pattern — a second copy is how it
/// drifted the first time.
/// </para>
/// </remarks>
public static class UnsSegment
{
/// <summary>
/// The reserved segment for an unnamed level. Accepted alongside <see cref="Pattern"/>, so a
/// validator that applies the pattern <em>alone</em> is stricter than the deploy gate and would
/// make such a row uneditable.
/// </summary>
public const string DefaultSegment = "_default";
/// <summary>
/// The segment pattern, as a constant so it can be used in a
/// <c>[RegularExpression]</c> attribute (which needs a compile-time constant).
/// </summary>
/// <remarks>
/// ⚠️ This does <b>not</b> admit <see cref="DefaultSegment"/> — an attribute cannot express the
/// alternation and stay readable. <see cref="AttributePattern"/> is the one to put on a form;
/// <see cref="IsValid"/> is the one to call in code.
/// </remarks>
public const string Pattern = "^[a-z0-9-]{1,32}$";
/// <summary>
/// <see cref="Pattern"/> widened to admit <see cref="DefaultSegment"/> — the form of the rule to
/// put in a <c>[RegularExpression]</c> attribute, so the UI accepts exactly what the deploy gate
/// accepts and no less.
/// </summary>
public const string AttributePattern = "^([a-z0-9-]{1,32}|_default)$";
/// <summary>The message shown for a rejected segment, in the UI and from the service guards.</summary>
public const string Message =
"Lowercase letters, digits and dashes only; 1-32 characters. This becomes a level of the OPC UA "
+ "NodeId, so a space or a capital would make the deployment fail.";
private static readonly Regex Rx = new(Pattern, RegexOptions.Compiled);
/// <summary>Whether a name is a usable UNS path segment.</summary>
/// <param name="segment">The candidate name.</param>
/// <returns><see langword="true"/> when it may be deployed.</returns>
public static bool IsValid(string? segment) =>
segment is not null && (Rx.IsMatch(segment) || segment == DefaultSegment);
/// <summary>
/// The error for a rejected segment, naming the level and the offending value — the shape the
/// deploy gate reports, so an operator sees the same sentence wherever it is caught.
/// </summary>
/// <param name="level">The entity and property, e.g. <c>UnsArea.Name</c>.</param>
/// <param name="segment">The rejected value.</param>
/// <returns>The message.</returns>
public static string Describe(string level, string? segment) =>
$"{level} '{segment}' does not match [a-z0-9-]{{1,32}} or '{DefaultSegment}'";
}