-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAllValid.ps1
More file actions
40 lines (35 loc) · 1019 Bytes
/
AllValid.ps1
File metadata and controls
40 lines (35 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<#
.SYNOPSIS
Determines if any validation passes, given an object.
.DESCRIPTION
Determines if all of the `[ValidateScript]` or `[ValidatePattern]` attributes on a `[ScriptBlock]` pass, given one or more inputs.
Any input considered valid by all `[ValidateScript]` or `[ValidatePattern]` will be returned.
If there is no validation present, no objects will be returned.
.EXAMPLE
{
[ValidatePattern("a")]
[ValidatePattern("c$")]
param()
}.AllValid("c","b","a","abc")
.EXAMPLE
{
[ValidateScript({$_ % 2})]
[ValidateScript({-not ($_ % 3)})]
param()
}.AllValid(1..10)
#>
param()
$allArgs = $args | & { process { $_ }}
, @(
:nextArg foreach ($arg in $allArgs) {
$validatedArg = $false
foreach ($attr in $this.Attributes) {
if (-not $attr.Validate) { continue }
if (-not $attr.Validate($arg)) { continue nextArg}
else { $validatedArg = $true }
}
if ($validatedArg) {
$arg
}
}
)