Giter VIP home page Giter VIP logo

vanilla_validator's Introduction

๐Ÿšง Project Under Development ๐Ÿšง

Ruby Gem

VanillaValidator

VanillaValidator is a lightweight and clean solution for implementing validation in Ruby. It inspired from Laravel Validator and allows you to separate validations from the model layer in your Rails applications. With this gem, you can easily define and enforce validation rules for input data, ensuring the consistency and cleanliness of your application's data.

Installation:

To use VanillaValidator in your Ruby project, execute the following command to install it:

$ bundle add vanilla_validator

Basic Usage

There are two options to use VanillaValidator, in the first place you can invoke validate method directlly from VanillaValidator class and then result would be an object witch contains different methods to determine the result of validation.

params = {
  user: {
    email: '[email protected]'
  }
}

validation = VanillaValidator.validate(params, {
  'user.email' => 'required|email'
})

validation.success?
validation.failed?
validation.validated
validation.errors


# In Rails, you can access the validate method directly:
params.validate({
  'user.email' => 'require|email'
})

# If you want validation to stop at the first failure, use a bang sign (!) after 'validate':
params.validate!({
  'user.password' => 'require|min:16'
})

Rules Declaration

You have the flexibility to define validation rules either explicitly or implicitly, depending on your specific requirements. When taking the explicit approach, you must specify the exact attribute you wish to validate. To access nested attributes, you can employ a period (.) in the attribute path, as demonstrated below:

params = {
  user: {
    email: '[email protected]',
    preferences: {
      notifications: true
    }
  }
}

VanillaValidator.validate(params, {
  'user.email' => 'required|email',
  'user.preferences.notifications' => 'boolean'
})

In cases where your input consists of a collection of items, you can specify the attributes implicitly. This can be achieved by using a wildcard (*) in the attribute path, as shown in the following example:

params = {
  user: {
    addresses: [
      { city: 'San Francisco', state: 'CA' },
      { city: 'Los Angeles', state: 'CA' }
    ],
    orders: [
      { total: 50.0, status: 'shipped' },
      { total: 75.0, status: 'delivered' }
    ]
  }
}

validation = VanillaValidator.validate(params, {
  'user.addresses.*.state' => 'required|alpha|in:CA,NY',
  'user.orders.*.total'    => 'required|numeric|min:0',
  'user.orders.*.status'   => 'required|alpha|in:pending,shipped,delivered'
})

Available Validation Rules

After

The validated field must have a value that is after a specified date.

'start_date' => 'required|date|after:tomorrow'
After Or Equal
Before
Before Or Equal
Boolean
Date
Email
Eq
Falsy
Gte
In
Like
Max
Min
Numeric
Required
Required If
Url
Custom Validation Rules:

Contributions:

Contributions to this gem are welcome. Please read the Contribution Guidelines before submitting your contributions.

Reporting Issues:

If you encounter any issues or have suggestions for improvements, please open an issue on the GitHub repository.

License:

This gem is available under the MIT License.

vanilla_validator's People

Contributors

leurias avatar

Stargazers

 avatar  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.