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: less than char validation #739

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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