Skip to content

Commit

Permalink
fix: less than char validation (#739)
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Shih <jushih@amazon.com>
  • Loading branch information
Jshhhh and Justin Shih committed Nov 1, 2022
1 parent 369e7a9 commit c0ee240
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ describe('validateField tests', () => {
});
it('should validate LESS_THAN_CHAR_LENGTH type', () => {
expect(
validateField('123', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, numValues: [4], validationMessage: '' }]),
validateField('123', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, numValues: [3], validationMessage: '' }]),
).toEqual({ hasError: false });
expect(
validateField('', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, numValues: [3], validationMessage: '' }]),
).toEqual({ hasError: false });
expect(
validateField('23445', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, numValues: [3], validationMessage: '' }]),
).toEqual({ hasError: true, errorMessage: 'The value must be shorter than 3' });
).toEqual({ hasError: true, errorMessage: 'The value must be 3 characters or fewer' });
});
it('should validate GREATER_THAN_CHAR_LENGTH type', () => {
expect(
Expand Down
10 changes: 6 additions & 4 deletions packages/codegen-ui-react/lib/utils/forms/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ const checkValidation = (value: any, validation: FieldValidationConfiguration) =
switch (validation.type) {
case 'LessThanChar':
return {
hasError: !(value.length < validation.numValues[0]),
errorMessage: validation.validationMessage || `The value must be shorter than ${validation.numValues[0]}`,
hasError: !(value.length <= validation.numValues[0]),
errorMessage:
validation.validationMessage || `The value must be ${validation.numValues[0]} characters or fewer`,
};
case 'GreaterThanChar':
return {
hasError: !(value.length > validation.numValues[0]),
errorMessage: validation.validationMessage || `The value must be longer than ${validation.numValues[0]}`,
errorMessage:
validation.validationMessage || `The value must be at least ${validation.numValues[0]} characters`,
};
case 'LessThanNum':
return {
Expand Down Expand Up @@ -459,7 +461,7 @@ export const generateValidationFunction = () => {
factory.createIdentifier('value'),
factory.createIdentifier('length'),
),
factory.createToken(ts.SyntaxKind.LessThanToken),
factory.createToken(ts.SyntaxKind.LessThanEqualsToken),
factory.createElementAccessExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('validation'),
Expand Down

0 comments on commit c0ee240

Please sign in to comment.