Module: Validate

Defined in:
lib/validate.rb,
lib/validate/ast.rb,
lib/validate/scope.rb,
lib/validate/errors.rb,
lib/validate/compare.rb,
lib/validate/helpers.rb,
lib/validate/version.rb,
lib/validate/arguments.rb,
lib/validate/assertions.rb,
lib/validate/constraint.rb,
lib/validate/validators.rb,
lib/validate/constraints.rb,
lib/validate/validators/dsl.rb,
lib/validate/constraints/validation_context.rb

Overview

typed: strict frozen_string_literal: true

Defined Under Namespace

Modules: AST, Arguments, Assertions, ClassMethods, Compare, Constraints, Error, Helpers, Validators Classes: Constraint, Scope

Constant Summary collapse

VERSION =
'1.1.0'

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Hook to allow for inclusion in class or module



71
72
73
# File 'lib/validate.rb', line 71

def self.included(base)
  base.extend(ClassMethods)
end

.validate(object, as: object.class) ⇒ Array<Constraint::Violation>

Validate an object and get constraint violations list back

Parameters:

  • object (Object)

    object to validate

  • as (Symbol, Class) (defaults to: object.class)

    (object.class) validator to use, defaults to object’s class

Returns:



53
54
55
56
57
58
59
# File 'lib/validate.rb', line 53

def self.validate(object, as: object.class)
  violations = []
  Scope.current
       .validator(as)
       .validate(Constraints::ValidationContext.root(object, violations))
  violations.freeze
end

.validator?(name) ⇒ Boolean

Check if a given validator exists

Parameters:

  • name (Symbol, Class)

    validator to check

Returns:

  • (Boolean)

    ‘true` if validator is present, `else` otherwise



66
67
68
# File 'lib/validate.rb', line 66

def self.validator?(name)
  Scope.current.validator?(name)
end