Giter Site home page Giter Site logo

Optional values about envalid HOT 10 CLOSED

af avatar af commented on June 24, 2024 1
Optional values

from envalid.

Comments (10)

SimenB avatar SimenB commented on June 24, 2024 1

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

from envalid.

SimenB avatar SimenB commented on June 24, 2024

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

from envalid.

af avatar af commented on June 24, 2024

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?

from envalid.

LandonSchropp avatar LandonSchropp commented on June 24, 2024

from envalid.

SimenB avatar SimenB commented on June 24, 2024

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

from envalid.

LandonSchropp avatar LandonSchropp commented on June 24, 2024

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

from envalid.

LandonSchropp avatar LandonSchropp commented on June 24, 2024

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.

from envalid.

af avatar af commented on June 24, 2024

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.

from envalid.

af avatar af commented on June 24, 2024

^ 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?

from envalid.

SimenB avatar SimenB commented on June 24, 2024

It looks fine to me 🙂

from envalid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.