Skip to content

Commit

Permalink
Add port validation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardjkim committed Oct 29, 2024
1 parent 9041241 commit f455f90
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions web/packages/shared/components/Validation/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
requiredRoleArn,
requiredEmailLike,
requiredIamRoleName,
requiredPort,
} from './rules';

describe('requiredField', () => {
Expand Down Expand Up @@ -138,3 +139,17 @@ describe('requiredEmailLike', () => {
);
});
});

describe('requiredPort', () => {
const errMsg = 'Port required [0-65535]';
test.each`
port | expected
${''} | ${{ valid: false, message: errMsg }}
${'alice'} | ${{ valid: false, message: errMsg }}
${'99999'} | ${{ valid: false, message: errMsg }}
${'1'} | ${{ valid: true }}
${'65535'} | ${{ valid: true }}
`('port: $port', ({ port, expected }) => {
expect(requiredPort(port)()).toEqual(expected);
});
});
17 changes: 17 additions & 0 deletions web/packages/shared/components/Validation/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@ const requiredMatchingRoleNameAndRoleArn =
};
};

/**
* requiredPort checks if the given value is a valid port value [1-65535].
*/
const requiredPort: Rule = port => () => {
let val = Number(port);
if (Number.isInteger(val) && val > 0 && val <= 65535) {
return {
valid: true,
};
}
return {
valid: false,
message: 'Port required [1-65535]',
};
};

/**
* A rule function that combines multiple inner rule functions. All rules must
* return `valid`, otherwise it returns a comma separated string containing all
Expand Down Expand Up @@ -275,4 +291,5 @@ export {
requiredAll,
requiredMatchingRoleNameAndRoleArn,
validAwsIAMRoleName,
requiredPort,
};

0 comments on commit f455f90

Please sign in to comment.