< Summary - Kestrun — Combined Coverage

Information
Class: Public.Form.New-KrFormPartRule
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Form/New-KrFormPartRule.ps1
Tag: Kestrun/Kestrun@d9261bd752e45afa789d10bc0c82b7d5724d9589
Line coverage
0%
Covered lines: 0
Uncovered lines: 34
Coverable lines: 34
Total lines: 140
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 02/05/2026 - 00:28:18 Line coverage: 0% (0/34) Total lines: 140 Tag: Kestrun/Kestrun@d9261bd752e45afa789d10bc0c82b7d5724d9589

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Form/New-KrFormPartRule.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Creates a new form part rule.
 4.DESCRIPTION
 5    This function creates and adds a form part rule to the server's form options collection.
 6    It allows you to specify various parameters for the form part rule, such as name, description,
 7    allowed content types, maximum size, and storage options.
 8.PARAMETER Rules
 9    An array of existing form part rules to which the new rule will be added.
 10.PARAMETER Name
 11    The name of the form part rule.
 12.PARAMETER Scope
 13    The multipart scope that this rule applies to. When omitted, the rule applies only at the root level.
 14.PARAMETER Description
 15    A description of the form part rule.
 16.PARAMETER Required
 17    Indicates whether the form part is required.
 18.PARAMETER  AllowOnlyOne
 19    Indicates whether only one instance of the form part is allowed.
 20.PARAMETER AllowedContentTypes
 21    An array of allowed content types for the form part.
 22.PARAMETER AllowedExtensions
 23    An array of allowed file extensions for the form part.
 24.PARAMETER MaxBytes
 25    The maximum size in bytes for the form part.
 26.PARAMETER DecodeMode
 27    The decode mode for the form part.
 28.PARAMETER DestinationPath
 29    The destination path where the form part should be stored.
 30.PARAMETER StoreToDisk
 31    Indicates whether the form part should be stored to disk.
 32.EXAMPLE
 33    New-KrFormPartRule -Name 'file' -Required -AllowedContentTypes 'text/plain' -MaxBytes 1048576
 34    This example adds a form part rule named 'file' that is required, allows only 'text/plain' content type,
 35    and has a maximum size of 1 MB.
 36.NOTES
 37    This function is part of the Kestrun.Forms module and is used to define form part rules.
 38#>
 39function New-KrFormPartRule {
 40    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
 41    [KestrunRuntimeApi('Definition')]
 42    [CmdletBinding()]
 43    [OutputType([Kestrun.Forms.KrFormPartRule[]])]
 44    [OutputType([System.Array])]
 45    param(
 46        [Parameter(ValueFromPipeline)]
 47        [Kestrun.Forms.KrFormPartRule[]] $Rules,
 48
 49        [Parameter(Mandatory = $true)]
 50        [string] $Name,
 51
 52        [Parameter()]
 53        [string] $Scope,
 54
 55        [Parameter()]
 56        [string] $Description,
 57
 58        [Parameter()]
 59        [switch] $Required,
 60
 61        [Parameter()]
 62        [switch] $AllowOnlyOne,
 63
 64        [Parameter()]
 65        [string[]] $AllowedContentTypes,
 66
 67        [Parameter()]
 68        [string[]] $AllowedExtensions,
 69
 70        [Parameter()]
 71        [long] $MaxBytes,
 72
 73        [Parameter()]
 74        [KrPartDecodeMode] $DecodeMode = [KrPartDecodeMode]::None,
 75
 76        [Parameter()]
 77        [string] $DestinationPath,
 78
 79        [Parameter()]
 80        [switch] $StoreToDisk
 81    )
 82    begin {
 083        $bag = [System.Collections.Generic.List[Kestrun.Forms.KrFormPartRule]]::new()
 84        # Ensure the server instance is resolved
 085        $Server = Resolve-KestrunServer
 86    }
 87    process {
 088        if ( $null -ne $Rules ) {
 089            $bag.AddRange($Rules)
 90        }
 91    }
 92    end {
 093        $Rule = [Kestrun.Forms.KrFormPartRule]::new()
 094        $Rule.Name = $Name
 095        if ($PSBoundParameters.ContainsKey('Scope')) {
 096            $Rule.Scope = $Scope
 97        }
 098        if ($PSBoundParameters.ContainsKey('Description')) {
 099            $Rule.Description = $Description
 100        }
 0101        if ($PSBoundParameters.ContainsKey('Required')) {
 0102            $Rule.Required = $Required.IsPresent
 103        }
 0104        if ($PSBoundParameters.ContainsKey('AllowOnlyOne')) {
 0105            $Rule.AllowMultiple = -not $AllowOnlyOne.IsPresent
 106        }
 0107        if ($PSBoundParameters.ContainsKey('AllowedContentTypes')) {
 0108            $Rule.AllowedContentTypes.Clear()
 0109            foreach ($type in $AllowedContentTypes) {
 0110                $Rule.AllowedContentTypes.Add($type)
 111            }
 112        }
 0113        if ($PSBoundParameters.ContainsKey('AllowedExtensions')) {
 0114            $Rule.AllowedExtensions.Clear()
 0115            foreach ($ext in $AllowedExtensions) {
 0116                $Rule.AllowedExtensions.Add($ext)
 117            }
 118        }
 0119        if ($PSBoundParameters.ContainsKey('MaxBytes')) {
 0120            $Rule.MaxBytes = $MaxBytes
 121        }
 0122        if ($PSBoundParameters.ContainsKey('DecodeMode')) {
 0123            $Rule.DecodeMode = $DecodeMode
 124        }
 0125        if ($PSBoundParameters.ContainsKey('DestinationPath')) {
 0126            $Rule.DestinationPath = $DestinationPath
 127        }
 0128        if ($PSBoundParameters.ContainsKey('StoreToDisk')) {
 0129            $Rule.StoreToDisk = $StoreToDisk.IsPresent
 130        }
 131
 0132        if (-not ($Server.AddFormPartRule($Rule))) {
 0133            throw "Failed to add form part rule named '$Name' because a rule with the same name already exists."
 134        }
 135
 136        # Add to output collection
 0137        $bag.Add($Rule) | Out-Null
 0138        , [Kestrun.Forms.KrFormPartRule[]] $bag.ToArray()
 139    }
 140}

Methods/Properties

New-KrFormPartRule()