Parent

Class/Module Index [+]

Quicksearch

Fog::Schema::DataValidator

This validates a data object against a Ruby based schema to see if they match.

The schema and validation are very simple and probably not suitable for some cases.

The following classes can be used to check for special behaviour

All the "nullable" objects will pass if the value is of the class or if it is nil. This allows you to match APIs that may include keys when the value is not available in some cases but will always be a String. Such as an password that is only displayed on the reset action.

The keys for "nullable" resources should always be present but original matcher had a bug that allowed these to also appear to work as optional keys/values.

If you need the original behaviour, data with a missing key is still valid, then you may pass the :allow_optional_rules option to the validate method.

That is not recommended because you are describing a schema with optional keys in a format that does not support it.

Setting :allow_extra_keys as true allows the data to contain keys not declared by the schema and still pass. This is useful if new attributes appear in the API in a backwards compatible manner and can be ignored.

This is the behaviour you would have seen with strict being false in the original test helper.

@example Schema example

{
  "id" => String,
  "ram" => Integer,
  "disks" => [
    "size" => Float
  ],
  "dns_name" => Fog::Nullable::String,
  "active" => Fog::Boolean,
  "created" => DateTime
}

Public Class Methods

new() click to toggle source
# File lib/fog/schema/data_validator.rb, line 65
def initialize
  @message = nil
end

Public Instance Methods

message() click to toggle source

This returns the last message set by the validator

@return [String]

# File lib/fog/schema/data_validator.rb, line 95
def message
  @message
end
validate(data, schema, options = {}) click to toggle source

Checks if the data structure matches the schema passed in and returns true if it fits.

@param [Object] data Hash or Array to check @param [Object] schema Schema pattern to check against @param [Boolean] options @option options [Boolean] :allow_extra_keys

If +true+ does not fail if extra keys are in the data
that are not in the schema.

@option options [Boolean] :allow_optional_rules

If +true+ does not fail if extra keys are in the schema
that do not match the data. Not recommended!

@return [Boolean] Did the data fit the schema?

# File lib/fog/schema/data_validator.rb, line 83
def validate(data, schema, options = {})
  valid = validate_value(schema, data, options)

  unless valid
    @message = "#{data.inspect} does not match #{schema.inspect}"
  end
  valid
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.