Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
OGC
node-3DPS
Commits
300497f4
Commit
300497f4
authored
Jul 25, 2021
by
Athanasios
Browse files
refactor validation checks
parent
7c60a607
Changes
2
Hide whitespace changes
Inline
Side-by-side
routes/v1.js
View file @
300497f4
const
express
=
require
(
"
express
"
);
const
{
check
,
validationResult
,
oneOf
}
=
require
(
"
express-validator
"
)
const
{
toLower
}
=
require
(
"
../src/customSanitizers
"
);
const
{
validationResult
}
=
require
(
"
express-validator
"
)
const
{
errorHandler
}
=
require
(
"
../src/errorHandler
"
);
const
checkFactory
=
require
(
"
../src/validationCheckFactory
"
);
const
{
getSceneHandler
}
=
require
(
"
../src/getSceneHandler
"
);
const
{
getCapabilitiesHandler
}
=
require
(
"
../src/getCapabilitiesHandler
"
);
const
redirectTo
=
require
(
"
../src/redirectTo
"
);
const
assets
=
require
(
"
../src/assets
"
);
const
bb
=
require
(
"
../src/boundingbox
"
);
let
router
=
express
.
Router
();
const
checks
=
[
...
checkFactory
.
commonChecks
(),
check
(
"
request
"
)
.
exists
().
withMessage
(
"
MissingParameterValue
"
)
.
customSanitizer
(
toLower
)
.
isIn
([
"
getscene
"
,
"
getcapabilities
"
]).
withMessage
(
"
InvalidParameterValue
"
)
];
const
requestChecks
=
checkFactory
.
getChecksFor
(
"
request
"
);
router
.
route
(
"
/
"
).
get
(
c
hecks
,
(
req
,
res
)
=>
{
router
.
route
(
"
/
"
).
get
(
requestC
hecks
,
(
req
,
res
)
=>
{
const
errors
=
validationResult
(
req
);
if
(
!
errors
.
isEmpty
())
{
...
...
@@ -32,12 +23,7 @@ router.route("/").get(checks, (req, res) => {
});
const
capabilitiesChecks
=
[
check
(
"
request
"
)
.
exists
().
withMessage
(
"
MissingParameterValue
"
)
.
customSanitizer
(
toLower
)
.
equals
(
"
getcapabilities
"
).
withMessage
(
"
InvalidParameterValue
"
)
];
const
capabilitiesChecks
=
checkFactory
.
getChecksFor
(
"
capabilities
"
);
router
.
route
(
"
/capabilities
"
).
get
(
capabilitiesChecks
,
(
req
,
res
)
=>
{
...
...
@@ -52,15 +38,7 @@ router.route("/capabilities").get(capabilitiesChecks, (req, res) => {
});
const
sceneChecks
=
[
check
(
"
boundingbox
"
)
.
exists
().
withMessage
(
"
MissingParameterValue
"
)
.
matches
(
/^
((\-?\d
+
(\.\d
+
)?)
,
){3}(\-?\d
+
(\.\d
+
)?)
$/
).
withMessage
(
"
InvalidParameterValue
"
)
.
custom
(
value
=>
bb
.
isValid
(
value
)).
withMessage
(
"
InvalidParameterValue
"
),
check
(
"
layers
"
)
.
exists
().
withMessage
(
"
MissingParameterValue
"
)
.
custom
(
value
=>
assets
.
allLayersExist
(
value
)).
withMessage
(
"
UnknownLayer
"
)
];
const
sceneChecks
=
checkFactory
.
getChecksFor
(
"
scene
"
);
router
.
route
(
"
/scene
"
).
get
(
sceneChecks
,
(
req
,
res
)
=>
{
...
...
src/validationCheckFactory.js
View file @
300497f4
const
{
check
}
=
require
(
"
express-validator
"
);
const
{
toLower
}
=
require
(
"
../src/customSanitizers
"
);
const
{
check
,
query
}
=
require
(
"
express-validator
"
);
const
{
toLower
,
toUpper
,
boundingboxIsValid
}
=
require
(
"
../src/customSanitizers
"
);
const
assets
=
require
(
"
../src/assets
"
);
const
bb
=
require
(
"
../src/boundingbox
"
);
const
commonChecks
=
()
=>
{
return
[
...
...
@@ -16,25 +15,46 @@ const commonChecks = () => {
}
const
specific
Checks
=
(
parameter
)
=>
{
const
get
Checks
For
=
(
parameter
)
=>
{
switch
(
parameter
.
toLowerCase
())
{
case
"
getscene
"
:
{
case
"
request
"
:
{
return
[
check
(
"
boundingbox
"
)
...
commonChecks
(),
query
(
"
request
"
)
.
exists
().
withMessage
(
"
MissingParameterValue
"
)
.
matches
(
/^
((\-?\d
+
(\.\d
+
)?)
,
){3}(\-?\d
+
(\.\d
+
)?)
$/
).
withMessage
(
"
InvalidParameterValue
"
)
.
custom
(
value
=>
bb
.
isValid
(
value
)).
withMessage
(
"
InvalidParameterValue
"
),
check
(
"
layers
"
)
.
customSanitizer
(
toLower
)
.
isIn
([
"
getscene
"
,
"
getcapabilities
"
]).
withMessage
(
"
InvalidParameterValue
"
)
];
}
case
"
scene
"
:
{
return
[
query
(
"
crs
"
)
.
exists
().
withMessage
(
"
MissingParameterValue
"
)
.
custom
(
value
=>
assets
.
allLayersExist
(
value
)).
withMessage
(
"
UnknownLayer
"
)
.
customSanitizer
(
toUpper
)
.
matches
(
/^EPSG:
\d{4,5}
$/
).
withMessage
(
"
InvalidParameterValue
"
),
query
(
"
layers
"
)
.
exists
().
withMessage
(
"
MissingParameterValue
"
)
.
custom
(
value
=>
assets
.
allLayersExist
(
value
)).
withMessage
(
"
UnknownLayer
"
),
query
(
"
boundingbox
"
)
.
optional
()
.
matches
(
/^
((\-?\d
+
(\.\d
+
)?)
,
){3}(\-?\d
+
(\.\d
+
)?)
$/
).
withMessage
(
"
InvalidParameterValue
"
)
.
custom
(
value
=>
boundingboxIsValid
(
value
)).
withMessage
(
"
InvalidParameterValue
"
)
];
}
case
"
getcapabilities
"
:
{
return
[];
case
"
capabilities
"
:
{
return
[
query
(
"
request
"
)
.
exists
().
withMessage
(
"
MissingParameterValue
"
)
.
customSanitizer
(
toLower
)
.
equals
(
"
getcapabilities
"
).
withMessage
(
"
InvalidParameterValue
"
)
];
}
}
}
module
.
exports
=
{
commonChecks
,
specificChecks
};
\ No newline at end of file
module
.
exports
=
{
getChecksFor
};
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment