Skip to content

Commit

Permalink
chore: enhance code
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-moncel committed May 20, 2021
1 parent 9aaccb3 commit a0e1e0b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 57 deletions.
2 changes: 2 additions & 0 deletions src/context/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const ConfigStore = require('../services/config-store');
const PermissionsChecker = require('../services/permissions-checker');
const PermissionsGetter = require('../services/permissions-getter');
const SchemaFileUpdater = require('../services/schema-file-updater');
const SmartActionFieldValidator = require('../services/smart-action-field-validator');
const SmartActionHook = require('../services/smart-action-hook');
const schemasGenerator = require('../generators/schemas');
const ModelsManager = require('../services/models-manager');
Expand Down Expand Up @@ -147,6 +148,7 @@ function initServices(context) {
context.addClass(OidcConfigurationRetrieverService);
context.addClass(OidcClientManagerService);
context.addClass(AuthenticationService);
context.addClass(SmartActionFieldValidator);
context.addClass(SmartActionHook);
}

Expand Down
58 changes: 58 additions & 0 deletions src/services/smart-action-field-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class SmartActionFieldValidator {
validFieldPrimitifType = [
'String',
'Number',
'Date',
'Boolean',
'File',
'Enum',
'Json',
'Dateonly',
];

validFieldArrayType = [
'String',
'Number',
'Date',
'boolean',
'File',
'Enum',
];

validateField(field) {
if (!field || Array.isArray(field) || typeof field !== 'object') throw new Error('Field must be an object.');

const {
field: fieldName,
description,
enums,
isRequired,
isReadOnly,
reference,
type,
} = field;

if (!fieldName) throw new Error('field attribute must be defined.');

if (typeof fieldName !== 'string') throw new Error('field attribute must be a string.');

if (description && typeof description !== 'string') throw new Error(`description of "${fieldName}" must be a string.`);

if (enums && !Array.isArray(enums)) throw new Error(`enums of "${fieldName}" must be an array.`);

if (isRequired && typeof isRequired !== 'boolean') throw new Error(`isRequired of "${fieldName}" must be a boolean.`);

if (isReadOnly && typeof isReadOnly !== 'boolean') throw new Error(`isReadOnly of "${fieldName}" must be a boolean.`);

if (reference && typeof reference !== 'string') throw new Error(`reference of "${fieldName}" should be a string.`);

if (type !== undefined && (Array.isArray(type)
? !this.validFieldArrayType.includes(type[0])
: !this.validFieldPrimitifType.includes(type))
) {
throw new Error(`type of "${fieldName}" must be a valid type. See the documentation for more information. https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields#available-field-options`);
}
}
}

module.exports = SmartActionFieldValidator;
60 changes: 3 additions & 57 deletions src/services/smart-action-hook.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
class SmartActionHook {
constructor({ isSameDataStructure, setFieldWidget }) {
constructor({ isSameDataStructure, setFieldWidget, smartActionFieldValidator }) {
this.isSameDataStructure = isSameDataStructure;
this.setFieldWidget = setFieldWidget;

this.validFieldPrimitifType = [
'String',
'Number',
'Date',
'Boolean',
'File',
'Enum',
'Json',
'Dateonly',
];

this.validFieldArrayType = [
'String',
'Number',
'Date',
'boolean',
'File',
'Enum',
];
this.smartActionFieldValidator = smartActionFieldValidator;
}

/**
Expand All @@ -38,41 +19,6 @@ class SmartActionHook {
});
}

validateField(field) {
if (!field || Array.isArray(field) || typeof field !== 'object') throw new Error('Field must be an object.');

const {
field: fieldName,
description,
enums,
isRequired,
isReadOnly,
reference,
type,
} = field;

if (!fieldName) throw new Error('field attribute must be defined.');

if (typeof fieldName !== 'string') throw new Error('field attribute must be a string.');

if (description && typeof description !== 'string') throw new Error(`description of "${fieldName}" must be a string.`);

if (enums && !Array.isArray(enums)) throw new Error(`enums of "${fieldName}" must be an array.`);

if (isRequired && typeof isRequired !== 'boolean') throw new Error(`isRequired of "${fieldName}" must be a boolean.`);

if (isReadOnly && typeof isReadOnly !== 'boolean') throw new Error(`isReadOnly of "${fieldName}" must be a boolean.`);

if (reference && typeof reference !== 'string') throw new Error(`reference of "${fieldName}" should be a string.`);

if (Array.isArray(type)
? !this.validFieldArrayType.includes(type[0])
: !this.validFieldPrimitifType.includes(type)
) {
throw new Error(`type of "${fieldName}" must be a valid type. See the documentation for more information. https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields#available-field-options`);
}
}

/**
* Get the response from user-defined hook.
*
Expand All @@ -95,7 +41,7 @@ class SmartActionHook {
}

return result.map((field) => {
this.validateField(field);
this.smartActionFieldValidator.validateField(field);
// Reset `value` when not present in `enums` (which means `enums` has changed).
if (Array.isArray(field.enums)) {
// `Value` can be an array if the type of fields is `[x]`
Expand Down
2 changes: 2 additions & 0 deletions test/services/smart-action-hook.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const ApplicationContext = require('../../src/context/application-context');
const SmartActionHook = require('../../src/services/smart-action-hook');
const SmartFieldActionFieldValidator = require('../../src/services/smart-action-field-validator');

function initContext(isSameDataStructure) {
const context = new ApplicationContext();
context.init((ctx) => ctx
.addInstance('isSameDataStructure', isSameDataStructure)
.addInstance('setFieldWidget', jest.fn())
.addClass(SmartFieldActionFieldValidator)
.addClass(SmartActionHook));

return context;
Expand Down

0 comments on commit a0e1e0b

Please sign in to comment.