Files
lmxopcua/src/Core/ZB.MOM.WW.OtOpcUa.Configuration/Validation/UnsSegment.cs
T
Joseph Doherty d2818e2ef2 fix(uns): #978 one UNS name authority and a script-reference gate — an illegal VirtualTag name or override is refused in the editor and at publish, and an in-use script cannot be deleted
- V3NodeIds.ValidateSegment is the single rule Uns()/UnsChild() enforce; the VirtualTag modal
  ([UnsLeafName]), UnsTreeService (VirtualTag create/update, SetReferenceOverrideAsync) and the
  deploy gate (VirtualTagNameInvalid, UnsEffectiveNameInvalid) all call it (C-1, C-4).
- Effective name is stated once (EquipmentReferenceMap.EffectiveName): an empty override falls
  back to the tag name in the gate and the Monaco catalog, as at both compose seams (C-4).
- UnsSegment patterns anchored \A..\z, so a trailing newline is rejected (C-7).
- VirtualTagScriptNotFound / ScriptedAlarmScriptNotFound deploy-gate rules; ScriptEdit refuses to
  delete a script used by a VirtualTag, ScriptedAlarm or Calculation tag and names them (C-2).
- R-5 backstop: VirtualTagHostActor skips (Error log) a plan whose NodeId cannot be built instead
  of crashing on every apply; AddressSpaceApplier counts it as a failed node instead of aborting
  the VirtualTag materialise pass.
2026-12-03 10:28:27 -05:00

77 lines
4.1 KiB
C#

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.</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.
/// <para>⚠️ Anchored with <c>\A</c>/<c>\z</c>, never <c>^</c>/<c>$</c>: in .NET <c>$</c> also matches before a
/// final <c>\n</c>, so <c>"press-1\n"</c> passed — through equipment CSV import, then the deploy gate —
/// and the applier dropped the equipment folder after a green deploy (lmxopcua#978, review C-7).</para>
/// </remarks>
public const string Pattern = @"\A[a-z0-9-]{1,32}\z";
/// <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(?:[a-z0-9-]{1,32}|_default)\z";
/// <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}'";
}