Class: Schemacop::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/schemacop/schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Schema

Create a new Schema

For detailed usage, please refer to README.md in the root of this project.

Parameters:

  • args (Array)

    An array of arguments to apply to the root node of the Schema.

  • block

    A block with further Schema specification.

Raises:



12
13
14
15
16
# File 'lib/schemacop/schema.rb', line 12

def initialize(*args, &block)
  @root = HashValidator.new do
    req :root, *args, &block
  end
end

Instance Method Details

#invalid?(data) ⇒ Boolean

Query data validity

Parameters:

  • data

    The data to validate.

Returns:

  • (Boolean)

    True if data is invalid, false otherwise.



30
31
32
# File 'lib/schemacop/schema.rb', line 30

def invalid?(data)
  !valid?(data)
end

#valid?(data) ⇒ Boolean

Query data validity

Parameters:

  • data

    The data to validate.

Returns:

  • (Boolean)

    True if the data is valid, false otherwise.



22
23
24
# File 'lib/schemacop/schema.rb', line 22

def valid?(data)
  validate(data).valid?
end

#validate(data) ⇒ Schemacop::Collector

Validate data for the defined Schema

Parameters:

  • data

    The data to validate.

Returns:



39
40
41
42
43
# File 'lib/schemacop/schema.rb', line 39

def validate(data)
  collector = Collector.new
  @root.fields[:root].validate({ root: data }, collector)
  return collector
end

#validate!(data) ⇒ Object

Validate data for the defined Schema

Parameters:

  • data

    The data to validate.

Returns:

  • nil

Raises:



51
52
53
54
55
56
57
58
59
# File 'lib/schemacop/schema.rb', line 51

def validate!(data)
  collector = validate(data)

  unless collector.valid?
    fail Exceptions::ValidationError, collector.exception_message
  end

  return nil
end