-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
back: Add validation for numerical inputs in API.
This commit includes the addition of a utility function that checks the range of the data types and validates them. This is particularly useful for numerical inputs in the API that need to be within a specific range. The commit also updates the backend routes to use these validations. Issue: #202 Signed-off-by: Nikolay Martyanov <[email protected]>
- Loading branch information
1 parent
527ad8e
commit 39e8890
Showing
3 changed files
with
59 additions
and
11 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
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,23 @@ | ||
function getDataTypeRange(model, fieldName) { | ||
const field = model.rawAttributes[fieldName]; | ||
if (!field) { | ||
throw new Error(`Field ${fieldName} does not exist in the model`); | ||
} | ||
|
||
const dataType = field.type.key; | ||
|
||
switch (dataType) { | ||
case 'INTEGER': // Standard 4-byte integer | ||
return { min: -2147483648, max: 2147483647 }; | ||
case 'BIGINT': // 8-byte integer | ||
return { min: -9223372036854775808, max: 9223372036854775807n }; | ||
case 'SMALLINT': // 2-byte integer | ||
return { min: -32768, max: 32767 }; | ||
default: | ||
throw new Error(`Range for data type ${dataType} is not defined`); | ||
} | ||
} | ||
|
||
module.exports = { | ||
getDataTypeRange, | ||
}; |