Skip to content

feat: allow users to define environment shape with zod #57

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

Closed
wants to merge 1 commit into from

Conversation

neolectron
Copy link
Member

Problem

Modules aren't encapsulated, they should have a schema to define the shape of the environment they need.

Solution

Allow modules to export a zodSchema to validate env.

@neolectron neolectron marked this pull request as draft September 5, 2023 03:30
@neolectron
Copy link
Member Author

neolectron commented Sep 5, 2023

I think one of the blocker is that we do not have a module-creator.

I tried several things, and in order to get all the keys inside the env object in a module, I need to declare my modules as satisfies BotModule which can be annoying for modules dev.

Then the solution was to have a function like getEnv() that would be typesafe and return the requested environment variables. I couldn't do it simply.

Therefore I would consider putting this on blocked until we have the module-creator.
I picture something like :

export const myModule = createModule((ctx) => ({
  env: {
	MY_ENV_VAR: z.string().optionnal()
  },
  storage: {
  	channelList: z.string().array()
  },
  slashCommands: {/*...*/},
  eventHandlers: {
  	messageCreate: async () => {
		// Here I would be able to use ctx.env and ctx.storage
		console.log(ctx.storage.channelList) // typesafe + can be validated
  	}
  },
}));

@neolectron neolectron added the blocked blocked by another PR label Sep 5, 2023
@neolectron neolectron mentioned this pull request Sep 5, 2023
3 tasks
@neolectron
Copy link
Member Author

deprecated by #81

@neolectron neolectron closed this Sep 13, 2023
@potb potb deleted the feat/validate-env branch September 13, 2023 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked blocked by another PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant