Skip to content

Commit

Permalink
Move api_object_validation to the discovery_v1 gem (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouball committed Nov 21, 2023
1 parent cc48f8c commit 18b4d9b
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 1,003 deletions.
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,29 +277,40 @@ Google Sheets API will do one of following depending on the nature of the proble
common result)
3. Not return an error with some of the batch requests not having the expected outcome

Luckily, this library provides a way to validate that requests are valid and
identifies precisely where the request objects do not conform to the API description.
Luckily, you can validate that requests are valid and identifies precisely where
the request objects do not conform to the API description using the DiscoveryV1 API.
That is the subject of the next section [Validating requests](#validating-requests).

### Validating requests

The [`SheetsV4.validate_api_object`](https://rubydoc.info/gems/sheets_v4/SheetsV4#validate_api_object-class_method)
method can be used to validate request objects prior to using them in the Google
Sheets API.
Use the [DiscoveryV1 API](https://github.com/main-branch/discovery_v1)
can be used to validate request object prior to using them in the Google Sheets API.

This method takes a `schema_name` and an `object` to validate. Schema names can be
listed using [`SheetsV4.api_object_schema_names`](https://rubydoc.info/gems/sheets_v4/SheetsV4#api_object_schema_names-class_method).
In this API, [`DiscoveryV1.validate_object`](https://rubydoc.info/gems/discovery_v1/DiscoveryV1#validate_object-class_method)
validates a request object for a given schema. This method takes a `schema_name`
and an `object` to validate. Valid schemas names for an API can be listed using
[`SheetsV4.api_object_schema_names`](https://rubydoc.info/gems/sheets_v4/SheetsV4#api_object_schema_names-class_method).

This method will either return `true` if `object` conforms to the schema OR it
`validate_object` will either return `true` if `object` conforms to the schema OR it
will raise a RuntimeError noting where the object structure did not conform to
the schema.

In the previous examples (see [Building a request](#building-a-request)), the
following line can be inserted after the `requests = ...` line to validate the
following lines can be inserted after the `requests = ...` line to validate the
request:

```Ruby
SheetsV4.validate_api_object(schema: 'batch_update_spreadsheet_request', object: requests)
require 'discovery_v1'
discovery_service = DiscoveryV1.discovery_service
rest_description = discovery_service.get_rest_api('sheets', 'v4')
schema_name = 'batch_update_spreadsheet_request'
object = requests
begin
DiscoveryV1.validate_object(rest_description:, schema_name:, object:)
puts 'BatchUpdateSpreadsheetRequest object is valid'
rescue RuntimeError => e
puts e.message
end
```

### Google Extensions
Expand Down
5 changes: 4 additions & 1 deletion examples/set_background_color2
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'discovery_v1'
require 'discovery_v1/google_extensions'
require 'sheets_v4'
require 'googleauth'

Expand Down Expand Up @@ -48,7 +50,8 @@ def requests = { requests: [write_names, set_background_colors] }
# While not necessary, it can be helpful to identify errors since the API will return
# an error if the request is invalid but does not tell you where the problem is.
#
SheetsV4.validate_api_object(schema_name: 'batch_update_spreadsheet_request', object: requests)
rest_description = DiscoveryV1.discovery_service.get_rest_api('sheets', 'v4')
rest_description.validate_object(schema_name: 'batch_update_spreadsheet_request', object: requests)

spreadsheet_id = '18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY'
SheetsV4.sheets_service.batch_update_spreadsheet(spreadsheet_id, requests)
36 changes: 0 additions & 36 deletions lib/sheets_v4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require_relative 'sheets_v4/color'
require_relative 'sheets_v4/convert_dates_and_times'
require_relative 'sheets_v4/create_credential'
require_relative 'sheets_v4/api_object_validation'

require 'active_support'
require 'active_support/values/time_zone'
Expand Down Expand Up @@ -57,41 +56,6 @@ def sheets_service(credential_source: nil, scopes: nil, credential_creator: Shee
end
end

# @!group Validation

# Validate the object using the named JSON schema
#
# The JSON schemas are loaded from the Google Disocvery API. The schemas names are
# returned by `SheetsV4.api_object_schema_names`.
#
# @example
# schema_name = 'batch_update_spreadsheet_request'
# object = { 'requests' => [] }
# SheetsV4.validate_api_object(schema_name:, object:)
#
# @param schema_name [String] the name of the schema to validate against
# @param object [Object] the object to validate
# @param logger [Logger] the logger to use for logging error, info, and debug message
#
# @raise [RuntimeError] if the object does not conform to the schema
#
# @return [void]
#
def validate_api_object(schema_name:, object:, logger: Logger.new(nil))
SheetsV4::ApiObjectValidation::ValidateApiObject.new(logger:).call(schema_name:, object:)
end

# List the names of the schemas available to use in the Google Sheets API
#
# @example List the name of the schemas available
# SheetsV4.api_object_schema_names #=> ["add_banding_request", "add_banding_response", ...]
#
# @return [Array<String>] the names of the schemas available
#
def api_object_schema_names(logger: Logger.new(nil))
SheetsV4::ApiObjectValidation::LoadSchemas.new(logger:).call.keys.sort
end

# @!group Colors

# Given the name of the color, return a Google Sheets API color object
Expand Down
155 changes: 0 additions & 155 deletions lib/sheets_v4/api_object_validation/load_schemas.rb

This file was deleted.

72 changes: 0 additions & 72 deletions lib/sheets_v4/api_object_validation/resolve_schema_ref.rb

This file was deleted.

0 comments on commit 18b4d9b

Please sign in to comment.