Git Product home page Git Product logo

ruby-livr's Introduction

Build Status

LIVR Validator

LIVR.Validator - Lightweight validator supporting Language Independent Validation Rules Specification (LIVR) Implements latest '2.0 specification'

SYNOPSIS

Common usage:

gem 'ruby-livr'
require 'livr'

validator = LIVR::Validator.new({
    'name'      => 'required',
    'email'     => [ 'required', 'email' ],
    'gender'    => { 'one_of' => ['male', 'female'] },
    'phone'     => { 'max_length' => 10 },
    'password'  => [ 'required', { 'min_length' => 10} ],
    'password2' => { 'equal_to_field' => 'password' }
});

valid_data = validator.validate(user_data)

if valid_data
    save_user(validData)
else
    handle_errors(validator.get_errors)
end

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

validator = LIVR::Validator.new({
    'email' => [ 'required', 'trim', 'email', 'to_lc' ]
});

Feel free to register your own rules:

You can use aliases (prefferable, syntax covered by the specification) for a lot of cases:

validator = LIVR::Validator.new({
    'password' => ['required', 'strong_password']
});

validator.register_aliased_rule({
    'name' => 'strong_password',
    'rules' => { 'min_length' => 6},
    'error' => 'WEAK_PASSWORD'
});

Or you can write more sophisticated rules directly:

validator = LIVR::Validator.new({
    'password' => ['required', 'strong_password']
})

class StrongPassword
    def call(value, *other_args)
        # We already have "required" rule to check that the value is present
        return if value == nil || value == ""
        if value.length < 6
            return 'WEAK_PASSWORD'
        end
    end
end

validator.register_rules(strong_password: StrongPassword)

DESCRIPTION

This ruby gem is an implementation of LIVR Specification. 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 validatate complex hierarchical structures
  • Easy to describe and undersand 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)

INSTALL

as a gem

gem install ruby-livr

in rails

Add to Gemfile

gem "ruby-livr"

AUTHORS

Ruby implementation

Vitaliy Yanchuk (@fuksito)

Idea and specification

Viktor Turskyi (@koorchik)

ruby-livr's People

Contributors

juanbroz-lh avatar fuksito 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.