Class: RSchema::Schemas::Convenience

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/rschema/schemas/convenience.rb

Overview

A wrapper that provides convenience methods for schema objects.

Because this class inherits from ‘SimpleDelegator`, convenience wrappers behave like their underlying schemas. That is, you can call methods on the underlying schema object through the convenience wrapper.

Schema objects only need to implement the ‘call` method to validate values. This small interface is simple for schema classes to implement, but not very descriptive when actually using the schema objects. So, to make schema objects nicer to use, this class provides a variety of more-descriptive methods like #validate, #validate!, #valid?, and #invalid?.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(underlying_schema) ⇒ Convenience

Returns a new instance of Convenience.



21
22
23
# File 'lib/rschema/schemas/convenience.rb', line 21

def initialize(underlying_schema)
  super
end

Class Method Details

.unwrap(schema) ⇒ schema

Removes any RSchema::Schemas::Convenience wrappers from a schema

Parameters:

  • schema (schema)

    The schema to unwrap

Returns:



124
125
126
127
128
129
# File 'lib/rschema/schemas/convenience.rb', line 124

def self.unwrap(schema)
  while schema.is_a?(self)
    schema = schema.underlying_schema
  end
  schema
end

.wrap(schema) ⇒ Convenience

Wraps the given schema in a RSchema::Schemas::Convenience, if it isn’t already wrapped.

Parameters:

  • schema (schema)

    The schema to wrap

Returns:



110
111
112
113
114
115
116
# File 'lib/rschema/schemas/convenience.rb', line 110

def self.wrap(schema)
  if schema.is_a?(self)
    schema
  else
    new(schema)
  end
end

Instance Method Details

#error_for(value, options = Options.default) ⇒ Object

Returns the validation error for the given value

Parameters:

  • value (Object)

    The value to validate

  • options (RSchema::Options) (defaults to: Options.default)

Returns:

  • The error object if ‘value` is invalid, otherwise `nil`.

See Also:



55
56
57
58
59
60
61
62
# File 'lib/rschema/schemas/convenience.rb', line 55

def error_for(value, options=Options.default)
  result = underlying_schema.call(value, options)
  if result.valid?
    nil
  else
    result.error
  end
end

#invalid?(value) ⇒ Boolean

The opposite of #valid?

Returns:

See Also:



100
101
102
# File 'lib/rschema/schemas/convenience.rb', line 100

def invalid?(value)
  not valid?(value)
end

#underlying_schemaschema

Returns the underlying schema object.

Returns:

  • (schema)

    the underlying schema object



26
27
28
# File 'lib/rschema/schemas/convenience.rb', line 26

def underlying_schema
  __getobj__
end

#valid?(value) ⇒ Boolean

Checks whether a value is valid or not

Parameters:

  • value (Object)

    The value to validate

Returns:

  • (Boolean)

    ‘true` if the value is valid, otherwise `false`



90
91
92
93
# File 'lib/rschema/schemas/convenience.rb', line 90

def valid?(value)
  result = underlying_schema.call(value, Options.fail_fast)
  result.valid?
end

#validate(value, options = Options.default) ⇒ RSchema::Result

Applies the schema to a value

This is that same as the ‘call` method available on all schema objects, except that the `options` param is optional.

Parameters:

  • value (Object)

    The value to validate

  • options (RSchema::Options) (defaults to: Options.default)

Returns:



41
42
43
# File 'lib/rschema/schemas/convenience.rb', line 41

def validate(value, options=Options.default)
  call(value, options)
end

#validate!(value, options = Options.default) ⇒ Object

Applies the schema to a value, raising an exception if the value is invalid

Parameters:

  • value (Object)

    The value to validate

  • options (RSchema::Options) (defaults to: Options.default)

Returns:

  • (Object)

    The validated value

Raises:

See Also:



75
76
77
78
79
80
81
82
# File 'lib/rschema/schemas/convenience.rb', line 75

def validate!(value, options=Options.default)
  result = underlying_schema.call(value, options)
  if result.valid?
    result.value
  else
    raise RSchema::Invalid.new(result.error)
  end
end