first
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
// parse the params from http call
|
||||
|
||||
import * as ERR from '../utilities/errortexts.js'
|
||||
import {returnOnError} from "./reporterror.js"
|
||||
|
||||
const checkParams = (params, mo) => {
|
||||
let o = {opts: {}, err: null}
|
||||
if ((mo.mandatory.length !== 0) && (params === undefined)) {
|
||||
return returnOnError(o, ERR.NOPARAMETER, checkParams.name )
|
||||
}
|
||||
for (let p of mo.mandatory) {
|
||||
if (!(p.name in params)) {
|
||||
return returnOnError(o, ERR.NOMANDPARAM.replace('xx', p.name), checkParams.name )
|
||||
}
|
||||
if (p.type === 'int') {
|
||||
let x = parseInt(params[p.name])
|
||||
if (isNaN(x)) {
|
||||
return returnOnError(o, ERR.PARAMNONUM, checkParams.name )
|
||||
} else {
|
||||
o.opts[p.name] = x
|
||||
continue
|
||||
}
|
||||
} else if (p.type === 'float') {
|
||||
let x = parseFloat(params[p.name])
|
||||
if (isNaN(x)) {
|
||||
return returnOnError(o, ERR.PARAMNONUM, checkParams.name )
|
||||
} else {
|
||||
o.opts[p.name] = x
|
||||
continue
|
||||
}
|
||||
}
|
||||
o.opts[p.name] = params[p.name]
|
||||
}
|
||||
for(let p of mo.optional) {
|
||||
if (!(p.name in params)) {
|
||||
o.opts[p.name] = p.default
|
||||
} else {
|
||||
if (p.type === 'int') {
|
||||
let x = parseInt(params[p.name])
|
||||
if (isNaN(x)) {
|
||||
o.opts[p.name] = p.default
|
||||
} else {
|
||||
o.opts[p.name] = x
|
||||
}
|
||||
} else if (p.type === 'bool') {
|
||||
o.opts[p.name] = params[p.name] === 'true'
|
||||
} else {
|
||||
o.opts[p.name] = params[p.name]
|
||||
}
|
||||
}
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
export default checkParams
|
||||
Reference in New Issue
Block a user