0x/lib/validate.js
Matteo Collina eeb0f2d2c6
Updated deps and added CI (#246)
* Updated deps and added CI

* fixes
2021-12-30 12:31:29 +01:00

43 lines
1.1 KiB
JavaScript

'use strict'
const Ajv = require('ajv')
const ajv = new Ajv()
module.exports = (schema) => {
return validate
function validate (args) {
const onPortFn = typeof args.onPort === 'function' && args.onPort
if (onPortFn) delete args.onPort
const privateProps = {
workingDir: { type: 'string' }
}
const valid = ajv.compile({
...schema,
properties: { ...schema.properties, ...privateProps }
}
)
if (valid(args)) {
if (onPortFn) args.onPort = onPortFn
return
}
const [{ keyword, dataPath, params, message }] = valid.errors
if (keyword === 'type') {
const flag = dataPath.substr(
1,
dataPath[dataPath.length - 1] === ']'
? dataPath.length - 2
: dataPath.length - 1
)
const dashPrefix = flag.length === 1 ? '-' : '--'
throw Error(`The ${dashPrefix}${flag} option ${message}\n`)
}
if (keyword === 'additionalProperties') {
const flag = params.additionalProperty
const dashPrefix = flag.length === 1 ? '-' : '--'
throw Error(`${dashPrefix}${flag} is not a recognized flag\n`)
}
}
}