Giter VIP home page Giter VIP logo

validates_subset's Introduction

validates_subset

Build Status

Subset validator for Rails

Purpose

Validate that an attribute is a subset of another. If an attribute saved to the database (or simply kept in memory) needs to be a subset of another, this is the way to go.

Usage

With ActiveRecord
class Foo < ActiveRecord::Base
  attr_accessor :bar

  # validate that the attribute :bar is a subset of the set [2, 4, 5]
  validates_subset :bar, [2, 4, 5]
end

# Example usage:
foo = Foo.new(bar: [2])
foo.valid?
# => true

foo = Foo.new(bar: [7])
foo.valid?
# => false

foo = Foo.new(bar: 'banana')
foo.valid?
# => false
With ActiveModel
class Bar
  include ActiveModel

  attr_accessor :foo

  # validate that the attribute :foo is a subset of the set ['a', 'b', 'c']
  validates_subset :foo, ['a', 'b', 'c']
end

# Example usage:
bar = Bar.new(foo: ['a'])
bar.valid?
# => true

bar = Bar.new(foo: ['q'])
bar.valid?
# => false

bar = Bar.new(foo: 123)
bar.valid?
# => false
With validates syntax
class Banana < ActiveRecord::Base

  attr_accessor :peel

  # validate that the attribute :peel is a subset of the set ['yellow', 'green']
  validates :peel, subset: ['yellow', 'green']
end

# Example usage:
banana = Banana.new(peel: ['yellow'])
banana.valid?
# => true

banana = Banana.new(peel: ['brown']) # Who wants a brown peel? No one.
banana.valid?
# => false

banana = Banana.new(peel: 123)
banana.valid?
# => false
With multiple modifiers
class Foo < ActiveRecord::Base
  attr_accessor :baz

  # validate that attribute :baz is a subset of [1, 2, 3] with a custom error message
  #   only if :conditional_method evaluates to true
  validates_subset :baz, [1, 2, 3],
                  message: 'Baz is not a subset of [1, 2, 3], make it so!',
                  if: :conditional_method

  def conditional_method
    # some kind of logic that is important to pass
  end
end

# Example usage:
foo = Foo.new(baz: [1])

foo.valid?
# => true

foo = Foo.new(baz: [99999])

# When the conditional method is true
foo.conditional_method
# => true
foo.valid?
# => false

# When the conditional method is false
foo.conditional_method
# => false
foo.valid?
# => true

validates_subset's People

Contributors

davidelbe avatar yez avatar

Watchers

 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.