FioriPasscodePolicy
public struct FioriPasscodePolicy
A passcode policy that defines the requirements a passcode must satisfy.
This is the SwiftUI counterpart of the UIKit FUIPasscodePolicy. It carries the pure
validation business logic (minimum length, required character classes, minimum unique
characters, and user-defined rules) and is decoupled from any UI so it can be reused and
unit-tested. Use it together with PasscodePolicyAuthenticationStyle to drive a
requirement checklist in the Authentication component.
Example:
var policy = FioriPasscodePolicy(minLength: 8, hasDigit: true, hasUpper: true, hasSpecial: true, minUniqueChars: 4)
policy.addPasscodeRule(FioriPasscodeRule(displayName: "At least 2 lowercase", isDisplayed: true) { passcode in
passcode.unicodeScalars.filter { CharacterSet.lowercaseLetters.contains($0) }.count >= 2
})
let isValid = policy.validate(passcode: "Abc12!de")
-
The default minimum length of characters in a passcode.
Declaration
Swift
public static let defaultMinLength: Int -
Represents “no limit” for
Intproperties such asminUniqueChars.Declaration
Swift
public static let noLimit: Int -
The minimum number of characters in the passcode.
validate(passcode:)returnsfalseif the passcode length is less than this value.Declaration
Swift
public var minLength: Int -
Whether the passcode must contain at least one digit.
Declaration
Swift
public var hasDigit: Bool -
Whether the passcode must contain at least one uppercase character.
Declaration
Swift
public var hasUpper: Bool -
Whether the passcode must contain at least one lowercase character.
Declaration
Swift
public var hasLower: Bool -
Whether the passcode must contain at least one special (non-alphanumeric) character.
Declaration
Swift
public var hasSpecial: Bool -
When
true, the passcode must contain digits only. All ofhasDigit,hasUpper,hasLower, andhasSpecialare ignored when this istrue. User-defined rules added viaaddPasscodeRule(_:)are still evaluated.Declaration
Swift
public var isDigitsOnly: Bool -
The minimum number of unique characters the passcode must contain. A value of
noLimit(or0) disables this check.Declaration
Swift
public var minUniqueChars: Int -
The user-defined passcode rules added to this policy.
Declaration
Swift
public private(set) var passcodeRules: [FioriPasscodeRule] { get } -
Creates a passcode policy.
Declaration
Swift
public init(minLength: Int = FioriPasscodePolicy.defaultMinLength, hasDigit: Bool = false, hasUpper: Bool = false, hasLower: Bool = false, hasSpecial: Bool = false, isDigitsOnly: Bool = false, minUniqueChars: Int = FioriPasscodePolicy.noLimit)Parameters
minLengthMinimum number of characters. Defaults to
defaultMinLength(8).hasDigitRequires at least one digit. Defaults to
false.hasUpperRequires at least one uppercase character. Defaults to
false.hasLowerRequires at least one lowercase character. Defaults to
false.hasSpecialRequires at least one special character. Defaults to
false.isDigitsOnlyRequires the passcode to contain digits only. Defaults to
false.minUniqueCharsMinimum number of unique characters. Defaults to
noLimit. -
Adds a user-defined passcode rule to the policy.
Declaration
Swift
public mutating func addPasscodeRule(_ rule: FioriPasscodeRule)Parameters
ruleThe rule to add.
-
Validates a passcode against the policy.
The passcode passes only if every enabled requirement is satisfied. User-defined rules added via
addPasscodeRule(_:)are always evaluated, including whenisDigitsOnlyistrue.Declaration
Swift
public func validate(passcode: String) -> BoolParameters
passcodeThe passcode to check.
Return Value
trueif the passcode satisfies all requirements. -
Returns each enabled requirement together with whether it is currently satisfied by the given passcode. This is intended to drive a live requirement checklist in the UI.
Note
Displayed user-defined rules (seeaddPasscodeRule(_:)) are always included, regardless ofisDigitsOnly.Declaration
Swift
public func requirements(for passcode: String) -> [(requirement: FioriPasscodeRequirement, isSatisfied: Bool)]Parameters
passcodeThe passcode to evaluate.
Return Value
The list of requirements with their current satisfaction state.