Code Monkey home page Code Monkey logo

swift-validator-livr's Introduction

Swift LIVR Validator ๐Ÿ“

Build Status Coverage Status Swift CocoaPods compatible License

Lightweight Swift validator supporting Language Independent Validation Rules Specification (LIVR).

Description

See LIVR Specification for detailed documentation and list of supported rules.

Features:

  • Rules are declarative and language independent
  • Any number of rules for each field
  • Return together errors for all fields
  • Excludes all fields that do not have validation rules described
  • Has possibility to validate complex hierarchical structures
  • Easy to describe and understand rules
  • Returns understandable error codes(not error messages)
  • Easy to add own rules
  • Rules are be able to change results output ("trim", "nested_object", for example)
  • Multipurpose (user input validation, configs validation, contracts programming etc)

Synopsis

Common usage:

let rules = ["first_name": "required",
             "age": "adult_age",
             "last_name": ["required"],
             "middle_name": [["required": []]],
             "salary": ["required": []]
            ]

var validator = LIVR.validator(validationRules: rules, isAutoTrim: true)

var output: Validator.Output?
do {
    output = try validator.validate(data: inputJson)
} catch { print((error as? ValidatingError)?.description) }

if let output = output {
  save(userData)
} else {
    print("Errors" + validator.errors.description)
}

You can use modifiers separately or can combine them with validation:

let rules = ["email": ["required", "trim", "email", "to_lc"]]
var validator = LIVR.validator(validationRules: rules, isAutoTrim: true)

Feel free to register your own rules:

You can use aliases(preferable, syntax covered by the specification) for a lot of cases, through a Dictionary of rules:

let rules = ["age": ["required", "adult_age"],
             "age-2": ["adult_age_with_custom_error"]
            ]
var validator = LIVR.validator(validationRules: rules)

let aliasingRules = [
                      [
                          "name": "adult_age",
                          "rules": ["positive_integer", ["min_number": 18]]
                      ],
                      [
                          "name": "adult_age_with_custom_error",
                          "rules": ["positive_integer", ["min_number": 18]],
                          "error": "WRONG_AGE"
                      ]
                    ]

validator.registerRule(aliases: aliasingRules)

or passing alias name, rules and errorCode:

let rules = ["password": ["required", "strong_password"]]
var validator = LIVR.validator(validationRules: rules)

validator.registerRule(alias: "strong_password", rules: ["min_length": 6], errorCode: "WEAK_PASSWORD")

Installation

Tested with pod --version: 1.0.2

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
    pod 'Livr'
end

Replace YOUR_TARGET_NAME and then, in the Podfile directory, type:

$ pod install

Contributing and future improvements

Feel free to fork, open feature requests, issues and PRs. Known issues and mapped activities are in this repo issues. A contribuiting.md will be addressed soon.

License

Livr is released under the MIT license. See LICENSE for details.

Special thanks/credits

  • @jotafeldmann for helping me come up with understanding and maping the challenges of creating this framweork.

swift-validator-livr's People

Contributors

marinofelipe avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.