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

Optional values #37

Closed
LandonSchropp opened this issue Jun 16, 2017 · 10 comments
Closed

Optional values #37

LandonSchropp opened this issue Jun 16, 2017 · 10 comments
Assignees

Comments

@LandonSchropp
Copy link

I would like to validate an environment variable, but allow it to optionally be undefined. I know I can do something like this:

MY_AWESOME_VARIABLE: envalid.str({
  default: '',
  choices: ['astonishing', 'awe-inspiring', 'magnificent', 'wonderous'],
  desc: 'My awesome variable'
})

However, it seems more precise to have the value of MY_AWESOME_VARIABLE be undefined (since it's not defined). Is there a way to achieve this with Envalid? Something like this would be great:

MY_AWESOME_VARIABLE: envalid.str({
  required: false,
  choices: ['astonishing', 'awe-inspiring', 'magnificent', 'wonderous'],
  desc: 'My awesome variable'
})

Thanks!

@SimenB
Copy link
Collaborator

SimenB commented Jun 17, 2017

SGTM. If @af wants it, I can send a PR for it

@af
Copy link
Owner

af commented Jun 17, 2017

Rather than add a new option, I think it'd be better to support setting default: undefined, which would be the same as required: false. Right now setting default: undefined will trigger a validation error, but we should be able to change it so that passing undefined explicitly works this way instead. Thoughts?

@LandonSchropp
Copy link
Author

LandonSchropp commented Jun 17, 2017 via email

@SimenB
Copy link
Collaborator

SimenB commented Jun 17, 2017

We can't detect if undefined is passed explicitly or left out. So either special casing null or a new option is the way to go

@SimenB
Copy link
Collaborator

SimenB commented Jun 17, 2017

(we could do Object.keys and count, but that feels way too fragile)

@LandonSchropp
Copy link
Author

default: null also seems like a fine solution to me.

@LandonSchropp
Copy link
Author

You could use Object#hasOwnProperty to detect if the property is defined on the object, but that breaks if the user passes in config that inherits from a prototype. I'm not sure why somebody would do that, but I don't see a reason to prevent it.

@af
Copy link
Owner

af commented Jun 18, 2017

Yeah I was thinking about hasOwnProperty(); I believe if ('default' in spec) should also work for these purposes. The latter is a bit more readable to me. There is the prototype gotcha, but I can't think of a case where anyone would use a prototype-based config, so I'm comfortable with going this way.

af added a commit that referenced this issue Jun 21, 2017
@af
Copy link
Owner

af commented Jun 21, 2017

^ the commit above is a stab at how I was thinking this would work. It fails a test, plus I want to add new test(s) to cover the new use case, so it's not ready for prime-time yet. Could someone take a look and let me know if there's anything obviously wrong with this approach?

@SimenB
Copy link
Collaborator

SimenB commented Jun 21, 2017

It looks fine to me 🙂

af added a commit that referenced this issue Jul 27, 2017
@af af self-assigned this Aug 1, 2017
@af af closed this as completed in #48 Aug 23, 2017
tuannm151 pushed a commit to BSSCommerce/shopify-envalid that referenced this issue Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants