Skip to content

Create Canadian Postal code Validation rule #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions SwiftValidator/Rules/CanadianPostalCodeRule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// CanadianPostalCode function .swift
// SwiftValidator
//
// Created by Abdalla Elnajjar on 2020-11-08.
// Copyright © 2020 jpotts18. All rights reserved.
//


import Foundation

/**
`PhoneNumberRule` is a subclass of Rule that defines how a phone number is validated.
*/
public class CanadianPostalCodeRule: RegexRule {


/// Canadian Postal Code express string to be used in validation.
static let regex = "^[A-Za-z]\\d[A-Za-z][ -]?\\d[A-Za-z]\\d$"

/**
Initializes a `PhoneNumberRule` object. Used to validate that a field has a valid phone number.

- parameter message: Error message that is displayed if validation fails.
- returns: An initialized `CanadianPostalCode` object, or nil if an object could not be created for some reason that would not result in an exception.
*/
public convenience init(message : String = "Enter a valid c digit Canadian Postal Code") {
self.init(regex: CanadianPostalCodeRule.regex, message : message)
}

}
23 changes: 22 additions & 1 deletion SwiftValidatorTests/SwiftValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ class SwiftValidatorTests: XCTestCase {
XCTAssertNotNil(ZipCodeRule().errorMessage())
}

// MARK: Canadian Postal Code

func testCanadianPostalCodeValid() {
let validCanadianPostalCodes = ["N6K 2V9","N6K-2V9","n7k 1n2","n7k-1n2","n7k1n2","N6K2V9"]
for code in validCanadianPostalCodes {
XCTAssertTrue(CanadianPostalCodeRule().validate(code), "Canadian Postal Code should be valid")
}
}

func testCanadianPostalCodeInvalid() {
let invaliCanadianPostalCodesd = ["N6K 2V","N68-279","989 1n2","7k-1n2"]
for code in invaliCanadianPostalCodesd {
XCTAssertTrue(CanadianPostalCodeRule().validate(code), "Canadian Postal Code should be valid")
}
}

func testCanadianPostalCodeMessage() {
XCTAssertNotNil(CanadianPostalCodeRule().errorMessage())
}


// MARK: Email

func testEmail() {
Expand Down Expand Up @@ -407,7 +428,7 @@ class SwiftValidatorTests: XCTestCase {
}
REGISTER_TXT_FIELD.text = INVALID_EMAIL
REGISTER_VALIDATOR.validateField(REGISTER_TXT_FIELD) { error in
XCTAssert(error?.errorMessage.characters.count ?? 0 > 0, "Should state 'invalid email'")
XCTAssert(error?.errorMessage.count ?? 0 > 0, "Should state 'invalid email'")
}
}

Expand Down
36 changes: 26 additions & 10 deletions Validator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
7CC1E4D51C637C8500AF013C /* IPV4Rule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CC1E4D41C637C8500AF013C /* IPV4Rule.swift */; };
7CC1E4D71C637F6E00AF013C /* ISBNRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CC1E4D61C637F6E00AF013C /* ISBNRule.swift */; };
7CC1E4DB1C63BFA600AF013C /* HexColorRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CC1E4DA1C63BFA600AF013C /* HexColorRule.swift */; };
8E363B6F2558D1DF00FF41E5 /* CanadianPostalCodeRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E363B6E2558D1DF00FF41E5 /* CanadianPostalCodeRule.swift */; };
C87F606A1E2B678B00EB8429 /* CardExpiryMonthRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = C87F60691E2B678B00EB8429 /* CardExpiryMonthRule.swift */; };
C87F606C1E2B68C900EB8429 /* CardExpiryYearRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = C87F606B1E2B68C900EB8429 /* CardExpiryYearRule.swift */; };
FB465CB81B9884F400398388 /* SwiftValidator.h in Headers */ = {isa = PBXBuildFile; fileRef = FB465CB71B9884F400398388 /* SwiftValidator.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -66,6 +67,7 @@
7CC1E4D41C637C8500AF013C /* IPV4Rule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IPV4Rule.swift; sourceTree = "<group>"; };
7CC1E4D61C637F6E00AF013C /* ISBNRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ISBNRule.swift; sourceTree = "<group>"; };
7CC1E4DA1C63BFA600AF013C /* HexColorRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HexColorRule.swift; sourceTree = "<group>"; };
8E363B6E2558D1DF00FF41E5 /* CanadianPostalCodeRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CanadianPostalCodeRule.swift; sourceTree = "<group>"; };
C87F60691E2B678B00EB8429 /* CardExpiryMonthRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardExpiryMonthRule.swift; sourceTree = "<group>"; };
C87F606B1E2B68C900EB8429 /* CardExpiryYearRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardExpiryYearRule.swift; sourceTree = "<group>"; };
FB465CB31B9884F400398388 /* SwiftValidator.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftValidator.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -238,6 +240,7 @@
FB465CEE1B9889EA00398388 /* ValidationRule.swift */,
FB465CEF1B9889EA00398388 /* ZipCodeRule.swift */,
62C1821C1C6312F5003788E7 /* ExactLengthRule.swift */,
8E363B6E2558D1DF00FF41E5 /* CanadianPostalCodeRule.swift */,
);
path = Rules;
sourceTree = "<group>";
Expand Down Expand Up @@ -334,17 +337,19 @@
TargetAttributes = {
62D1AE2B1A1E6D4500E4DFF8 = {
CreatedOnToolsVersion = 6.1;
DevelopmentTeam = J7YCHK8ZJ4;
LastSwiftMigration = 0820;
TestTargetID = 62D1AE161A1E6D4400E4DFF8;
};
FB465CB21B9884F400398388 = {
CreatedOnToolsVersion = 6.4;
DevelopmentTeam = J7YCHK8ZJ4;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
FB465CBC1B9884F400398388 = {
CreatedOnToolsVersion = 6.4;
DevelopmentTeam = J7YCHK8ZJ4;
LastSwiftMigration = 1020;
TestTargetID = 62D1AE161A1E6D4400E4DFF8;
};
};
};
Expand Down Expand Up @@ -428,6 +433,7 @@
FB465CFB1B9889EA00398388 /* RegexRule.swift in Sources */,
7CC1E4CF1C636B4500AF013C /* AlphaRule.swift in Sources */,
62D9B2561C7C0B2A00BAFCE3 /* ValidationDelegate.swift in Sources */,
8E363B6F2558D1DF00FF41E5 /* CanadianPostalCodeRule.swift in Sources */,
FB465CF81B9889EA00398388 /* MinLengthRule.swift in Sources */,
FB465CF71B9889EA00398388 /* MaxLengthRule.swift in Sources */,
62C1821D1C6312F5003788E7 /* ExactLengthRule.swift in Sources */,
Expand Down Expand Up @@ -578,8 +584,8 @@
62D1AE3A1A1E6D4500E4DFF8 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ENABLE_MODULES = YES;
DEVELOPMENT_TEAM = J7YCHK8ZJ4;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
Expand All @@ -588,30 +594,31 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
};
name = Debug;
};
62D1AE3B1A1E6D4500E4DFF8 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ENABLE_MODULES = YES;
DEVELOPMENT_TEAM = J7YCHK8ZJ4;
INFOPLIST_FILE = ValidatorTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
};
name = Release;
};
FB465CCC1B9884F400398388 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "";
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = J7YCHK8ZJ4;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -625,6 +632,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
"PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = "";
SKIP_INSTALL = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
Expand All @@ -636,11 +645,14 @@
FB465CCD1B9884F400398388 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "";
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = J7YCHK8ZJ4;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -650,6 +662,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
"PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = "";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 5.0;
Expand All @@ -662,7 +676,9 @@
FB465CCE1B9884F400398388 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = J7YCHK8ZJ4;
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -678,15 +694,16 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.levous.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
};
name = Debug;
};
FB465CCF1B9884F400398388 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = J7YCHK8ZJ4;
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -699,7 +716,6 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
};
name = Release;
};
Expand Down