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

Add a Config.nonEmptyString constructor #3515

Closed
titouancreach opened this issue Aug 29, 2024 · 0 comments
Closed

Add a Config.nonEmptyString constructor #3515

titouancreach opened this issue Aug 29, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@titouancreach
Copy link
Contributor

titouancreach commented Aug 29, 2024

What is the problem this feature would solve?

Most of the time, empty string is not a valid configuration, it often comes from a .env file not filled in by the user (file that comes from .env.example or something)

# Redirect the emails to this user
EMAIL_REDIRECT=

DB_URL=

Some other packages (like https://env.t3.gg/docs/core) have an option to treat empty string as undefined.

I believe this case is common enough to be a part of the Effect repo.

What is the feature you are proposing to solve the problem?

Add a new constructor:

const dbUrl = yield* Config. stringNonEmpty("DB_URL");

What alternatives have you considered?

Making my own function like the one @tim-smart did:

import { Config, ConfigError, Either, Redacted } from "effect"

export const nonEmptyString = (name: string) =>
  Config.string(name).pipe(
    Config.mapOrFail(_ => {
      const trimmed = _.trim()
      return trimmed !== ""
        ? Either.right(trimmed)
        : Either.left(ConfigError.MissingData([], "must not be empty"))
    }),
  )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants