Module: Pedicel::Validator::InstanceMethods

Included in:
Token, TokenData
Defined in:
lib/pedicel/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



148
149
150
# File 'lib/pedicel/validator.rb', line 148

def output
  @output
end

Instance Method Details

#errorsObject



166
167
168
169
170
# File 'lib/pedicel/validator.rb', line 166

def errors
  valid? unless @validation

  @validation.errors
end

#errors_formatted(node = [errors]) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/pedicel/validator.rb', line 172

def errors_formatted(node = [errors])
  node.pop.flat_map do |key, value|
    if value.is_a?(Array)
      value.map { |error| "#{(node + [key]).join('.')} #{error}" }
    else
      errors_formatted(node + [key, value])
    end
  end
end

#valid?Boolean

Returns:

  • (Boolean)


160
161
162
163
164
# File 'lib/pedicel/validator.rb', line 160

def valid?
  validate
rescue Error
  false
end

#validateObject

Raises:



150
151
152
153
154
155
156
157
158
# File 'lib/pedicel/validator.rb', line 150

def validate
  @validation ||= @schema.call(@input)

  @output = @validation.output

  return true if @validation.success?

  raise Error, "validation error: #{@validation.errors.keys.join(', ')}"
end