Class: Portage::Ucp::SchemaValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/portage/ucp/schema_validator.rb

Overview

Validates data against UCP's own published JSON Schemas / OpenRPC docs — offline, against the copy vendored under schemas// (see §13: ucpchecker.com is a manual pre-release check only, never a CI gate; this is the CI-safe equivalent).

The vendored tree mirrors https://ucp.dev//... path-for-path (schemas/2026-04-08/schemas/shopping/cart.json <-> https://ucp.dev/2026-04-08/schemas/shopping/cart.json), so every $ref inside a vendored document — however deeply nested — resolves to another vendored file with a single prefix rewrite, no network access needed.

Instance Method Summary collapse

Constructor Details

#initialize(version: "2026-04-08", root: File.join(__dir__, "..", "..", "..", "schemas")) ⇒ SchemaValidator

Returns a new instance of SchemaValidator.



17
18
19
20
# File 'lib/portage/ucp/schema_validator.rb', line 17

def initialize(version: "2026-04-08", root: File.join(__dir__, "..", "..", "..", "schemas"))
  @base_url = "https://ucp.dev/#{version}/"
  @base_dir = File.expand_path(File.join(root, version))
end

Instance Method Details

#errors_for(relative_path, data) ⇒ Array<String>

Returns human-readable validation error messages: empty means data conforms.

Parameters:

  • relative_path (String) —

    e.g. "schemas/shopping/cart.json", matching the vendored path under schemas//. A path with a #/$defs/... fragment (e.g. "schemas/shopping/catalog_search.json#/$defs/search_response") validates against that named subschema instead of the document root — for schemas like catalog_search.json/catalog_lookup.json that define several request/response shapes as siblings under $defs rather than being one schema per file.

Returns:

  • (Array<String>) —

    human-readable validation error messages: empty means data conforms.



32
33
34
35
# File 'lib/portage/ucp/schema_validator.rb', line 32

def errors_for(relative_path, data)
  schemer = JSONSchemer.schema(schema_for(relative_path), ref_resolver: method(:resolve_ref))
  schemer.validate(data).map { |error| JSONSchemer::Errors.pretty(error) }
end

#method_names(relative_path) ⇒ Object

Method names declared by a vendored OpenRPC document, e.g. "services/shopping/mcp.openrpc.json" -> %w[create_checkout get_cart ...].



43
44
45
# File 'lib/portage/ucp/schema_validator.rb', line 43

def method_names(relative_path)
  load(relative_path).fetch("methods").map { |m| m.fetch("name") }
end

#valid?(relative_path, data) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/portage/ucp/schema_validator.rb', line 37

def valid?(relative_path, data)
  errors_for(relative_path, data).empty?
end