in src/PSRule/Commands/AssertWithinCommand.cs [60:130]
protected override void ProcessRecord()
{
if (!IsRuleScope())
throw RuleScopeException(LanguageKeywords.Within);
var targetObject = InputObject ?? GetTargetObject();
var expected = !Not;
var match = false;
var found = string.Empty;
// Pass with any match, or (-Not) fail with any match
if (ObjectHelper.GetPath(
bindingContext: PipelineContext.CurrentThread,
targetObject: targetObject,
path: Field,
caseSensitive: false,
value: out object fieldValue))
{
for (var i = 0; (Value == null || i < Value.Length) && !match; i++)
{
// Null compare
if (fieldValue == null || Value == null || Value[i] == null)
{
if (fieldValue == null && (Value == null || Value[i] == null))
{
match = true;
RunspaceContext.CurrentThread.VerboseConditionMessage(
condition: RuleLanguageNouns.Within,
message: PSRuleResources.WithinTrue,
args: fieldValue);
}
else
{
break;
}
}
// String compare
else if (fieldValue is string strValue && Value[i].BaseObject is string)
{
if ((_LikePattern == null && _Comparer.Equals(Value[i].BaseObject, strValue)) || (_LikePattern != null && _LikePattern[i].IsMatch(strValue)))
{
match = true;
RunspaceContext.CurrentThread.VerboseConditionMessage(
condition: RuleLanguageNouns.Within,
message: PSRuleResources.WithinTrue,
args: strValue);
found = Value[i].BaseObject.ToString();
}
}
// Everything else
else if (Value[i].Equals(fieldValue))
{
match = true;
RunspaceContext.CurrentThread.VerboseConditionMessage(
condition: RuleLanguageNouns.Within,
message: PSRuleResources.WithinTrue,
args: fieldValue);
found = Value[i].ToString();
}
}
}
var result = expected == match;
RunspaceContext.CurrentThread.VerboseConditionResult(condition: RuleLanguageNouns.Within, outcome: result);
if (!(result || TryReason(Reason)))
{
WriteReason(Not ? string.Format(Thread.CurrentThread.CurrentCulture, ReasonStrings.WithinNot, found) : ReasonStrings.Within);
}
WriteObject(result);
}