Use ajv to validate schema

This commit is contained in:
khai96_ 2020-05-09 19:45:31 +07:00
parent bb24f595c2
commit e373fffa0a
5 changed files with 29 additions and 9 deletions

View file

@ -1,6 +1,9 @@
import { getInput, InputOptions } from '@actions/core'
import process from 'process'
import { getInput, error, InputOptions } from '@actions/core'
import expandTilde from 'expand-tilde'
import { safeLoad } from 'js-yaml'
import Ajv from 'ajv'
import runInstallSchema from './run-install-input.schema.json'
interface RunInstall {
readonly recursive?: boolean
@ -30,6 +33,17 @@ const parseInputPath = (name: string) => expandTilde(getInput(name, options))
function parseRunInstall(name: string): RunInstall[] {
const result: RunInstallInput = safeLoad(getInput(name, options))
const ajv = new Ajv({
allErrors: true,
async: false,
})
const validate = ajv.compile(runInstallSchema)
if (!validate(result)) {
for (const errorItem of validate.errors!) {
error(`${errorItem.dataPath}: ${errorItem.message}`)
}
return process.exit(1)
}
if (!result) return []
if (result === true) return [{ recursive: true }]
if (Array.isArray(result)) return result

View file

@ -3,7 +3,16 @@
"instruction": {
"compilerOptions": {
"strict": true,
"esModuleInterop": true
"target": "ES2018",
"lib": [
"ES2018",
"ES2019",
"ES2020",
"ESNext"
],
"moduleResolution": "Node",
"esModuleInterop": true,
"resolveJsonModule": true
},
"input": "index.ts",
"symbol": "RunInstallInput",