Introduction
Welcome to the documentation for YMValidator – a versatile and powerful library for validating user input in iOS applications. This documentation will guide you through the various features and usage of YMValidator, helping you integrate it seamlessly into your projects.
Getting Started
Before diving into the details of YMValidator, let’s quickly go through the installation process and basic setup.
Installation
To install YMValidator in your project, you have a couple of options:
- Using Cocoapods: Add the following line to your Podfile:
- Manual Installation: Download the latest version of YMValidator from the GitHub repository and manually add the source files to your project.
pod 'YMValidator'
Then run pod install
command.
Usage
Once you have successfully installed YMValidator, you can start using it in your project. Follow the steps below:
- Import the YMValidator module:
- Create an instance of the validator:
- Start validating your user input:
import YMValidator
let validator = YMValidator()
YMValidator provides a set of pre-defined validation rules to handle common cases. For example, to validate an email address, use the following:
let email = "example@example.com"
if validator.validateEmail(email) {
// Email is valid
} else {
// Email is not valid
}
Available Validation Rules
YMValidator offers a wide range of validation rules that you can use to validate different types of user input. Some of the available rules include:
- Email Validation: Validates the format of an email address.
- Password Validation: Validates the strength of a password, including criteria such as length, character types, etc.
- Phone Number Validation: Validates the format of a phone number.
- URL Validation: Validates the format of a URL.
- Custom Validation: Allows you to define your own custom validation rules.
Each validation rule comes with its own set of options and parameters that you can customize based on your project’s requirements. Refer to the API documentation for a detailed explanation of each rule and its usage.
Advanced Usage
While the pre-defined validation rules cover most common cases, YMValidator also offers advanced features that you can leverage for more complex scenarios. Some of these features include:
- Conditional Validation: Allowing or skipping validation based on certain conditions.
- Validation Groups: Grouping multiple validations together to be executed as a whole.
- Error Messages: Customizing error messages for failed validations.
- Asynchronous Validation: Performing asynchronous validations, such as checking if an email is already in use.
These advanced features give you fine-grained control over the validation process, enabling you to handle complex validation scenarios with ease.
Examples
Here are a few examples to illustrate the usage of YMValidator in different scenarios:
Example 1: Validating Email
“`swift
let email = “example@example.com”
if validator.validateEmail(email) {
// Email is valid
} else {
// Email is not valid
}
“`
Example 2: Validating Phone Number
“`swift
let phoneNumber = “+1234567890”
if validator.validatePhoneNumber(phoneNumber) {
// Phone number is valid
} else {
// Phone number is not valid
}
“`
Example 3: Creating Custom Validation Rule
“`swift
let customRule = YMValidationRule { input in
return input.contains(“foo”) && input.hasPrefix(“bar”)
}
let input = “foobar”
if validator.validate(input, rule: customRule) {
// Input is valid
} else {
// Input is not valid
}
“`
These examples should give you a good starting point for integrating YMValidator into your projects. Make sure to explore the API documentation for a more comprehensive understanding of the available features and options.