Module: RSchema

Defined in:
lib/rschema.rb,
lib/rschema/dsl.rb,
lib/rschema/error.rb,
lib/rschema/rails.rb,
lib/rschema/result.rb,
lib/rschema/options.rb,
lib/rschema/version.rb,
lib/rschema/schemas/set.rb,
lib/rschema/schemas/sum.rb,
lib/rschema/coercers/any.rb,
lib/rschema/schemas/enum.rb,
lib/rschema/schemas/type.rb,
lib/rschema/coercers/date.rb,
lib/rschema/coercers/time.rb,
lib/rschema/schemas/maybe.rb,
lib/rschema/coercers/chain.rb,
lib/rschema/coercers/float.rb,
lib/rschema/coercers/symbol.rb,
lib/rschema/schemas/boolean.rb,
lib/rschema/schemas/coercer.rb,
lib/rschema/coercers/boolean.rb,
lib/rschema/coercers/integer.rb,
lib/rschema/coercion_wrapper.rb,
lib/rschema/schemas/anything.rb,
lib/rschema/schemas/pipeline.rb,
lib/rschema/schemas/predicate.rb,
lib/rschema/schemas/fixed_hash.rb,
lib/rschema/schemas/convenience.rb,
lib/rschema/schemas/variable_hash.rb,
lib/rschema/coercers/nil_empty_strings.rb,
lib/rschema/schemas/fixed_length_array.rb,
lib/rschema/coercion_wrapper/rack_params.rb,
lib/rschema/schemas/variable_length_array.rb,
lib/rschema/coercers/fixed_hash/symbolize_keys.rb,
lib/rschema/coercers/fixed_hash/default_arrays_to_empty.rb,
lib/rschema/coercers/fixed_hash/default_booleans_to_false.rb,
lib/rschema/coercers/fixed_hash/remove_extraneous_attributes.rb

Overview

Schema-based validation and coercion

Defined Under Namespace

Modules: Coercers, DSL, Rails, Schemas Classes: CoercionWrapper, DefaultDSL, Error, Invalid, Options, Result

Constant Summary collapse

VERSION =
'3.2.0'

Class Method Summary collapse

Class Method Details

.default_dslObject

Returns The default DSL object.

Returns:

  • The default DSL object.

See Also:



119
120
121
# File 'lib/rschema.rb', line 119

def self.default_dsl
  @default_dsl ||= DefaultDSL.new
end

.define(dsl = nil) { ... } ⇒ Schemas::Convenience

Creates a schema object using a DSL

Examples:

A typical fixed hash schema

schema = RSchema.define do
  fixed_hash(
    name: _String,
    optional(:age) => _Integer,
  )
end
schema.valid?({ name: "Tom" }) #=> true
schema.valid?({ name: "Dane", age: 55 }) #=> true

Parameters:

  • dsl (Object) (defaults to: nil)

    An optional DSL object to run the block with. Uses default_dsl if nil.

Yields:

  • Invokes the given block with access to the methods on ‘dsl`.

Returns:



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

def self.define(dsl = nil, &block)
  schema = dsl_eval(dsl, &block)
  Schemas::Convenience.wrap(schema)
end

.define_hash { ... } ⇒ Schemas::Convenience

A convenience method for creating RSchema::Schemas::FixedHash schemas

This method is a shorter way to write:

RSchema.define do
  fixed_hash(...)
end

Examples:

A typical fixed hash schema

person_schema = RSchema.define_hash {{
  name: _String,
  age: _Integer,
}}

Yields:

  • Invokes the given block with access to the methods on ‘dsl`.

Yield Returns:

Returns:



81
82
83
84
85
# File 'lib/rschema.rb', line 81

def self.define_hash(&block)
  Schemas::Convenience.wrap(
    default_dsl.fixed_hash(dsl_eval(&block)),
  )
end

.define_predicate(name = nil) { ... } ⇒ Schemas::Convenience

A convenience method for creating RSchema::Schemas::Predicate schemas.

This method is a shorter way to write:

RSchema.define do
  predicate(name) { ... }
end

Examples:

A predicate schema that only allows ‘odd?` objects.

odd_schema = RSchema.define_predicate('odd') do |x|
  x.odd?
end

Parameters:

  • name (String) (defaults to: nil)

    An optional name for the predicate schema. This serves no purpose other than to provide useful debugging information, or perhaps some metadata.

Yields:

  • Values being validated are yielded to the given block. The return value of the block indicates whether the value is valid or not.

Yield Returns:

  • (Boolean)

    Truthy if the value is valid, otherwise falsey.

Returns:

See Also:



109
110
111
112
113
# File 'lib/rschema.rb', line 109

def self.define_predicate(name = nil, &block)
  Schemas::Convenience.wrap(
    default_dsl.predicate(name, &block),
  )
end

.dsl_eval(dsl = nil) { ... } ⇒ Object

Runs a block using a DSL.

Examples:

Creating a typical fixed hash schema

person_schema = RSchema.dsl_eval do
  fixed_hash(
    name: _String,
    age: _Integer,
  )
end

Parameters:

  • dsl (Object) (defaults to: nil)

    An optional DSL object to run the block with. Uses default_dsl if nil.

Yields:

  • Invokes the given block with access to the methods on ‘dsl`.

Returns:

  • The return value of the given block (usually some kind of schema object)



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rschema.rb', line 48

def self.dsl_eval(dsl = nil, &block)
  if block.nil?
    raise ArgumentError, 'Must provide a block for the RSchema DSL'
  end

  Docile::Execution.exec_in_proxy_context(
    dsl || default_dsl,
    Docile::FallbackContextProxy,
    &block
  )
end