Class: Appfuel::Validation::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/appfuel/validation/validator.rb

Overview

Any validator that is run for an action or command will be created with this class. It service to abstract away Dry::Validation, the library we use from the system that runs all the validators. This allows us to use validators along side things like validator pipes without the system having to care.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, schema, fail_fast: false) ⇒ Validator

Parameters:

  • name (String)

    used to register this validator in a container

  • schema (Dry::Validation::Schema)

    the actual validator

  • fail_fast (Bool) (defaults to: false)

    tell the system how to fail



15
16
17
18
19
20
21
22
23
# File 'lib/appfuel/validation/validator.rb', line 15

def initialize(name, schema, fail_fast: false)
  @name = name
  unless schema.respond_to?(:call)
    fail ArgumentError, "schema must implement :call"
  end
  @schema = schema

  fail_fast == true ? enable_fail_fast : disable_fail_fast
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/appfuel/validation/validator.rb', line 9

def name
  @name
end

#schemaObject (readonly)

Returns the value of attribute schema.



9
10
11
# File 'lib/appfuel/validation/validator.rb', line 9

def schema
  @schema
end

Instance Method Details

#call(inputs) ⇒ Dry::Validation::Result

Delegate’s to the Dry::Validation schema to validate the inputs

Parameters:

  • inputs (Hash)

Returns:

  • (Dry::Validation::Result)


49
50
51
# File 'lib/appfuel/validation/validator.rb', line 49

def call(inputs)
  schema.call(inputs)
end

#disable_fail_fastBool

Ensures the system will continue validating when errors exist

Returns:

  • (Bool)


35
36
37
# File 'lib/appfuel/validation/validator.rb', line 35

def disable_fail_fast
  @fail_fast = false
end

#enable_fail_fastBool

Ensures the system will stop validating when this validator fails

Returns:

  • (Bool)


28
29
30
# File 'lib/appfuel/validation/validator.rb', line 28

def enable_fail_fast
  @fail_fast = true
end

#fail_fast?Bool

Returns:

  • (Bool)


40
41
42
# File 'lib/appfuel/validation/validator.rb', line 40

def fail_fast?
  @fail_fast
end

#pipe?Boolean

Tell this system this is not a validation pipe

Returns:

  • (Boolean)


54
55
56
# File 'lib/appfuel/validation/validator.rb', line 54

def pipe?
  false
end