Skip to content

Commit

Permalink
tweak operation & document validations
Browse files Browse the repository at this point in the history
- document's service is optional
- operation's alsoKnownAs is required
- don't fail on invalid services in document, just ignore them
- don't fail on invalid alsoKnownAs handles, just ignore them
  • Loading branch information
mackuba committed Mar 25, 2024
1 parent f2e7a29 commit 06e10ce
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
3 changes: 1 addition & 2 deletions lib/didkit/at_handles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ class FormatError < StandardError
def parse_also_known_as(aka)
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.is_a?(Array)
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.all? { |x| x.is_a?(String) }
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.all? { |x| x =~ %r(\Aat://[^/]+\z) }

aka.map { |x| x.gsub('at://', '') }
aka.select { |x| x =~ %r(\Aat://[^/]+\z) }.map { |x| x.gsub('at://', '') }
end
end
end
28 changes: 12 additions & 16 deletions lib/didkit/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,18 @@ def initialize(did, json)
@did = did
@json = json

service = json['service']
raise FormatError, "Missing service key" if service.nil?
raise FormatError, "Invalid service data" unless service.is_a?(Array) && service.all? { |x| x.is_a?(Hash) }

@services = service.map { |x|
id, type, endpoint = x.values_at('id', 'type', 'serviceEndpoint')

raise FormatError, "Missing service id" unless id
raise FormatError, "Invalid service id: #{id.inspect}" unless id.is_a?(String) && id.start_with?('#')
raise FormatError, "Missing service type" unless type
raise FormatError, "Invalid service type: #{type.inspect}" unless type.is_a?(String)
raise FormatError, "Missing service endpoint" unless endpoint
raise FormatError, "Invalid service endpoint: #{endpoint.inspect}" unless endpoint.is_a?(String)

ServiceRecord.new(id.gsub(/^#/, ''), type, endpoint)
}
if service = json['service']
raise FormatError, "Invalid service data" unless service.is_a?(Array) && service.all? { |x| x.is_a?(Hash) }

@services = service.filter_map { |x|
id, type, endpoint = x.values_at('id', 'type', 'serviceEndpoint')
next unless id.is_a?(String) && id.start_with?('#') && type.is_a?(String) && endpoint.is_a?(String)

ServiceRecord.new(id.gsub(/^#/, ''), type, endpoint)
}
else
@services = []
end

@handles = parse_also_known_as(json['alsoKnownAs'] || [])
end
Expand Down
2 changes: 1 addition & 1 deletion lib/didkit/plc_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def initialize(json)
ServiceRecord.new(k, type, endpoint)
}

@handles = parse_also_known_as(operation['alsoKnownAs'] || [])
@handles = parse_also_known_as(operation['alsoKnownAs'])
end
end
end

0 comments on commit 06e10ce

Please sign in to comment.