Skip to content

Commit

Permalink
refactor: improve data validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dy0gu committed Sep 13, 2024
1 parent 9ebddce commit 5a0d7f5
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .commitlintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@commitlint/config-conventional"
}
11 changes: 5 additions & 6 deletions .github/workflows/merge.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
name: merge

on:
pull_request:
types: [opened, edited]
pull_request_target:
types: [opened, reopened, edited, synchronize]

jobs:
# Check if the PR has a title that matches the conventional commit format
title:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
cache: npm
cache-dependency-path: package-lock.json
- name: Install dependencies
run: npm ci
- name: Install commitlint and config-conventional
run: npm install @commitlint/cli @commitlint/config-conventional
- run: echo "${{ github.event.pull_request.title }}" | npx commitlint
22 changes: 0 additions & 22 deletions commitlint.config.ts

This file was deleted.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
},
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@commitlint/cli": "^19.4.0",
"@commitlint/config-conventional": "^19.2.2",
"@types/node": "^22.4.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
Expand Down
6 changes: 3 additions & 3 deletions src/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
ScrollRestoration,
} from "@remix-run/react";

import { metadata } from "~/configs/metadata";
import { metadata } from "~/data/meta";

import { resources } from "~/configs/resources";
import { rels } from "~/data/attributes";

import "~/styles/globals.css";

Expand All @@ -27,7 +27,7 @@ export const meta: MetaFunction<typeof loader> = ({ data }) => {
};

export const links: LinksFunction = () => {
return resources;
return rels;
};

export default function Layout() {
Expand Down
2 changes: 1 addition & 1 deletion src/app/routes/[manifest.webmanifest].tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { info } from "~/configs/resources";
import { info } from "~/data/attributes";

// Dynamic manifest.webmanifest generator route
export const loader = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/routes/_index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMystery } from "~/hooks/mystery";

import { info } from "~/configs/resources";
import { info } from "~/data/attributes";

import { Beam } from "~/components/beam";
import { Hero, name } from "~/components/hero";
Expand Down
10 changes: 6 additions & 4 deletions src/configs/resources.ts → src/data/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ const info = {
name: "Dy0gu",
description: "Just an individual, writing some code on the internet.",
links: [
{ name: "Email", href: "mailto:[email protected]" },
{ name: "GitHub", href: "https://www.github.com/dy0gu" },
{ name: "Stack", href: "https://stackoverflow.com/users/22441488" },
{
name: "Stack Overflow",
href: "https://stackoverflow.com/users/22441488",
},
],
};

// Link tags for the <head> of the root html document
const resources = [
const rels = [
{ rel: "icon", href: "/favicon.svg", type: "image/svg+xml" },
{ rel: "sitemap", href: "/sitemap.xml" },
{ rel: "manifest", href: "/manifest.webmanifest" },
];

export { info, resources };
export { info, rels };
2 changes: 1 addition & 1 deletion src/configs/metadata.ts → src/data/meta.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { env } from "~/utils/env";

import { info } from "~/configs/resources";
import { info } from "~/data/attributes";

// Meta tags for the <head> of the root html document
// This object uses server environment variables, meaning it can only be called in Remix loader functions
Expand Down
35 changes: 21 additions & 14 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,30 @@ for (const key in process.env) {
}
}

// Validate all environment variables
const Environment = z.object({
PORT: z.coerce
.number({
invalid_type_error:
"PORT not correctly defined in environment variables!",
})
.int({ message: "PORT must be an integer!" })
.min(1000, { message: "PORT must be greater than 1000!" })
.max(65535, { message: "PORT must be less than 65535!" }),
DOMAIN: z.string({
required_error: "DOMAIN not correctly defined in environment variables!",
}),
// Schema that should contain all variables meant to be validated
const expected = z.object({
PORT: z.coerce.number().int().min(1000).max(65535),
DOMAIN: z.string(),
CI: z.string().optional(),
});

const env = Environment.parse(normalized);
// Pretty print errors during validation
function parse(input: { [key: string]: string }) {
try {
return expected.parse(input);
} catch (err) {
if (err instanceof z.ZodError) {
for (const issue of err.issues) {
console.error(
`❌ ${issue.message} for ${issue.path} environment variable!`,
);
}
}
process.exit(1);
}
}

const env = parse(normalized);

// Always read environment variables through this object instead of
// directly accessing process.env in order to ensure type safety
Expand Down

0 comments on commit 5a0d7f5

Please sign in to comment.