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.


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

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:


126
127
128
129
# 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) ⇒ Convenience

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

Parameters:

  • schema (schema)

    The schema to wrap

Returns:


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

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:


56
57
58
59
60
61
62
63
# 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) ⇒ Boolean

The opposite of #valid?

Returns:

See Also:


102
103
104
# File 'lib/rschema/schemas/convenience.rb', line 102

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

#underlying_schemaschema

Returns the underlying schema object.

Returns:

  • (schema)

    the underlying schema object


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

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


92
93
94
95
# 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) ⇒ 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:


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

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:


77
78
79
80
81
82
83
84
# 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