Private/Test-SFExternalIdDefinition.ps1

function Test-SFExternalIdDefinition {
    [CmdletBinding()]
    [OutputType([void])]
    param (
        [Parameter(Mandatory)]
        [System.Collections.IDictionary]$Definition,

        [Parameter(Mandatory)]
        [string]$Context
    )

    $idField = [string]$Definition.IdField
    $idFields = if ($null -eq $Definition.IdFields) { @() } else { @($Definition.IdFields) }
    $hasIdField = -not [string]::IsNullOrWhiteSpace($idField)
    $hasIdFields = $idFields.Count -gt 0

    if ($hasIdField -eq $hasIdFields) {
        throw "$Context must define exactly one of IdField or IdFields."
    }

    if ($hasIdFields) {
        if ($idFields.Count -lt 2) {
            throw "$Context must use IdField for a single field or define at least two IdFields."
        }
        if ([string]::IsNullOrEmpty([string]$Definition.IdSeparator)) {
            throw "$Context must define IdSeparator when IdFields is used."
        }
        foreach ($fieldDefinition in $idFields) {
            if ($fieldDefinition -isnot [System.Collections.IDictionary]) {
                throw "Every IdFields entry for $Context must be an object containing Field and optional Format."
            }
            if ([string]::IsNullOrWhiteSpace([string]$fieldDefinition.Field)) {
                throw "Every IdFields entry for $Context must define a non-empty Field."
            }
        }
    }
}