Skip to content

Commit

Permalink
chore: add phone validation, change required validation, and fix regex (
Browse files Browse the repository at this point in the history
#615)

Co-authored-by: Hein Jeong <heinje@amazon.com>
  • Loading branch information
hein-j and Hein Jeong authored Sep 1, 2022
1 parent ec5dab8 commit c8c9689
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export default function CustomDataForm(props) {
const [modelFields, setModelFields] = useStateMutationAction({});
const [errors, setErrors] = useStateMutationAction({});
const validations = {
name: [],
email: [],
name: [{ type: \\"Required\\" }],
email: [{ type: \\"Required\\" }],
city: [],
category: [],
};
Expand Down Expand Up @@ -1008,7 +1008,7 @@ export default function InputGalleryCreateForm(props) {
const validations = {
num: [],
rootbeer: [],
attend: [],
attend: [{ type: \\"Required\\" }],
maybeSlide: [],
maybeCheck: [],
timestamp: [],
Expand Down
58 changes: 42 additions & 16 deletions packages/codegen-ui-react/lib/__tests__/forms/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ describe('validateField tests', () => {
it('should validate REQUIRED type', () => {
expect(validateField('123', [{ type: ValidationTypes.REQUIRED, validationMessage: '' }])).toEqual({
hasError: false,
errorMessage: 'The value is required',
});
expect(validateField('', [{ type: ValidationTypes.REQUIRED, validationMessage: '' }])).toEqual({
hasError: true,
errorMessage: 'The value is required',
});
expect(validateField(0, [{ type: ValidationTypes.REQUIRED, validationMessage: 'test' }])).toEqual({
hasError: false,
errorMessage: 'test',
});
});
it('should validate START_WITH type', () => {
Expand Down Expand Up @@ -91,19 +89,22 @@ describe('validateField tests', () => {
validateField('123', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, numValues: [4], validationMessage: '' }]),
).toEqual({ hasError: false, errorMessage: 'The value must be shorter than 4' });
expect(
validateField('', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, numValues: [0], validationMessage: '' }]),
).toEqual({ hasError: true, errorMessage: 'The value must be shorter than 0' });
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' });
});
it('should validate GREATER_THAN_CHAR_LENGTH type', () => {
expect(
validateField('123', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, numValues: [0], validationMessage: '' }]),
).toEqual({ hasError: false, errorMessage: 'The value must be longer than 0' });
expect(
validateField('', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, numValues: [0], validationMessage: '' }]),
).toEqual({ hasError: true, errorMessage: 'The value must be longer than 0' });
validateField('', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, numValues: [3], validationMessage: '' }]),
).toEqual({ hasError: false });
expect(
validateField('', [
{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, numValues: [1], validationMessage: 'test' },
validateField('df', [
{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, numValues: [3], validationMessage: 'test' },
]),
).toEqual({ hasError: true, errorMessage: 'test' });
});
Expand Down Expand Up @@ -251,13 +252,12 @@ describe('validateField tests', () => {
hasError: false,
errorMessage: 'The value must be in a correct JSON format',
});
expect(validateField('\\\\', [{ type: ValidationTypes.JSON, validationMessage: '' }])).toEqual({
expect(validateField('\\\\', [{ type: ValidationTypes.JSON, validationMessage: 'test' }])).toEqual({
hasError: true,
errorMessage: 'The value must be in a correct JSON format',
errorMessage: 'test',
});
expect(validateField('', [{ type: ValidationTypes.JSON, validationMessage: 'test' }])).toEqual({
hasError: true,
errorMessage: 'test',
hasError: false,
});
});
it('should validate IP_ADDRESS type', () => {
Expand All @@ -270,13 +270,12 @@ describe('validateField tests', () => {
{ type: ValidationTypes.IP_ADDRESS, validationMessage: '' },
]),
).toEqual({ hasError: false, errorMessage: 'The value must be an IPv4 or IPv6 address' });
expect(validateField('1.1', [{ type: ValidationTypes.IP_ADDRESS, validationMessage: '' }])).toEqual({
expect(validateField('1.1', [{ type: ValidationTypes.IP_ADDRESS, validationMessage: 'test' }])).toEqual({
hasError: true,
errorMessage: 'The value must be an IPv4 or IPv6 address',
errorMessage: 'test',
});
expect(validateField('', [{ type: ValidationTypes.IP_ADDRESS, validationMessage: 'test' }])).toEqual({
hasError: true,
errorMessage: 'test',
hasError: false,
});
});
it('should validate URL type', () => {
Expand All @@ -297,4 +296,31 @@ describe('validateField tests', () => {
errorMessage: 'test',
});
});

it('should validate Phone type', () => {
expect(validateField('kdj34324', [{ type: ValidationTypes.PHONE }])).toEqual({
hasError: true,
errorMessage: 'The value must be a valid phone number',
});

expect(validateField('2938493029', [{ type: ValidationTypes.PHONE, validationMessage: 'test' }])).toEqual({
hasError: false,
errorMessage: 'test',
});

expect(validateField('293 849 3029', [{ type: ValidationTypes.PHONE, validationMessage: 'test' }])).toEqual({
hasError: false,
errorMessage: 'test',
});

expect(validateField('293-849-3029', [{ type: ValidationTypes.PHONE, validationMessage: 'test' }])).toEqual({
hasError: false,
errorMessage: 'test',
});

expect(validateField('293 849-3029', [{ type: ValidationTypes.PHONE, validationMessage: 'test' }])).toEqual({
hasError: false,
errorMessage: 'test',
});
});
});
Loading

0 comments on commit c8c9689

Please sign in to comment.