Skip to content

Commit

Permalink
Add generic message validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahad-10 committed Jul 24, 2024
1 parent 7a63e37 commit 490c8a8
Show file tree
Hide file tree
Showing 9 changed files with 706 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/wampproto/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
require_relative "message/invocation"
require_relative "message/yield"

require_relative "message/util"

module Wampproto
# message root
module Message
Expand Down
35 changes: 35 additions & 0 deletions lib/wampproto/message/abort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,41 @@

module Wampproto
module Message
# interface for abort fields
class IAbortFields
attr_reader :details, :reason, :args, :kwargs
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 = 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)
Expand All @@ -14,6 +45,10 @@ def initialize(details, reason, *args, **kwargs)
@kwargs = Validate.hash!("Keyword Arguments", kwargs)
end

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

def marshal
@marshal = [Type::ABORT, details, reason]
@marshal << args if kwargs.any? || args.any?
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

0 comments on commit 490c8a8

Please sign in to comment.