-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
60 lines (54 loc) · 1.55 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as serverless from 'serverless-http'
import * as express from 'express'
import {getFlowStructureErrors, IContainer} from '@floip/flow-runner'
const app = express()
// parse application/x-www-form-urlencoded
const urlParser = express.urlencoded({ extended: false })
// parse application/json
const jsonParser = express.json()
app.post('/validate', jsonParser, function (req, res) {
res.set('Access-Control-Allow-Origin', '*')
const containerJson = req.body as IContainer
validate(containerJson, req, res)
})
app.post('/validateForm', urlParser, function (req, res) {
res.set('Access-Control-Allow-Origin', '*')
try {
const containerJson = JSON.parse(req.body.flow) as IContainer
validate(containerJson, req, res)
}
catch(e) {
res.json({
specification_version: undefined,
result: 'Failed',
errors: [
{
keyword: 'invalid',
dataPath: '/container',
schemaPath: null,
params: [],
propertyName: null,
message: 'Could not parse Flow Container JSON: ' + e.message,
}
]
})
}
})
function validate(container: any, req, res) {
const specVersion = container.specification_version
const errors = getFlowStructureErrors(container)
if(errors == null) {
res.json({
specification_version: container.specification_version,
result: 'Passed',
})
}
else {
res.json({
specification_version: container.specification_version,
result: 'Failed',
errors: errors
})
}
}
export const handler = serverless(app)