Skip to content

Commit

Permalink
chore: update validation type to match model (#545)
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 authored Jul 28, 2022
1 parent 363ea15 commit ea486eb
Show file tree
Hide file tree
Showing 17 changed files with 1,110 additions and 859 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ export default function myPostForm(props) {
const { value } = e.target;
const isValidResult = onValidate?.[\\"caption\\"]
? await onValidate[\\"caption\\"](value)
: validateField(value, caption - validation - rules);
: validateField(
value,
undefined - caption - validation - rules
);
setCaptionFieldError({ ...captionFieldError, ...isValidResult });
setFormValid(!captionFieldError.hasError);
setModelFields({ ...modelFields, caption: value });
Expand All @@ -239,7 +242,10 @@ export default function myPostForm(props) {
const { value } = e.target;
const isValidResult = onValidate?.[\\"username\\"]
? await onValidate[\\"username\\"](value)
: validateField(value, username - validation - rules);
: validateField(
value,
undefined - username - validation - rules
);
setUsernameFieldError({
...usernameFieldError,
...isValidResult,
Expand All @@ -266,7 +272,10 @@ export default function myPostForm(props) {
const { value } = e.target;
const isValidResult = onValidate?.[\\"post_url\\"]
? await onValidate[\\"post_url\\"](value)
: validateField(value, post_url - validation - rules);
: validateField(
value,
undefined - post_url - validation - rules
);
setPost_urlFieldError({
...post_urlFieldError,
...isValidResult,
Expand All @@ -293,7 +302,10 @@ export default function myPostForm(props) {
const { value } = e.target;
const isValidResult = onValidate?.[\\"profile_url\\"]
? await onValidate[\\"profile_url\\"](value)
: validateField(value, profile_url - validation - rules);
: validateField(
value,
undefined - profile_url - validation - rules
);
setProfile_urlFieldError({
...profile_urlFieldError,
...isValidResult,
Expand Down Expand Up @@ -425,7 +437,10 @@ export default function myPostForm(props) {
const { value } = e.target;
const isValidResult = onValidate?.[\\"caption\\"]
? await onValidate[\\"caption\\"](value)
: validateField(value, caption - validation - rules);
: validateField(
value,
undefined - caption - validation - rules
);
setCaptionFieldError({ ...captionFieldError, ...isValidResult });
setFormValid(!captionFieldError.hasError);
setModelFields({ ...modelFields, caption: value });
Expand All @@ -449,7 +464,10 @@ export default function myPostForm(props) {
const { value } = e.target;
const isValidResult = onValidate?.[\\"username\\"]
? await onValidate[\\"username\\"](value)
: validateField(value, username - validation - rules);
: validateField(
value,
undefined - username - validation - rules
);
setUsernameFieldError({
...usernameFieldError,
...isValidResult,
Expand All @@ -476,7 +494,10 @@ export default function myPostForm(props) {
const { value } = e.target;
const isValidResult = onValidate?.[\\"post_url\\"]
? await onValidate[\\"post_url\\"](value)
: validateField(value, post_url - validation - rules);
: validateField(
value,
undefined - post_url - validation - rules
);
setPost_urlFieldError({
...post_urlFieldError,
...isValidResult,
Expand All @@ -503,7 +524,10 @@ export default function myPostForm(props) {
const { value } = e.target;
const isValidResult = onValidate?.[\\"profile_url\\"]
? await onValidate[\\"profile_url\\"](value)
: validateField(value, profile_url - validation - rules);
: validateField(
value,
undefined - profile_url - validation - rules
);
setProfile_urlFieldError({
...profile_urlFieldError,
...isValidResult,
Expand Down
136 changes: 86 additions & 50 deletions packages/codegen-ui-react/lib/__tests__/forms/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,137 +32,161 @@ describe('validateField tests', () => {
});
});
it('should validate START_WITH type', () => {
expect(validateField('abc', [{ type: ValidationTypes.START_WITH, values: [], validationMessage: 'test' }])).toEqual(
{ hasError: true, errorMessage: 'test' },
);
expect(validateField('abc', [{ type: ValidationTypes.START_WITH, values: ['4'], validationMessage: '' }])).toEqual({
expect(
validateField('abc', [{ type: ValidationTypes.START_WITH, strValues: [], validationMessage: 'test' }]),
).toEqual({ hasError: false });
expect(
validateField('abc', [{ type: ValidationTypes.START_WITH, strValues: ['4'], validationMessage: '' }]),
).toEqual({
hasError: true,
errorMessage: 'The value must start with 4',
});
expect(
validateField('aardvark', [{ type: ValidationTypes.START_WITH, values: ['a', 'b'], validationMessage: '' }]),
validateField('aardvark', [{ type: ValidationTypes.START_WITH, strValues: ['a', 'b'], validationMessage: '' }]),
).toEqual({ hasError: false, errorMessage: 'The value must start with a, b' });
});
it('should validate END_WITH type', () => {
expect(validateField('abc', [{ type: ValidationTypes.END_WITH, values: ['c'], validationMessage: '' }])).toEqual({
expect(
validateField('abc', [{ type: ValidationTypes.END_WITH, strValues: ['c'], validationMessage: 'test' }]),
).toEqual({
hasError: false,
errorMessage: 'The value must end with c',
errorMessage: 'test',
});
expect(
validateField('abc', [{ type: ValidationTypes.END_WITH, values: ['e', 'f'], validationMessage: '' }]),
validateField('abc', [{ type: ValidationTypes.END_WITH, strValues: ['e', 'f'], validationMessage: '' }]),
).toEqual({ hasError: true, errorMessage: 'The value must end with e, f' });
expect(validateField('', [{ type: ValidationTypes.END_WITH, values: [], validationMessage: 'test' }])).toEqual({
hasError: true,
errorMessage: 'test',
expect(validateField('', [{ type: ValidationTypes.END_WITH, strValues: [], validationMessage: 'test' }])).toEqual({
hasError: false,
});
});
it('should validate CONTAINS type', () => {
expect(validateField('abc', [{ type: ValidationTypes.CONTAINS, values: ['a'], validationMessage: '' }])).toEqual({
hasError: false,
errorMessage: 'The value must contain a',
});
expect(validateField('abc', [{ type: ValidationTypes.CONTAINS, strValues: ['a'], validationMessage: '' }])).toEqual(
{
hasError: false,
errorMessage: 'The value must contain a',
},
);
expect(
validateField('abcd', [{ type: ValidationTypes.CONTAINS, values: ['a', 'e'], validationMessage: '' }]),
validateField('abcd', [{ type: ValidationTypes.CONTAINS, strValues: ['a', 'e'], validationMessage: '' }]),
).toEqual({ hasError: false, errorMessage: 'The value must contain a, e' });
expect(
validateField('abc', [{ type: ValidationTypes.CONTAINS, values: ['d'], validationMessage: 'test' }]),
validateField('abc', [{ type: ValidationTypes.CONTAINS, strValues: ['d'], validationMessage: 'test' }]),
).toEqual({ hasError: true, errorMessage: 'test' });
});
it('should validate NOT_CONTAINS type', () => {
expect(
validateField('abc', [{ type: ValidationTypes.NOT_CONTAINS, values: ['4'], validationMessage: '' }]),
validateField('abc', [{ type: ValidationTypes.NOT_CONTAINS, strValues: ['4'], validationMessage: '' }]),
).toEqual({ hasError: false, errorMessage: 'The value must not contain 4' });
expect(
validateField('abc', [{ type: ValidationTypes.NOT_CONTAINS, values: ['d', 'a'], validationMessage: '' }]),
validateField('abc', [{ type: ValidationTypes.NOT_CONTAINS, strValues: ['d', 'a'], validationMessage: '' }]),
).toEqual({ hasError: true, errorMessage: 'The value must not contain d, a' });
expect(validateField('', [{ type: ValidationTypes.NOT_CONTAINS, values: [], validationMessage: 'test' }])).toEqual({
expect(
validateField('', [{ type: ValidationTypes.NOT_CONTAINS, strValues: [], validationMessage: 'test' }]),
).toEqual({
hasError: false,
errorMessage: 'test',
});
});
it('should validate LESS_THAN_CHAR_LENGTH type', () => {
expect(
validateField('123', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, values: 4, validationMessage: '' }]),
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, values: 0, validationMessage: '' }]),
validateField('', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, numValues: [0], validationMessage: '' }]),
).toEqual({ hasError: true, errorMessage: 'The value must be shorter than 0' });
});
it('should validate GREATER_THAN_CHAR_LENGTH type', () => {
expect(
validateField('123', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, values: 0, validationMessage: '' }]),
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, values: 0, validationMessage: '' }]),
validateField('', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, numValues: [0], validationMessage: '' }]),
).toEqual({ hasError: true, errorMessage: 'The value must be longer than 0' });
expect(
validateField('', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, values: 1, validationMessage: 'test' }]),
validateField('', [
{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, numValues: [1], validationMessage: 'test' },
]),
).toEqual({ hasError: true, errorMessage: 'test' });
});
it('should validate LESS_THAN_NUM type', () => {
expect(validateField(1, [{ type: ValidationTypes.LESS_THAN_NUM, values: 10, validationMessage: '' }])).toEqual({
hasError: false,
errorMessage: 'The value must be less than 10',
});
expect(validateField(2, [{ type: ValidationTypes.LESS_THAN_NUM, values: 1, validationMessage: '' }])).toEqual({
expect(validateField(1, [{ type: ValidationTypes.LESS_THAN_NUM, numValues: [10], validationMessage: '' }])).toEqual(
{
hasError: false,
errorMessage: 'The value must be less than 10',
},
);
expect(validateField(2, [{ type: ValidationTypes.LESS_THAN_NUM, numValues: [1], validationMessage: '' }])).toEqual({
hasError: true,
errorMessage: 'The value must be less than 1',
});
expect(validateField(0, [{ type: ValidationTypes.LESS_THAN_NUM, values: 0, validationMessage: 'test' }])).toEqual({
expect(
validateField(0, [{ type: ValidationTypes.LESS_THAN_NUM, numValues: [0], validationMessage: 'test' }]),
).toEqual({
hasError: true,
errorMessage: 'test',
});
});
it('should validate GREATER_THAN_NUM type', () => {
expect(validateField(1, [{ type: ValidationTypes.GREATER_THAN_NUM, values: 0, validationMessage: '' }])).toEqual({
expect(
validateField(1, [{ type: ValidationTypes.GREATER_THAN_NUM, numValues: [0], validationMessage: '' }]),
).toEqual({
hasError: false,
errorMessage: 'The value must be greater than 0',
});
expect(validateField(2, [{ type: ValidationTypes.GREATER_THAN_NUM, values: 3, validationMessage: '' }])).toEqual({
expect(
validateField(2, [{ type: ValidationTypes.GREATER_THAN_NUM, numValues: [3], validationMessage: '' }]),
).toEqual({
hasError: true,
errorMessage: 'The value must be greater than 3',
});
expect(
validateField(3, [{ type: ValidationTypes.GREATER_THAN_NUM, values: 3, validationMessage: 'test' }]),
validateField(3, [{ type: ValidationTypes.GREATER_THAN_NUM, numValues: [3], validationMessage: 'test' }]),
).toEqual({ hasError: true, errorMessage: 'test' });
});
it('should validate EQUAL_TO_NUM type', () => {
expect(validateField(1, [{ type: ValidationTypes.EQUAL_TO_NUM, values: [1, 2], validationMessage: '' }])).toEqual({
expect(
validateField(1, [{ type: ValidationTypes.EQUAL_TO_NUM, numValues: [1, 2], validationMessage: '' }]),
).toEqual({
hasError: false,
errorMessage: 'The value must be equal to 1 or 2',
});
expect(validateField(2, [{ type: ValidationTypes.EQUAL_TO_NUM, values: 3, validationMessage: '' }])).toEqual({
expect(validateField(2, [{ type: ValidationTypes.EQUAL_TO_NUM, numValues: [3], validationMessage: '' }])).toEqual({
hasError: true,
errorMessage: 'The value must be equal to 3',
});
expect(
validateField(3, [{ type: ValidationTypes.EQUAL_TO_NUM, values: [4, 5, 6], validationMessage: 'test' }]),
validateField(3, [{ type: ValidationTypes.EQUAL_TO_NUM, numValues: [4, 5, 6], validationMessage: 'test' }]),
).toEqual({ hasError: true, errorMessage: 'test' });
});
it('should validate BE_AFTER type', () => {
const startDate = new Date().toDateString();
const endDate1 = new Date('2021-01-09').toDateString();
const endDate2 = new Date('3000-01-09').toDateString();
expect(
validateField(startDate, [{ type: ValidationTypes.BE_AFTER, values: endDate1, validationMessage: '' }]),
validateField(startDate, [{ type: ValidationTypes.BE_AFTER, strValues: [endDate1], validationMessage: '' }]),
).toEqual({ hasError: false, errorMessage: `The value must be after ${endDate1}` });
expect(
validateField(startDate, [{ type: ValidationTypes.BE_AFTER, values: endDate2, validationMessage: '' }]),
validateField(startDate, [{ type: ValidationTypes.BE_AFTER, strValues: [endDate2], validationMessage: '' }]),
).toEqual({ hasError: true, errorMessage: `The value must be after ${endDate2}` });
expect(
validateField(startDate, [{ type: ValidationTypes.BE_AFTER, values: '', validationMessage: 'test' }]),
validateField(startDate, [{ type: ValidationTypes.BE_AFTER, strValues: [''], validationMessage: 'test' }]),
).toEqual({ hasError: true, errorMessage: 'test' });

const startTime = Date.now();
const endTime1 = startTime - 10;
expect(
validateField(startTime, [{ type: ValidationTypes.BE_AFTER, values: endTime1, validationMessage: '' }]),
validateField(startTime, [
{ type: ValidationTypes.BE_AFTER, strValues: [endTime1.toString()], validationMessage: '' },
]),
).toEqual({ hasError: false, errorMessage: `The value must be after ${endTime1}` });
expect(
validateField(endTime1, [{ type: ValidationTypes.BE_AFTER, values: startTime, validationMessage: '' }]),
validateField(endTime1, [
{ type: ValidationTypes.BE_AFTER, strValues: [startTime.toString()], validationMessage: '' },
]),
).toEqual({ hasError: true, errorMessage: `The value must be after ${startTime}` });
expect(
validateField(endTime1, [{ type: ValidationTypes.BE_AFTER, values: startTime, validationMessage: 'test' }]),
validateField(endTime1, [
{ type: ValidationTypes.BE_AFTER, strValues: [startTime.toString()], validationMessage: 'test' },
]),
).toEqual({ hasError: true, errorMessage: 'test' });
});
it('should validate BE_BEFORE type', () => {
Expand All @@ -171,25 +195,37 @@ describe('validateField tests', () => {
const endDate2 = new Date('2021-01-09').toString();

expect(
validateField(startDate, [{ type: ValidationTypes.BE_BEFORE, values: endDate1, validationMessage: '' }]),
validateField(startDate, [
{ type: ValidationTypes.BE_BEFORE, strValues: [endDate1.toString()], validationMessage: '' },
]),
).toEqual({ hasError: false, errorMessage: `The value must be before ${endDate1}` });
expect(
validateField(startDate, [{ type: ValidationTypes.BE_BEFORE, values: endDate2, validationMessage: '' }]),
validateField(startDate, [
{ type: ValidationTypes.BE_BEFORE, strValues: [endDate2.toString()], validationMessage: '' },
]),
).toEqual({ hasError: true, errorMessage: `The value must be before ${endDate2}` });
expect(
validateField(startDate, [{ type: ValidationTypes.BE_BEFORE, values: endDate2, validationMessage: 'test' }]),
validateField(startDate, [
{ type: ValidationTypes.BE_BEFORE, strValues: [endDate2.toString()], validationMessage: 'test' },
]),
).toEqual({ hasError: true, errorMessage: 'test' });

const startTime = Date.now();
const endTime1 = startTime + 10;
expect(
validateField(startTime, [{ type: ValidationTypes.BE_BEFORE, values: endTime1, validationMessage: '' }]),
validateField(startTime, [
{ type: ValidationTypes.BE_BEFORE, strValues: [endTime1.toString()], validationMessage: '' },
]),
).toEqual({ hasError: false, errorMessage: `The value must be before ${endTime1}` });
expect(
validateField(endTime1, [{ type: ValidationTypes.BE_BEFORE, values: startTime, validationMessage: '' }]),
validateField(endTime1, [
{ type: ValidationTypes.BE_BEFORE, strValues: [startTime.toString()], validationMessage: '' },
]),
).toEqual({ hasError: true, errorMessage: `The value must be before ${startTime}` });
expect(
validateField(endTime1, [{ type: ValidationTypes.BE_BEFORE, values: startTime, validationMessage: 'test' }]),
validateField(endTime1, [
{ type: ValidationTypes.BE_BEFORE, strValues: [startTime.toString()], validationMessage: 'test' },
]),
).toEqual({ hasError: true, errorMessage: 'test' });
});
it('should validate EMAIL type', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('form-render utils', () => {

it('should generate a datastore function', () => {
const form: StudioForm = {
id: '123',
name: 'mySampleForm',
formActionType: 'create',
dataType: { dataSourceType: 'DataStore', dataTypeName: 'Post' },
Expand All @@ -47,6 +48,7 @@ describe('form-render utils', () => {

it('should generate before & complete types if datastore config is set', () => {
const form: StudioForm = {
id: '123',
name: 'mySampleForm',
formActionType: 'create',
dataType: { dataSourceType: 'DataStore', dataTypeName: 'Post' },
Expand All @@ -62,6 +64,7 @@ describe('form-render utils', () => {

it('should generate regular onsubmit if dataSourceType is custom', () => {
const form: StudioForm = {
id: '123',
name: 'myCustomForm',
formActionType: 'create',
dataType: { dataSourceType: 'Custom', dataTypeName: 'Custom' },
Expand Down
Loading

0 comments on commit ea486eb

Please sign in to comment.