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.



285
286
287
# File 'lib/pedicel/validator.rb', line 285

def output
  @output
end

Instance Method Details

#errorsObject



303
304
305
306
307
# File 'lib/pedicel/validator.rb', line 303

def errors
  valid? unless @validation

  @validation.errors.to_h.sort
end

#errors_formatted(node = [errors]) ⇒ Object



309
310
311
312
313
314
315
316
317
# File 'lib/pedicel/validator.rb', line 309

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)


297
298
299
300
301
# File 'lib/pedicel/validator.rb', line 297

def valid?
  validate
rescue Error
  false
end

#validateObject

Raises:



287
288
289
290
291
292
293
294
295
# File 'lib/pedicel/validator.rb', line 287

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

  @output = @validation.to_h

  return true if @validation.success?

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