generated from ubiquity-os/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from gentlementlegen/feat/schema-validation
feat: added schema validation workflow
- Loading branch information
Showing
4 changed files
with
72 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: "Validate Schema" | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
settings: | ||
description: "Settings" | ||
|
||
jobs: | ||
validate: | ||
name: "Validate Schema" | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Setup node | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20.10.0" | ||
|
||
- name: Install dependencies | ||
run: | | ||
yarn install --immutable --immutable-cache --check-cache | ||
yarn tsx src/validate-schema.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Value } from "@sinclair/typebox/value"; | ||
import { envSchema, envValidator, PluginSettings, pluginSettingsSchema, pluginSettingsValidator } from "../types"; | ||
|
||
export function validateAndDecodeSchemas(env: object, rawSettings: object) { | ||
if (!envValidator.test(env)) { | ||
const errors: object[] = []; | ||
for (const error of envValidator.errors(env)) { | ||
const errorMessage = { path: error.path, message: error.message, value: error.value }; | ||
console.error(errorMessage); | ||
errors.push(errorMessage); | ||
} | ||
throw new Error(`Invalid environment provided. ${errors}`); | ||
} | ||
const envDecoded = Value.Decode(envSchema, env || {}); | ||
|
||
const settings = Value.Default(pluginSettingsSchema, rawSettings) as PluginSettings; | ||
if (!pluginSettingsValidator.test(settings)) { | ||
const errors: object[] = []; | ||
for (const error of pluginSettingsValidator.errors(settings)) { | ||
const errorMessage = { path: error.path, message: error.message, value: error.value }; | ||
console.error(errorMessage); | ||
errors.push(errorMessage); | ||
} | ||
throw new Error(`Invalid settings provided. ${errors}`); | ||
} | ||
|
||
const settingsDecoded = Value.Decode(pluginSettingsSchema, settings); | ||
|
||
return { envDecoded, settingsDecoded }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as github from "@actions/github"; | ||
import { validateAndDecodeSchemas } from "./helpers/validator"; | ||
|
||
function main() { | ||
const payload = github.context.payload.inputs; | ||
|
||
payload.env = { ...(payload.env || {}), workflowName: github.context.workflow }; | ||
const decodedSchemas = validateAndDecodeSchemas(payload.env, JSON.parse(payload.settings)); | ||
console.log(decodedSchemas); | ||
} | ||
|
||
main(); |