Class: DiscoveryV1::Validation::ValidateObject
- Inherits:
-
Object
- Object
- DiscoveryV1::Validation::ValidateObject
- Defined in:
- lib/discovery_v1/validation/validate_object.rb
Overview
Validate objects against a Google Discovery V1 API request object schema
Instance Attribute Summary collapse
-
#logger ⇒ Logger
readonly
The logger to use internally.
-
#rest_description ⇒ Google::Apis::DiscoveryV1::RestDescription
readonly
The Google Discovery V1 API description containing schemas to use for validation.
Instance Method Summary collapse
-
#call(schema_name:, object:) ⇒ void
Validate the object using the JSON schema named schema_name.
-
#initialize(rest_description:, logger: Logger.new(nil)) ⇒ ValidateObject
constructor
Create a new api object validator.
Constructor Details
#initialize(rest_description:, logger: Logger.new(nil)) ⇒ ValidateObject
Create a new api object validator
By default, a nil logger is used. This means that no messages are logged.
21 22 23 24 |
# File 'lib/discovery_v1/validation/validate_object.rb', line 21 def initialize(rest_description:, logger: Logger.new(nil)) @rest_description = rest_description @logger = logger end |
Instance Attribute Details
#logger ⇒ Logger (readonly)
The logger to use internally
Validation errors are logged at the error level. Other messages are logged at the debug level.
50 51 52 |
# File 'lib/discovery_v1/validation/validate_object.rb', line 50 def logger @logger end |
#rest_description ⇒ Google::Apis::DiscoveryV1::RestDescription (readonly)
The Google Discovery V1 API description containing schemas to use for validation
35 36 37 |
# File 'lib/discovery_v1/validation/validate_object.rb', line 35 def rest_description @rest_description end |
Instance Method Details
#call(schema_name:, object:) ⇒ void
This method returns an undefined value.
Validate the object using the JSON schema named schema_name
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/discovery_v1/validation/validate_object.rb', line 67 def call(schema_name:, object:) logger.debug { "Validating #{object} against #{schema_name}" } schema = { '$ref' => schema_name } schemer = JSONSchemer.schema(schema, ref_resolver:) errors = schemer.validate(object) raise_error!(schema_name, object, errors) if errors.any? logger.debug { "Object #{object} conforms to #{schema_name}" } end |