Introduce a schema for ConfigurationOptions.

This commit illustrates an approach, but is currently
failing the tests.
This commit is contained in:
Federico Builes
2022-06-01 06:36:02 +02:00
parent 7db11574b7
commit db9f724163
5 changed files with 169 additions and 35 deletions
+8 -20
View File
@@ -1,6 +1,6 @@
import * as fs from 'fs'
import YAML from 'yaml'
import * as z from 'zod'
import { ConfigurationOptions, ConfigurationOptionsSchema } from './schemas'
import path from 'path'
export type Severity = "critical" | "high" | "moderate" | "low"
@@ -8,18 +8,11 @@ export type Severity = "critical" | "high" | "moderate" | "low"
export const SEVERITIES = ["critical", "high", "moderate", "low"] as const
export const CONFIG_FILEPATH = "./.github/dep-review.yml"
type ConfigurationOptions = {
fail_on_severity: string,
allow_licenses: Array<string>,
deny_licenses: Array<string>
}
export function readConfigFile(filePath: string = CONFIG_FILEPATH): ConfigurationOptions {
// By default we want to fail on all severities and allow all licenses.
const defaultOptions: ConfigurationOptions = {
fail_on_severity: "low",
allow_licenses: ['all'],
deny_licenses: []
fail_on_severity: 'low',
allow_licenses: []
}
let data
@@ -34,16 +27,11 @@ export function readConfigFile(filePath: string = CONFIG_FILEPATH): Configuratio
}
}
// This is a copy of line 34, not sure why this is failing!
ConfigurationOptionsSchema.parse({ fail_on_severity: 'critical', allow_licenses: ['BSD', 'GPL 2'] })
const values = YAML.parse(data)
const parsed = ConfigurationOptionsSchema.parse(values)
const parsed = z.object({
fail_on_severity: z.enum(SEVERITIES),
allow_licenses: z.array(z.string()),
deny_licenses: z.array(z.string())
})
.partial()
.refine(obj => !(obj.allow_licenses && obj.deny_licenses), "Can't specify both allow_licenses and deny_licenses")
.parse(values)
return <ConfigurationOptions>parsed;
return parsed;
}
+10
View File
@@ -1,4 +1,5 @@
import * as z from 'zod'
import { SEVERITIES } from './config'
export const ChangeSchema = z.object({
change_type: z.enum(['added', 'removed']),
@@ -28,6 +29,15 @@ export const PullRequestSchema = z.object({
head: z.object({ sha: z.string() })
})
export const ConfigurationOptionsSchema = z.object({
fail_on_severity: z.enum(SEVERITIES).default("low"),
allow_licenses: z.array(z.string()).default([]),
deny_licenses: z.array(z.string()).default([])
}).partial()
.refine(obj => !(obj.allow_licenses && obj.deny_licenses), "Can't specify both allow_licenses and deny_licenses")
export const ChangesSchema = z.array(ChangeSchema)
export type Change = z.infer<typeof ChangeSchema>
export type Changes = z.infer<typeof ChangesSchema>
export type ConfigurationOptions = z.infer<typeof ConfigurationOptionsSchema>