generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #13 from gentlementlegen/feat/schema-validation
feat: schema validation
- Loading branch information
Showing
9 changed files
with
252 additions
and
82 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,58 @@ | ||
name: "Update Configuration" | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
|
||
jobs: | ||
update: | ||
name: "Update Configuration in manifest.json" | ||
runs-on: ubuntu-latest | ||
permissions: write-all | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Setup node | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20.10.0" | ||
|
||
- name: Install deps and run configuration update | ||
run: | | ||
yarn install --immutable --immutable-cache --check-cache | ||
yarn tsc --noCheck --project tsconfig.json | ||
- name: Update manifest configuration using GitHub Script | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { pluginSettingsSchema } = require('./src/types/plugin-input'); | ||
const manifestPath = path.resolve("${{ github.workspace }}", './manifest.json'); | ||
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); | ||
const configuration = JSON.stringify(pluginSettingsSchema); | ||
manifest["configuration"] = JSON.parse(configuration); | ||
const updatedManifest = JSON.stringify(manifest, null, 2) | ||
console.log('Updated manifest:', updatedManifest); | ||
fs.writeFileSync(manifestPath, updatedManifest); | ||
- name: Commit and Push generated types | ||
run: | | ||
git config --global user.name 'ubiquity-os[bot]' | ||
git config --global user.email 'ubiquity-os[bot]@users.noreply.github.com' | ||
git add ./manifest.json | ||
if [ -n "$(git diff-index --cached --name-only HEAD)" ]; then | ||
git commit -m "chore: updated generated configuration" || echo "Lint-staged check failed" | ||
git push origin HEAD:${{ github.ref_name }} | ||
else | ||
echo "No changes to commit" | ||
fi | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
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 |
---|---|---|
@@ -1,11 +1,25 @@ | ||
{ | ||
"name": "Query user", | ||
"description": "Queries a user and retrieves its related information, such as the wallet, the label access control, or the current XP.", | ||
"ubiquity:listeners": [ "issue_comment.created" ], | ||
"ubiquity:listeners": [ | ||
"issue_comment.created" | ||
], | ||
"commands": { | ||
"query": { | ||
"ubiquity:example": "/query @ubiquibot", | ||
"description": "Returns the user's wallet, access, and multiplier information." | ||
} | ||
}, | ||
"configuration": { | ||
"type": "object", | ||
"properties": { | ||
"allowPublicQuery": { | ||
"default": true, | ||
"type": "boolean" | ||
} | ||
}, | ||
"required": [ | ||
"allowPublicQuery" | ||
] | ||
} | ||
} | ||
} |
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,37 @@ | ||
import { TransformDecodeCheckError, TransformDecodeError, Value, ValueError } from "@sinclair/typebox/value"; | ||
import { Env, envConfigValidator, envSchema } from "../types/env"; | ||
import { CommandQuerySettings, pluginSettingsSchema, commandQueryUserSchemaValidator } from "../types/plugin-input"; | ||
|
||
export function validateAndDecodeSchemas(env: Env, rawSettings: object) { | ||
const errors: ValueError[] = []; | ||
const settings = Value.Default(pluginSettingsSchema, rawSettings) as CommandQuerySettings; | ||
|
||
if (!commandQueryUserSchemaValidator.test(settings)) { | ||
for (const error of commandQueryUserSchemaValidator.errors(settings)) { | ||
console.error(error); | ||
errors.push(error); | ||
} | ||
} | ||
|
||
if (!envConfigValidator.test(env)) { | ||
for (const error of envConfigValidator.errors(env)) { | ||
console.error(error); | ||
errors.push(error); | ||
} | ||
} | ||
|
||
if (errors.length) { | ||
throw { errors }; | ||
} | ||
|
||
try { | ||
const decodedEnv = Value.Decode(envSchema, env); | ||
const decodedSettings = Value.Decode(pluginSettingsSchema, settings); | ||
return { decodedEnv, decodedSettings }; | ||
} catch (e) { | ||
if (e instanceof TransformDecodeCheckError || e instanceof TransformDecodeError) { | ||
throw { errors: [e.error] }; | ||
} | ||
throw e; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { Type as T, StaticDecode } from "@sinclair/typebox"; | ||
import { StandardValidator } from "typebox-validators"; | ||
|
||
export const envSchema = T.Object({ | ||
SUPABASE_URL: T.String(), | ||
SUPABASE_KEY: T.String(), | ||
}); | ||
|
||
export const envConfigValidator = new StandardValidator(envSchema); | ||
|
||
export type Env = StaticDecode<typeof envSchema>; |
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
Oops, something went wrong.