Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic message validator #44

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,3 @@ jobs:
bundler-cache: true
- name: Run the default task
run: bundle exec rake

- name: Run Steep check
run: bundle exec steep check
4 changes: 4 additions & 0 deletions lib/wampproto/message.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

require_relative "message/exceptions"
require_relative "message/util"
require_relative "message/validation_spec"

require_relative "message/base"
require_relative "message/hello"
require_relative "message/welcome"
Expand Down
65 changes: 57 additions & 8 deletions lib/wampproto/message/abort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,65 @@

module Wampproto
module Message
# interface for abort fields
class IAbortFields
def details
raise NotImplementedError
end

def reason
raise NotImplementedError
end

def args
raise NotImplementedError
end

def kwargs
raise NotImplementedError
end
end

# abort fields
class AbortFields < IAbortFields
attr_reader :details, :reason, :args, :kwargs

def initialize(details, reason, *args, **kwargs)
super()
@details = details
@reason = reason
@args = args
@kwargs = kwargs
end
end

# abort message
class Abort < Base
attr_reader :details, :reason, :args, :kwargs

TEXT = "ABORT"
VALIDATION_SPEC = Message::ValidationSpec.new(
3,
5,
TEXT,
{
1 => Message::Util.method(:validate_details),
2 => Message::Util.method(:validate_reason),
3 => Message::Util.method(:validate_args),
4 => Message::Util.method(:validate_kwargs)
}
)

def initialize(details, reason, *args, **kwargs)
super()
@details = Validate.hash!("Details", details)
@reason = Validate.string!("Reason", reason)
@args = Validate.array!("Arguments", args)
@kwargs = Validate.hash!("Keyword Arguments", kwargs)
@details = details
@reason = reason
@args = args
@kwargs = kwargs
end

def self.with_fields(fields)
new(fields.details, fields.reason, *fields.args, **fields.kwargs)
end

def marshal
Expand All @@ -22,10 +71,10 @@ def marshal
end

def self.parse(wamp_message)
_type, details, reason, args, kwargs = wamp_message
args ||= []
kwargs ||= {}
new(details, reason, *args, **kwargs)
fields = Util.validate_message(wamp_message, Type::ABORT, VALIDATION_SPEC)
fields.args ||= []
fields.kwargs ||= {}
Abort.with_fields(fields)
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions lib/wampproto/message/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Wampproto
module Message
module Exceptions
INVALID_DATA_TYPE_ERROR = "%<message>s: value at index %<index>s must be of type '%<expected_type>s'" \
"but was %<actual_type>s"
INVALID_RANGE_ERROR = "%<message>s: value at index %<index>s must be between '%<start>s' and '%<end>s" \
"but was %<actual>s"
INVALID_DETAIL_ERROR = "%<message>s: value at index %<index>s for key '%<key>s' must be of type" \
"'%<expected_type>s' but was %<actual_type>s"
end
end
end
Loading