# Upgrade to v7: ValidationResult

> Version 7 replaces three validation result types with one MTConnect.ValidationResult with IsValid, Code, and Message.

URL: https://www.trakhound.com/docs/mtconnect-net/migration/v7-validation-result

Prior to MTConnect.NET v7, three near-identical validation result structs lived side by side in the codebase — one per "domain" that produced validation outcomes. Each carried the same shape (`IsValid` + `Message`) but a different type identity, forcing handler authors to write the same shape three times and the agent to allocate per-domain wrappers at every emit site.

v7 collapses all three into a single universal type — `MTConnect.ValidationResult` — and adds a new `Code` property so subscribers can branch on the failure category without parsing the message string. Every member of the [Agent Validation Events](https://www.trakhound.com/docs/mtconnect-net/concepts/agent-validation-events) family now carries the universal type.

## What Changed

| Pre-v7 type | Namespace | Replaced by |
| --- | --- | --- |
| `AssetValidationResult` | `MTConnect.Assets` | `MTConnect.ValidationResult` |
| `ValidationResult` (DataItems) | `MTConnect.Devices.DataItems` | `MTConnect.ValidationResult` |
| `ObservationValidationResult` | `MTConnect.Observations` | `MTConnect.ValidationResult` |

The three legacy structs are removed from the public surface. Any handler signature that mentioned one of them is now phrased in terms of the universal `MTConnect.ValidationResult`.

## The New Shape

`MTConnect.ValidationResult` is a `readonly struct` with three properties and a pair of static factory methods for the common cases:

```csharp
namespace MTConnect
{
    public readonly struct ValidationResult
    {
        public bool   IsValid { get; }
        public string Code    { get; }   // machine-readable discriminator
        public string Message { get; }   // human-readable description

        public ValidationResult(bool isValid, string message = null, string code = null);

        public static ValidationResult Valid();
        public static ValidationResult Invalid(string code, string message);
    }
}
```

The `Code` property is the only structural addition. Both legacy structs already had `IsValid` and `Message` — those move across unchanged.

### Why `Code`

The pre-v7 result types only carried a free-form `Message`. Subscribers wanting to react differently to different failure categories had to substring-match the message, which broke whenever the message text was tightened or localised. `Code` is a stable, lower-case-camelCase string the agent stamps on every failure (e.g. `DeviceNull`, `DeviceUuidMissing`). Subscribers branch on `Code`; logs render `Message`.

## Migration Table

| Before | After |
| --- | --- |
| `using MTConnect.Assets; … AssetValidationResult` | `using MTConnect; … ValidationResult` |
| `using MTConnect.Devices.DataItems; … ValidationResult` | `using MTConnect; … ValidationResult` |
| `using MTConnect.Observations; … ObservationValidationResult` | `using MTConnect; … ValidationResult` |
| `new AssetValidationResult(false, "Missing SerialNumber")` | `ValidationResult.Invalid("SerialNumberMissing", "Missing SerialNumber")` (or `new ValidationResult(false, "Missing SerialNumber")` if you don't yet have a code) |
| `new ValidationResult(true)` | `ValidationResult.Valid()` |
| handler signature `(IAsset, AssetValidationResult)` | handler signature `(IAsset, ValidationResult)` |

The constructor remains compatible by parameter order — `new ValidationResult(bool, string)` still works — so the minimum migration for a callsite is to drop the legacy type name and the `using` directive.

## Before and After: Handler

```csharp
// Before — three different result types, three different using statements
using MTConnect.Assets;             // AssetValidationResult
using MTConnect.Observations;       // ObservationValidationResult
using MTConnect.Devices.DataItems;  // ValidationResult (DataItems variant)

agent.InvalidAssetAdded += (asset, AssetValidationResult result) =>
    Log.Warn($"asset {asset.AssetId}: {result.Message}");

agent.InvalidObservationAdded += (deviceUuid, dataItemKey, ObservationValidationResult result) =>
    Log.Warn($"[{deviceUuid}] {dataItemKey}: {result.Message}");
```

```csharp
// After — one type, one using, plus a stable Code for branching
using MTConnect;                    // universal ValidationResult

agent.InvalidAssetAdded += (asset, result) =>
    Log.Warn("asset {AssetId} failed ({Code}): {Message}",
             asset.AssetId, result.Code, result.Message);

agent.InvalidObservationAdded += (deviceUuid, dataItemKey, result) =>
    Log.Warn("[{DeviceUuid}] {DataItemKey} failed ({Code}): {Message}",
             deviceUuid, dataItemKey, result.Code, result.Message);
```

## Before and After: Asset Author

`Asset.IsValid(Version)` and its overrides previously returned `AssetValidationResult`; they now return the universal `ValidationResult`:

```csharp
// Before
public override AssetValidationResult IsValid(Version mtconnectVersion)
{
    if (string.IsNullOrEmpty(SerialNumber))
        return new AssetValidationResult(false, "SerialNumber property is Required");

    return new AssetValidationResult(true);
}
```

```csharp
// After
public override ValidationResult IsValid(Version mtconnectVersion)
{
    if (string.IsNullOrEmpty(SerialNumber))
        return ValidationResult.Invalid("SerialNumberMissing",
                                        "SerialNumber property is Required");

    return ValidationResult.Valid();
}
```

The structural change is type-name only; the per-asset validation logic is unaffected.

## See Also

- [Agent Validation Events](https://www.trakhound.com/docs/mtconnect-net/concepts/agent-validation-events) — the family of `Invalid*Added` events that now carry the universal type.
- [`MTConnect.NET-Common`](https://www.nuget.org/packages/MTConnect.NET-Common) — the package with the universal `MTConnect.ValidationResult` type.
- [TrakHound/MTConnect.NET#169](https://github.com/TrakHound/MTConnect.NET/pull/169) — the PR that performed the consolidation, added `InvalidDeviceAdded`, and introduced `ValidateDevice` as a pre-flight helper.
