Module: RSchema

Defined in:
lib/rschema.rb,
lib/rschema/dsl.rb,
lib/rschema/error.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/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_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, Schemas Classes: CoercionWrapper, DefaultDSL, Error, Invalid, Options, Result

Constant Summary collapse

VERSION =
'3.0.1.pre5'

Class Method Summary collapse

Class Method Details

.default_dslObject

Returns The default DSL object.

See Also:


112
113
114
# File 'lib/rschema.rb', line 112

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

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

Creates a schema object using a DSL

Yields:

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


24
25
26
27
# File 'lib/rschema.rb', line 24

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:


74
75
76
77
78
# File 'lib/rschema.rb', line 74

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

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.

See Also:


102
103
104
105
106
# File 'lib/rschema.rb', line 102

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

Yields:

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


46
47
48
49
50
51
52
# File 'lib/rschema.rb', line 46

def self.dsl_eval(dsl = nil, &block)
  Docile::Execution.exec_in_proxy_context(
    dsl || default_dsl,
    Docile::FallbackContextProxy,
    &block
  )
end