Skip to content

Commit

Permalink
add a couple of comments, use existing validation types
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Mar 11, 2024
1 parent 06a8f37 commit abfce06
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConsumerParameter } from '../@types/DDO/ConsumerParameter.js'
import { ValidateParams } from '../components/httpRoutes/validateCommands.js'
import { CORE_LOGGER } from './logging/common.js'

function checkString(value: any) {
Expand All @@ -20,10 +21,11 @@ function checkObject(value: any) {
export function validateConsumerParameters(
ddoConsumerParameters: ConsumerParameter[],
userSentObject: any[]
) {
const validation = {
): ValidateParams {
const validation: ValidateParams = {
valid: true,
message: ''
reason: '',
status: 200
}

try {
Expand All @@ -36,13 +38,18 @@ export function validateConsumerParameters(
if (!checkObject(sentObject)) {
throw new Error(`Value is not an object`)
}
// check if key exists in object and if it is required or not, if not add default value
const sentObjectKey = consumerParameter.name
if (sentObject[sentObjectKey] === undefined) {
if (
sentObject[sentObjectKey] === undefined ||
sentObject[sentObjectKey] === null
) {
if (consumerParameter.required) {
throw new Error(`value of key ${sentObjectKey} parameter is required`)
}
sentObject[sentObjectKey] = consumerParameter.default
}
// check the value for that key
const sentObjectValue = sentObject[sentObjectKey]
if (consumerParameter.type === 'text' && !checkString(sentObjectValue)) {
throw new Error(
Expand Down Expand Up @@ -75,7 +82,8 @@ export function validateConsumerParameters(
} catch (error) {
CORE_LOGGER.error(error.message)
validation.valid = false
validation.message = error.message
validation.reason = error.message
validation.status = 400
return validation
}
}

0 comments on commit abfce06

Please sign in to comment.