ModelValidator

master

railwaymen/ModelValidator

ModelValidator

Framework for model validations for Swift with custom errors returned on validation.

Usage

The framework is created to be used when you want to receive custom errors on each validation. This because we don't want to always display the error as a string but sometimes we would like to highlight not valid fields and give detailed custom information about what's wrong with the provided data. Moreover, if we think about highlighting the fields in the form, we have to get all the validation errors at once for the better UX. This framework is designed for these use cases.

Installation

Swift Package Manager

The preferred installation type is by using Swift Package Manager.

CocoaPods

To install the framework by using CocoaPods:

  1. Add to Podfile:
pod 'ModelValidator'
  1. Install pods
pod install

Usage

  • Full documentation of all references you can find in Documentation folder
  • For a list of all available validators enter Validator extension docs.
  • For many more examples of usage and examples of passing and not passing rules enter Tests folder.

Example usage

Below you can see the simplest validation for a model.

import ModelValidator

struct LoginForm: Validatable {
  var email: String?
  var password: String

  func validations(validations: inout Validations<LoginForm>) {
    validations.add(\.email, !.nil && .email, error: .emailIsInvalid)
    validations.add(\.password, .count(8...), error: .passwordIsTooShort)
  }

  enum ValidationError: Error {
    case emailIsInvalid, passwordIsTooShort
  }
}

More examples you can find in tests

Contibution

If you have some idea for a new feature, create an issue or add a pull request.

We don't use Linux for Swift, so if you have a proposition to run it on Linux, please create a pull request with the needed changes.

The framework has no dependencies. Only things you should have is Swift compiler and SwiftLint on your machine.

Release

The package is released manually. To release register with a proper email account to pod trunk:

bundle exec pod trunk register $email

Make sure that you are on a commit ready for release and run release script with a proper tag name:

Script/release.sh 1.0.0

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Apr 13 2024 04:07:54 GMT-0900 (Hawaii-Aleutian Daylight Time)