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

fix: allow strings for signal messages and validate amount only for transaction messages #62

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 28 additions & 7 deletions src/helpers/tests/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,16 @@ describe('validateMessage', () => {
expect(validator.validateMessage('', 2).success).toBe(false);
});

test('should return false for an empty string signal message', () => {
expect(validator.validateMessage('', 3).success).toBe(false);
test('should return true for an empty string signal message', () => {
expect(validator.validateMessage('', 3).success).toBe(true);
});

test('should return false for an empty json rich message', () => {
expect(validator.validateMessage('{}', 2).success).toBe(false);
test('should return true for an empty json rich message', () => {
expect(validator.validateMessage('{}', 2).success).toBe(true);
});

test('should return false for an empty json signal message', () => {
expect(validator.validateMessage('{}', 3).success).toBe(false);
expect(validator.validateMessage('{}', 3).success).toBe(true);
});

test('should return true for a json rich message with the given amount', () => {
Expand All @@ -360,15 +360,24 @@ describe('validateMessage', () => {
).toBe(false);
});

test('should return false for a json signal message with upercase coin name', () => {
test('should return false for a json rich message with upercase coin name', () => {
yoxira marked this conversation as resolved.
Show resolved Hide resolved
expect(
validator.validateMessage(
'{"amount": "0.13", "type": "ETH_transaction"}',
3
2
).success
).toBe(false);
});

test('should return true for a json signal message with upercase coin name', () => {
expect(
validator.validateMessage(
'{"amount": "0.13", "type": "ETH_transaction"}',
3
).success
).toBe(true);
});

yoxira marked this conversation as resolved.
Show resolved Hide resolved
test('should return true for a json rich message with lowercase coin name', () => {
expect(
validator.validateMessage(
Expand All @@ -386,6 +395,18 @@ describe('validateMessage', () => {
).success
).toBe(true);
});

test('should allow a string signal message', () => {
expect(validator.validateMessage('not a json string', 3).success).toBe(
true
);
});

test('should NOT allow a string rich message', () => {
expect(validator.validateMessage('not a json string', 2).success).toBe(
false
);
});
});

describe('isDelegateName', () => {
Expand Down
31 changes: 15 additions & 16 deletions src/helpers/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const validateMessage = (
};
}

if ([MessageType.Rich, MessageType.Signal].includes(messageType)) {
if ([MessageType.Rich].includes(messageType)) {
const data = parseJsonSafe(message);

const {success, result} = data;
Expand All @@ -98,21 +98,20 @@ export const validateMessage = (
}

const typeInLowerCase = result.type?.toLowerCase();
if (
typeInLowerCase?.includes('_transaction') &&
typeInLowerCase !== result.type
) {
return {
success: false,
error: "Value '<coin>_transaction' must be in lower case",
};
}

if (typeof result.amount !== 'string' || !isStringAmount(result.amount)) {
return {
success: false,
error: "Field 'amount' must be a string, representing a number",
};
if (typeInLowerCase?.includes('_transaction')) {
yoxira marked this conversation as resolved.
Show resolved Hide resolved
if (typeInLowerCase !== result.type) {
return {
success: false,
error: "Value '<coin>_transaction' must be in lower case",
};
}

if (typeof result.amount !== 'string' || !isStringAmount(result.amount)) {
return {
success: false,
error: "Field 'amount' must be a string, representing a number",
};
}
}
}

Expand Down