class RSchema::Schemas::Convenience

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?}.

Public Class Methods

new(underlying_schema) click to toggle source
Calls superclass method
# File lib/rschema/schemas/convenience.rb, line 22
def initialize(underlying_schema)
  super
end
unwrap(schema) click to toggle source

Removes any {Convenience} wrappers from a schema

@param schema [schema] The schema to unwrap @return [schema] The underlying schema, with all {Convenience} wrappers

removed
# File lib/rschema/schemas/convenience.rb, line 126
def self.unwrap(schema)
  schema = schema.underlying_schema while schema.is_a?(self)
  schema
end
wrap(schema) click to toggle source

Wraps the given schema in a {Convenience}, if it isn't already wrapped.

@param schema [schema] The schema to wrap @return {Convenience}

# File lib/rschema/schemas/convenience.rb, line 112
def self.wrap(schema)
  if schema.is_a?(self)
    schema
  else
    new(schema)
  end
end

Public Instance Methods

error_for(value, options = Options.default) click to toggle source

Returns the validation error for the given value

@param value [Object] The value to validate @param options [RSchema::Options]

@return The error object if `value` is invalid, otherwise `nil`.

@see Result#error

# File lib/rschema/schemas/convenience.rb, line 56
def error_for(value, options = Options.default)
  result = underlying_schema.call(value, options)
  if result.valid?
    nil
  else
    result.error
  end
end
invalid?(value) click to toggle source

The opposite of {#valid?}

@see valid?

# File lib/rschema/schemas/convenience.rb, line 102
def invalid?(value)
  !valid?(value)
end
underlying_schema() click to toggle source

@return [schema] the underlying schema object

# File lib/rschema/schemas/convenience.rb, line 27
def underlying_schema
  __getobj__
end
valid?(value) click to toggle source

Checks whether a value is valid or not

@param value [Object] The value to validate @return [Boolean] `true` if the value is valid, otherwise `false`

# File lib/rschema/schemas/convenience.rb, line 92
def valid?(value)
  result = underlying_schema.call(value, Options.fail_fast)
  result.valid?
end
validate(value, options = Options.default) click to toggle source

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.

@param value [Object] The value to validate @param options [RSchema::Options]

@return [RSchema::Result]

# File lib/rschema/schemas/convenience.rb, line 42
def validate(value, options = Options.default)
  call(value, options)
end
validate!(value, options = Options.default) click to toggle source

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

@param value [Object] The value to validate @param options [RSchema::Options]

@raise [RSchema::Invalid] If the value is not valid @return [Object] The validated value

@see Result#value

# File lib/rschema/schemas/convenience.rb, line 77
def validate!(value, options = Options.default)
  result = underlying_schema.call(value, options)
  if result.valid?
    result.value
  else
    raise RSchema::Invalid, result.error
  end
end
with_wrapped_subschemas(wrapper) click to toggle source

@!visibility private

# File lib/rschema/schemas/convenience.rb, line 132
def with_wrapped_subschemas(wrapper)
  self.class.new(wrapper.wrap(underlying_schema))
end