Socket
Book a DemoInstallSign in
Socket

enum_attributes_validation

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enum_attributes_validation

0.1.7
bundlerRubygems
Version published
Maintainers
1
Created
Source

EnumAttributesValidation

Enum Attributes Validation provides validation functionality for enum attributes in models. It validates inclusion for the specified enum keys. If values are nil the record is saved.

Installation

Add to Gemfile:

gem 'enum_attributes_validation'

And then execute:

$ bundle

Or install it yourself as:

$ gem install enum_attributes_validation

Usage

Use built in method to specify which enum attributes you want to validate.

class Dog < ApplicationRecord
  enum gender: { male: 0, female: 1 }
  enum hair_color: { brown: 0, black: 1, blonde: 2 }

  validate_enum_attributes :gender, :hair_color
end

dog = Dog.new(hair_color: :brown, gender: :female)
dog.save
# => <Dog gender: "female", hair_color: "brown" >

dog = Dog.new
dog.save
# => <Dog gender: nil, hair_color: nil >

dog = Dog.new(hair_color: nil, gender: nil)
dog.save
# => <Dog gender: nil, hair_color: nil >

Or use the singular method.

class Dog < ApplicationRecord
  enum gender: { male: 0, female: 1 }
  enum hair_color: { brown: 0, black: 1, blonde: 2 }

  validate_enum_attribute :gender
end

Custom error messages

To show a custom error message:

class Dog < ApplicationRecord
  enum gender: { male: 0, female: 1 }

  validate_enum_attribute :gender, message: "Some string msg..."
end

Translations

To use translations you need to update your locale file as follows

en:
  activerecord:
    errors:
      models:
        #your_model_name#:
          attributes:
            #your_attribute_name#::
              invalid_enum: "can't be %{value}. Valid values are: %{valid_values}"

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/CristiRazvi/enum_attributes_validation.

License

MIT License.

FAQs

Package last updated on 11 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.