-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9aaccb3
commit a0e1e0b
Showing
4 changed files
with
65 additions
and
57 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,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; |
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