Skip to content

Commit

Permalink
chore: add checker for models supported as forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Hein Jeong committed Sep 27, 2022
1 parent bc310ad commit b0a017c
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/codegen-ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from './lib/template-renderer-factory';
export * from './lib/generate-form-definition';
export * from './lib/generate-view-definition';
export * from './lib/generic-from-datastore';
export * from './lib/check-support';

export * from './lib/renderer-helper';
export * from './lib/validation-helper';
Expand Down
73 changes: 73 additions & 0 deletions packages/codegen-ui/lib/__tests__/check-support.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { checkIsSupportedAsForm } from '../check-support';
import { GenericDataModel } from '../types';

describe('checkIsSupportedAsForm', () => {
it('should return false if model has no fields', () => {
const model: GenericDataModel = {
fields: {},
};

expect(checkIsSupportedAsForm(model)).toBe(false);
});

it('should return false if all fields are unsupported', () => {
const model: GenericDataModel = {
fields: {
nonModel: { dataType: { nonModel: 'myNonModel' }, required: false, readOnly: false, isArray: false },
relationship: {
dataType: 'ID',
required: false,
readOnly: false,
isArray: false,
relationship: { type: 'HAS_ONE', relatedModelName: 'RelatedModel' },
},
},
};

expect(checkIsSupportedAsForm(model)).toBe(false);
});

it('should return true if one supported field w unrequired unsupported fields', () => {
const model: GenericDataModel = {
fields: {
nonModel: { dataType: { nonModel: 'myNonModel' }, required: false, readOnly: false, isArray: false },
relationship: {
dataType: 'ID',
required: false,
readOnly: false,
isArray: false,
relationship: { type: 'HAS_ONE', relatedModelName: 'RelatedModel' },
},
supportedField: { dataType: 'Boolean', required: false, readOnly: false, isArray: false },
},
};

expect(checkIsSupportedAsForm(model)).toBe(true);
});

it('should return false if an unsupported field is required', () => {
const model: GenericDataModel = {
fields: {
requiredNonModel: { dataType: { nonModel: 'myNonModel' }, required: true, readOnly: false, isArray: false },
supportedField: { dataType: 'Boolean', required: false, readOnly: false, isArray: false },
},
};

expect(checkIsSupportedAsForm(model)).toBe(false);
});
});
68 changes: 68 additions & 0 deletions packages/codegen-ui/lib/check-support.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { GenericDataField, GenericDataModel } from './types';

/**
* Checks whether a field is supported as a form input
*/

export const checkIsSupportedAsFormField = (field: GenericDataField): boolean => {
const unsupportedFieldMap: { [key: string]: (f: GenericDataField) => boolean } = {
nonModel: (f) => typeof f.dataType === 'object' && 'nonModel' in f.dataType,
relationship: (f) => !!f.relationship,
};

const unsupportedArray = Object.values(unsupportedFieldMap);
return !unsupportedArray.some((callback) => callback(field));
};
/**
* Checks whether a form should be autogen'd from the given model
*/
export const checkIsSupportedAsForm = (model: GenericDataModel): boolean => {
const fieldsArray = Object.values(model.fields);

// no empty forms
if (fieldsArray.length === 0) {
return false;
}

let unsupportedFieldsCount = 0;

const hasRequiredUnsupportedField = fieldsArray.some((field) => {
const isUnsupported = !checkIsSupportedAsFormField(field);
if (isUnsupported) {
unsupportedFieldsCount += 1;

// no unsupported, required fields
if (field.required) {
return true;
}
}

return false;
});

if (hasRequiredUnsupportedField) {
return false;
}

// if all fields are unsupported
if (unsupportedFieldsCount === fieldsArray.length) {
return false;
}

return true;
};

0 comments on commit b0a017c

Please sign in to comment.