Skip to content

Builtin Validations

mosop edited this page Dec 9, 2016 · 11 revisions

Inclusion (any_of)

Tests if a parsed value is any of the specified values.

Can be applied to:

  • String Option
  • String Argument
class SomewhereWarm < Optarg::Model
  arg "island", any_of: %w(tahiti okinawa hawaii)
end

SomewhereWarm.parse %w(gotland) # => raises an error

Element Inclusion (any_item_of)

Tests if a parsed element value of array is any of the specified values.

Can be applied to:

  • String Array Option
  • String Array Argument
class EuropeanWallet < Optarg::Model
  arg_array "bill", any_item_of: %w(€5 €10 €20 €50 €100 €200 €500)
end

EuropeanWallet.parse %w(€10 $50 €100) # => raises an error

Minimum Length of Array (min)

Tests if the count of a parsed array is equal to or greater than the specified number.

Can be applied to:

  • String Array Option
  • String Array Argument
class TeamOfCanoePolo < Optarg::Model
  arg_array "member", min: 5
end

TeamOfCanoePolo.parse %w(freddie brian roger john) # => raises an error

Existence (required)

Tests if a value is set.

Can be applied to:

  • String Option
  • String Argument
class GoToProm < Optarg::Model
  string "--dress"
  string "--partner", required: true  
end

GoToProm.parse %w(--dress brand-new-tuxedo) # => raises an error
Clone this wiki locally