-
Notifications
You must be signed in to change notification settings - Fork 181
/
validateInputs.js
78 lines (69 loc) · 2.08 KB
/
validateInputs.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { pickBy } from 'ramda'
import {
get,
keys,
resolve,
contains,
forEach,
mapObjIndexed,
forEachObjIndexed,
merge,
pick,
append,
prop
} from '@serverless/utils'
import ramlValidate from 'raml-validate'
import chalk from 'chalk'
const validate = ramlValidate()
const validateInputs = (Type, inputs) => {
const inputTypes = get('props.inputTypes', Type)
if (!inputTypes) {
return inputs
}
// we currently only support validating core types, not registry types
const coreInputTypes = pickBy(
(value) =>
contains(value.type, [
'boolean',
'string',
'number',
'integer',
'object',
'nil',
'datetime',
'date-only',
'time-only',
'datetime-only',
'file'
]),
inputTypes
)
const coreInputs = mapObjIndexed((value) => resolve(value), pick(keys(coreInputTypes), inputs))
const schema = validate({ ...coreInputTypes }, 'RAML10')
const validation = schema({ ...coreInputs })
if (!validation.valid) {
let errorMessages = []
const typeName = chalk.white(`"${Type.props.name}"`)
const header = chalk.redBright.bold(`\ninputType error(s) in Type ${typeName}:\n`)
errorMessages = append(header, errorMessages)
forEach((error) => {
const value = chalk.cyanBright(`"${error.value}"`)
const key = `${chalk.yellowBright(error.key)}`
const suppliedInputType = typeof error.value
const msg = ` - inputType ${key} has invalid \`${suppliedInputType}\` value of ${value} according to the rule: ${chalk.yellowBright(
error.rule
)} ${chalk.yellowBright(error.attr)}.\n`
errorMessages = append(msg, errorMessages)
}, prop('errors', validation))
const message = errorMessages.join('')
throw Error(message)
}
// set defaults if any...
forEachObjIndexed((coreInputType, key) => {
if (!coreInputs[key] && !coreInputType.required && coreInputType.default !== undefined) {
coreInputs[key] = coreInputType.default
}
}, coreInputTypes)
return merge(inputs, coreInputs)
}
export default validateInputs