Skip to content

Commit

Permalink
feature: mmsi and uuid validate
Browse files Browse the repository at this point in the history
Validate mmsi to be 9 digits and uuid to have the right format.
Require either mmsi or uuid to be set.
Validation added to 'vessel' endpoint that admin UI
uses so that admin UI shows the error message.
  • Loading branch information
tkurki committed May 29, 2022
1 parent 8b7e780 commit 060f9e0
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/serverroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const defaultSecurityStrategy = './tokensecurity'
const skPrefix = '/signalk/v1'
import { SERVERROUTESPREFIX } from './constants'

const MMSIPATTERN = /^\d{9}$/
const UUIDPATTERN = /^urn:mrn:signalk:uuid:[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-4[0-9A-Fa-f]{3}-[89ABab][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}$/

module.exports = function(app, saveSecurityConfig, getSecurityConfig) {
let securityWasEnabled
let restoreFilePath
Expand Down Expand Up @@ -637,10 +640,27 @@ module.exports = function(app, saveSecurityConfig, getSecurityConfig) {
})
}

app.put(`${SERVERROUTESPREFIX}/vessel`, (req, res, next) => {
app.put(`${SERVERROUTESPREFIX}/vessel`, (req, res) => {
console.log(JSON.stringify(req.body, null, 2))
const de = app.config.baseDeltaEditor
let vessel = req.body

if (!vessel.mmsi && !vessel.uuid) {
res.status(400)
res.send(`You must provide either mmsi or uuid`)
return
}
if (vessel.mmsi && !vessel.mmsi.match(MMSIPATTERN)) {
res.status(400)
res.send(`MMSI must be a number with 9 digits`)
return
}
if (vessel.uuid && !vessel.uuid.match(UUIDPATTERN)) {
res.status(400)
res.send(`Uuid must be a v4 UUID with prefix urn:mrn:signalk:uuid:`)
return
}

de.setSelfValue('name', vessel.name)
app.config.vesselName = vessel.name
de.setSelfValue('mmsi', vessel.mmsi)
Expand Down

0 comments on commit 060f9e0

Please sign in to comment.