Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: schema validation #127

Draft
wants to merge 5 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/validate-schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: "Validate Schema"

on:
workflow_dispatch:
inputs:
stateId:
description: "State Id"
eventName:
description: "Event Name"
eventPayload:
description: "Event Payload"
settings:
description: "Settings"
authToken:
description: "Auth Token"
ref:
description: "Ref"

jobs:
validate:
name: "Validate Schema"
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 validation
run: |
yarn install --immutable --immutable-cache --check-cache
yarn tsx src/validate-schema.ts
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
X25519_PRIVATE_KEY: ${{ secrets.X25519_PRIVATE_KEY }}
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
NFT_MINTER_PRIVATE_KEY: ${{ secrets.NFT_MINTER_PRIVATE_KEY }}
NFT_CONTRACT_ADDRESS: ${{ secrets.NFT_CONTRACT_ADDRESS }}
PERMIT_FEE_RATE: ${{ secrets.PERMIT_FEE_RATE }}
PERMIT_TREASURY_GITHUB_USERNAME: ${{ secrets.PERMIT_TREASURY_GITHUB_USERNAME }}
PERMIT_ERC20_TOKENS_NO_FEE_WHITELIST: ${{ secrets.PERMIT_ERC20_TOKENS_NO_FEE_WHITELIST }}
43 changes: 43 additions & 0 deletions src/helpers/validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { TransformDecodeCheckError, TransformDecodeError, Value, ValueError } from "@sinclair/typebox/value";
import {
IncentivesConfiguration,
incentivesConfigurationSchema,
validateIncentivesConfiguration,
} from "../configuration/incentives";
import envConfigSchema, { EnvConfigType, envValidator } from "../types/env-type";

export function validateAndDecodeSchemas(rawEnv: object, rawSettings: object) {
const errors: ValueError[] = [];

const env = Value.Default(envConfigSchema, rawEnv) as EnvConfigType;
if (!envValidator.test(env)) {
for (const error of envValidator.errors(env)) {
console.error(error);
errors.push(error);
}
}

const settings = Value.Default(incentivesConfigurationSchema, rawSettings) as IncentivesConfiguration;
if (!validateIncentivesConfiguration.test(settings)) {
for (const error of validateIncentivesConfiguration.errors(settings)) {
console.error(error);
errors.push(error);
}
}

if (errors.length) {
throw { errors };
}

try {
const decodedSettings = Value.Decode(incentivesConfigurationSchema, settings);
const decodedEnv = Value.Decode(envConfigSchema, rawEnv || {});
return { decodedEnv, decodedSettings };
} catch (e) {
console.error("validateAndDecodeSchemas", e);
if (e instanceof TransformDecodeCheckError || e instanceof TransformDecodeError) {
throw { errors: [e.error] };
}
throw e;
}
}
3 changes: 3 additions & 0 deletions src/types/env-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Type, Static } from "@sinclair/typebox";
import { StandardValidator } from "typebox-validators";

const envConfigSchema = Type.Object({
SUPABASE_URL: Type.String(),
Expand All @@ -15,4 +16,6 @@ const envConfigSchema = Type.Object({

export type EnvConfigType = Static<typeof envConfigSchema>;

export const envValidator = new StandardValidator(envConfigSchema);

export default envConfigSchema;
47 changes: 47 additions & 0 deletions src/validate-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import { Octokit } from "@octokit/rest";
import { validateAndDecodeSchemas } from "./helpers/validator";

export async function returnDataToKernel(
repoToken: string,
stateId: string,
output: object,
eventType = "return_data_to_ubiquibot_kernel"
) {
const octokit = new Octokit({ auth: repoToken });
return octokit.repos.createDispatchEvent({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
event_type: eventType,
client_payload: {
state_id: stateId,
output: JSON.stringify(output),
},
});
}

async function main() {
const payload = github.context.payload.inputs;

validateAndDecodeSchemas(process.env, JSON.parse(payload.settings));
return { errors: [], payload };
}

main()
.then((payload) => {
console.log("Configuration validated.");
return payload;
})
.catch((errors) => {
console.error("Failed to validate configuration", errors);
core.setFailed(errors);
return errors;
})
.then(async (errors) => {
const payload = github.context.payload.inputs;
await returnDataToKernel(process.env.GITHUB_TOKEN, payload.stateId, errors, "configuration_validation");
})
.catch((e) => {
console.error("Failed to return the data to the kernel.", e);
});
Loading