Class: Fog::Schema::DataValidator
- Inherits:
-
Object
- Object
- Fog::Schema::DataValidator
- Defined in:
- lib/fog/schema/data_validator.rb
Overview
This validates a data object against a Ruby based schema to see if they match.
-
An object matches the schema if == or === returns
true
-
Hashes match if all the key’s values match the classes given in the schema as well. This can be configured in the options
-
Arrays match when every element in the data matches the case given in the schema.
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
-
Fog::Boolean - value may be
true
orfalse
-
Fog::Nullable::Boolean - value may be
true
,false
ornil
-
Fog::Nullable::Integer - value may be an Integer or
nil
-
Fog::Nullable::String
-
Fog::Nullable::Time
-
Fog::Nullable::Float
-
Fog::Nullable::Hash
-
Fog::Nullable::Array
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.
Instance Attribute Summary collapse
-
#message ⇒ String
readonly
This returns the last message set by the validator.
Instance Method Summary collapse
-
#initialize ⇒ DataValidator
constructor
A new instance of DataValidator.
-
#validate(data, schema, options = {}) ⇒ Boolean
Checks if the data structure matches the schema passed in and returns
true
if it fits.
Constructor Details
#initialize ⇒ DataValidator
Returns a new instance of DataValidator.
69 70 71 |
# File 'lib/fog/schema/data_validator.rb', line 69 def initialize @message = nil end |
Instance Attribute Details
#message ⇒ String (readonly)
This returns the last message set by the validator
67 68 69 |
# File 'lib/fog/schema/data_validator.rb', line 67 def @message end |
Instance Method Details
#validate(data, schema, options = {}) ⇒ Boolean
Checks if the data structure matches the schema passed in and returns true
if it fits.
87 88 89 90 91 92 93 94 |
# File 'lib/fog/schema/data_validator.rb', line 87 def validate(data, schema, = {}) valid = validate_value(schema, data, ) unless valid @message = "#{data.inspect} does not match #{schema.inspect}" end valid end |