Sha256: 5fbc13442787e553b5aa38b2a16e70aaf514eae819d4b6647cae75fa639f4397

Contents?: true

Size: 464 Bytes

Versions: 1

Compression:

Stored size: 464 Bytes

Contents

class BooleanFormatValidator < ActiveModel::EachValidator # :nodoc:
  # inspired by https://github.com/MrJoy/to_bool/blob/5c9ed38e47c638725e33530ea1a8aec96281af20/lib/to_bool.rb#L23
  FALSE_BOOLEAN_REGEX = /^(false|f|no|n|0)$/i
  TRUE_BOOLEAN_REGEX = /^(true|t|yes|y|1)$/i

  def validate_each(record, attribute, value)
    return if value =~ FALSE_BOOLEAN_REGEX || value =~ TRUE_BOOLEAN_REGEX

    record.errors.add(attribute, 'is not a Boolean format')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv_row_model-1.0.0.beta2 lib/csv_row_model/validators/boolean_format_validator.rb