-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add checker for models supported as forms
- Loading branch information
Hein Jeong
committed
Sep 27, 2022
1 parent
bc310ad
commit b0a017c
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |