class Schemas::UI::Schema

Public Class Methods

define(&block) click to toggle source
# File lib/schemas/ui/schema.rb, line 8
def self.define(&block)
  schema = new
  schema.instance_eval(&block)
  schema
end
new() click to toggle source
# File lib/schemas/ui/schema.rb, line 4
def initialize
  @params = []
end

Public Instance Methods

errors(input) click to toggle source
# File lib/schemas/ui/schema.rb, line 19
def errors(input)
  merged_named_fields.errors(input)
end
optional(name, *args, default: raise("optional requires a default: param, " + "either a value or a lambda"), **kwargs) click to toggle source
# File lib/schemas/ui/schema.rb, line 29
def optional(name, *args,
             default: raise("optional requires a default: param, " +
                            "either a value or a lambda"), **kwargs)
  type_with_validator = construct_field_from_params(*args, **kwargs)
  optional_field = Fields::FieldWithDefault.new(type_with_validator,
                                                default)
  @params << Fields::NamedFieldFromHash.new(name, optional_field)
end
parse(input) click to toggle source
# File lib/schemas/ui/schema.rb, line 14
def parse(input)
  raise CannotParseWhenThereAreErrors if errors(input).any?
  merged_named_fields.parse(input)
end
required(name, *args) click to toggle source
# File lib/schemas/ui/schema.rb, line 23
def required(name, *args)
  type_with_validator = construct_field_from_params(*args)
  required_field = Fields::RequiredField.new(type_with_validator)
  @params << Fields::NamedFieldFromHash.new(name, required_field)
end

Private Instance Methods

construct_field_from_params(type: Fields::PassThroughField.new, required: true, validator: nil, validators: []) click to toggle source
# File lib/schemas/ui/schema.rb, line 40
def construct_field_from_params(type: Fields::PassThroughField.new,
                                required: true, validator: nil,
                                validators: [])
  validator_chain = Validators::ValidatorChain.new(Array(validator) +
                                                   validators)
  Types::TypeWithValidator.new(type, validator_chain)
end
merged_named_fields() click to toggle source
# File lib/schemas/ui/schema.rb, line 48
def merged_named_fields
  Fields::MergedNamedFields.new(@params)
end