Skip to content

Commit

Permalink
Merge pull request #92 from muzzammilshahid/validate-all-messages
Browse files Browse the repository at this point in the history
Use generic message validator in all messages
  • Loading branch information
muzzammilshahid authored May 22, 2024
2 parents dd00129 + 16ea748 commit 41351c9
Show file tree
Hide file tree
Showing 26 changed files with 784 additions and 309 deletions.
20 changes: 12 additions & 8 deletions lib/src/messages/abort.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "package:wampproto/src/messages/message.dart";
import "package:wampproto/src/messages/util.dart";
import "package:wampproto/src/messages/validation_spec.dart";

class Abort implements Message {
Abort(this.details, this.reason);
Expand All @@ -8,20 +9,23 @@ class Abort implements Message {

static const String text = "ABORT";

static const int minLength = 3;
static const int maxLength = 3;
static final _validationSpec = ValidationSpec(
minLength: 3,
maxLength: 3,
message: text,
spec: {
1: validateDetails,
2: validateReason,
},
);

final Map<String, dynamic> details;
final String reason;

static Abort parse(final List<dynamic> message) {
sanityCheck(message, minLength, maxLength, id, text);
var fields = validateMessage(message, id, text, _validationSpec);

Map<String, dynamic> details = validateMapOrRaise(message[1], text, "details");

String reason = validateStringOrRaise(message[2], text, "reason");

return Abort(details, reason);
return Abort(fields.details!, fields.reason!);
}

@override
Expand Down
20 changes: 12 additions & 8 deletions lib/src/messages/authenticate.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "package:wampproto/src/messages/message.dart";
import "package:wampproto/src/messages/util.dart";
import "package:wampproto/src/messages/validation_spec.dart";

class Authenticate implements Message {
Authenticate(this.signature, this.extra);
Expand All @@ -8,20 +9,23 @@ class Authenticate implements Message {

static const String text = "AUTHENTICATE";

static const int minLength = 3;
static const int maxLength = 3;
static final _validationSpec = ValidationSpec(
minLength: 3,
maxLength: 3,
message: text,
spec: {
1: validateSignature,
2: validateExtra,
},
);

final String signature;
final Map<String, dynamic> extra;

static Authenticate parse(final List<dynamic> message) {
sanityCheck(message, minLength, maxLength, id, text);
var fields = validateMessage(message, id, text, _validationSpec);

String signature = validateStringOrRaise(message[1], text, "signature");

Map<String, dynamic> extra = validateMapOrRaise(message[2], text, "extra");

return Authenticate(signature, extra);
return Authenticate(fields.signature!, fields.extra!);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/src/messages/call.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Call implements Message {
static Call parse(final List<dynamic> message) {
var fields = validateMessage(message, id, text, _validationSpec);

return Call(fields.requestId!, fields.uri!, args: fields.args, kwargs: fields.kwargs, options: fields.options);
return Call(fields.requestID!, fields.uri!, args: fields.args, kwargs: fields.kwargs, options: fields.options);
}

@override
Expand Down
20 changes: 12 additions & 8 deletions lib/src/messages/cancel.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "package:wampproto/src/messages/message.dart";
import "package:wampproto/src/messages/util.dart";
import "package:wampproto/src/messages/validation_spec.dart";

class Cancel implements Message {
Cancel(this.callRequestID, this.options);
Expand All @@ -8,20 +9,23 @@ class Cancel implements Message {

static const String text = "CANCEL";

static const int minLength = 3;
static const int maxLength = 3;
static final _validationSpec = ValidationSpec(
minLength: 3,
maxLength: 3,
message: text,
spec: {
1: validateRequestID,
2: validateOptions,
},
);

final int callRequestID;
final Map<String, dynamic> options;

static Cancel parse(final List<dynamic> message) {
sanityCheck(message, minLength, maxLength, id, text);
var fields = validateMessage(message, id, text, _validationSpec);

int callRequestID = validateIntOrRaise(message[1], text, "call requestID");

Map<String, dynamic> options = validateMapOrRaise(message[2], text, "options");

return Cancel(callRequestID, options);
return Cancel(fields.requestID!, fields.options!);
}

@override
Expand Down
20 changes: 12 additions & 8 deletions lib/src/messages/challenge.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "package:wampproto/src/messages/message.dart";
import "package:wampproto/src/messages/util.dart";
import "package:wampproto/src/messages/validation_spec.dart";

class Challenge implements Message {
Challenge(this.authMethod, this.extra);
Expand All @@ -8,20 +9,23 @@ class Challenge implements Message {

static const String text = "CHALLENGE";

static const int minLength = 3;
static const int maxLength = 3;
static final _validationSpec = ValidationSpec(
minLength: 3,
maxLength: 3,
message: text,
spec: {
1: validateAuthMethod,
2: validateExtra,
},
);

final String authMethod;
final Map<String, dynamic> extra;

static Challenge parse(final List<dynamic> message) {
sanityCheck(message, minLength, maxLength, id, text);
var fields = validateMessage(message, id, text, _validationSpec);

String authMethod = validateStringOrRaise(message[1], text, "authmethod");

Map<String, dynamic> extra = validateMapOrRaise(message[2], text, "extra");

return Challenge(authMethod, extra);
return Challenge(fields.authmethod!, fields.extra!);
}

@override
Expand Down
47 changes: 24 additions & 23 deletions lib/src/messages/error.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "package:wampproto/src/messages/message.dart";
import "package:wampproto/src/messages/util.dart";
import "package:wampproto/src/messages/validation_spec.dart";

class Error implements Message {
Error(
Expand All @@ -17,8 +18,19 @@ class Error implements Message {

static const String text = "ERROR";

static const int minLength = 5;
static const int maxLength = 7;
static final _validationSpec = ValidationSpec(
minLength: 5,
maxLength: 7,
message: text,
spec: {
1: validateMessageType,
2: validateRequestID,
3: validateDetails,
4: validateURI,
5: validateArgs,
6: validateKwargs,
},
);

final int msgType;
final int requestID;
Expand All @@ -28,27 +40,16 @@ class Error implements Message {
final Map<String, dynamic> details;

static Error parse(final List<dynamic> message) {
sanityCheck(message, minLength, maxLength, id, text);

int msgType = validateIntOrRaise(message[1], text, "message type");

int requestID = validateIntOrRaise(message[2], text, "request ID");

Map<String, dynamic> details = validateMapOrRaise(message[3], text, "details");

String uri = validateStringOrRaise(message[4], text, "uri");

List<dynamic>? args;
if (message.length > minLength) {
args = validateListOrRaise(message[5], text, "args");
}

Map<String, dynamic>? kwargs;
if (message.length == maxLength) {
kwargs = validateMapOrRaise(message[6], text, "kwargs");
}

return Error(msgType, requestID, uri, args: args, kwargs: kwargs, details: details);
var fields = validateMessage(message, id, text, _validationSpec);

return Error(
fields.messageType!,
fields.requestID!,
fields.uri!,
args: fields.args,
kwargs: fields.kwargs,
details: fields.details,
);
}

@override
Expand Down
43 changes: 22 additions & 21 deletions lib/src/messages/event.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "package:wampproto/src/messages/message.dart";
import "package:wampproto/src/messages/util.dart";
import "package:wampproto/src/messages/validation_spec.dart";

class Event implements Message {
Event(
Expand All @@ -16,8 +17,18 @@ class Event implements Message {

static const String text = "EVENT";

static const int minLength = 4;
static const int maxLength = 6;
static final _validationSpec = ValidationSpec(
minLength: 4,
maxLength: 6,
message: text,
spec: {
1: validateSubscriptionID,
2: validatePublicationID,
3: validateDetails,
4: validateArgs,
5: validateKwargs,
},
);

final int subscriptionID;
final int publicationID;
Expand All @@ -26,25 +37,15 @@ class Event implements Message {
final Map<String, dynamic> details;

static Event parse(final List<dynamic> message) {
sanityCheck(message, minLength, maxLength, id, text);

int subscriptionID = validateIntOrRaise(message[1], text, "subscription ID");

int publicationID = validateIntOrRaise(message[2], text, "publication ID");

Map<String, dynamic> details = validateMapOrRaise(message[3], text, "details");

List<dynamic>? args;
if (message.length > minLength) {
args = validateListOrRaise(message[4], text, "args");
}

Map<String, dynamic>? kwargs;
if (message.length == maxLength) {
kwargs = validateMapOrRaise(message[5], text, "kwargs");
}

return Event(subscriptionID, publicationID, args: args, kwargs: kwargs, details: details);
var fields = validateMessage(message, id, text, _validationSpec);

return Event(
fields.subscriptionID!,
fields.publicationID!,
args: fields.args,
kwargs: fields.kwargs,
details: fields.details,
);
}

@override
Expand Down
20 changes: 12 additions & 8 deletions lib/src/messages/goodbye.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "package:wampproto/src/messages/message.dart";
import "package:wampproto/src/messages/util.dart";
import "package:wampproto/src/messages/validation_spec.dart";

class Goodbye implements Message {
Goodbye(this.details, this.reason);
Expand All @@ -8,20 +9,23 @@ class Goodbye implements Message {

static const String text = "GOODBYE";

static const int minLength = 3;
static const int maxLength = 3;
static final _validationSpec = ValidationSpec(
minLength: 3,
maxLength: 3,
message: text,
spec: {
1: validateDetails,
2: validateReason,
},
);

final Map<String, dynamic> details;
final String reason;

static Goodbye parse(final List<dynamic> message) {
sanityCheck(message, minLength, maxLength, id, text);
var fields = validateMessage(message, id, text, _validationSpec);

Map<String, dynamic> details = validateMapOrRaise(message[1], text, "details");

String reason = validateStringOrRaise(message[2], text, "reason");

return Goodbye(details, reason);
return Goodbye(fields.details!, fields.reason!);
}

@override
Expand Down
34 changes: 19 additions & 15 deletions lib/src/messages/hello.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "package:wampproto/src/messages/message.dart";
import "package:wampproto/src/messages/util.dart";
import "package:wampproto/src/messages/validation_spec.dart";

class Hello implements Message {
Hello(this.realm, this.roles, this.authID, this.authMethods, {Map<String, dynamic>? authExtra})
Expand All @@ -8,8 +9,15 @@ class Hello implements Message {
static const int id = 1;
static const String text = "HELLO";

static const int minLength = 3;
static const int maxLength = 3;
static final _validationSpec = ValidationSpec(
minLength: 3,
maxLength: 3,
message: text,
spec: {
1: validateRealm,
2: validateDetails,
},
);

final String realm;
final Map<String, dynamic> roles;
Expand All @@ -18,30 +26,26 @@ class Hello implements Message {
final Map<String, dynamic> authExtra;

static Hello parse(final List<dynamic> message) {
sanityCheck(message, minLength, maxLength, id, text);
var fields = validateMessage(message, id, text, _validationSpec);

String realm = validateStringOrRaise(message[1], text, "realm1");

Map<String, dynamic> details = validateMapOrRaise(message[2], text, "details");

Map<String, dynamic> roles = validateRolesOrRaise(details["roles"], text);
Map<String, dynamic> roles = validateRolesOrRaise(fields.details!["roles"], text);

String authid = "";
if (details["authid"] != null) {
authid = validateStringOrRaise(details["authid"], text, "authid");
if (fields.details!["authid"] != null) {
authid = validateStringOrRaise(fields.details!["authid"], text, "authid");
}

List<dynamic> authMethods = [];
if (details["authmethods"] != null) {
authMethods = validateListOrRaise(details["authmethods"], text, "authmethods");
if (fields.details!["authmethods"] != null) {
authMethods = validateListOrRaise(fields.details!["authmethods"], text, "authmethods");
}

Map<String, dynamic>? authExtra;
if (details["authextra"] != null) {
authExtra = validateMapOrRaise(details["authextra"], text, "authextra");
if (fields.details!["authextra"] != null) {
authExtra = validateMapOrRaise(fields.details!["authextra"], text, "authextra");
}

return Hello(realm, roles, authid, authMethods, authExtra: authExtra);
return Hello(fields.realm!, roles, authid, authMethods, authExtra: authExtra);
}

@override
Expand Down
Loading

0 comments on commit 41351c9

Please sign in to comment.