Giter VIP home page Giter VIP logo

Comments (4)

ealush avatar ealush commented on June 17, 2024 1

Hey @alveshelio, thanks for reaching out.

The short answer to your question is yes, what you want done can be achieved pretty easily with inside, it allows you to check that a value is within an array.

enforce("hello").inside(["hello", "world"]);

The thing is that a typescript enum is not an array, but it transpiles down to an object, so it's not useful just like that. You need to get either the keys or the values:

enum Fruits {
  APPLE = "apple",
  BANANA = "banana",
  CANTELOPE = "cantelope"
}

// ...

// If you need the enum by key:
test('fruit', 'fruit is a key of fruits enum', () => {
  // data.fruit is a key of ["APPLE", "BANANA", "CANTELOPE"]
  enforce(data.fruit).inside(Object.keys(Fruits));
});

// If you need the enum by value:
test('fruit', 'fruit is a value of fruits enum', () => {
  // data.fruit is a value of ["apple", "banana", "cantelope"]
  enforce(data.fruit).inside(Object.values(Fruits));
});

Maybe it's a good idea to add two new matchers keyOf and valueOf`, but until then, these two examples should work for you.

from vest.

alveshelio avatar alveshelio commented on June 17, 2024

Hi @ealush,

Thank you very much for getting back. I think inside() might be enough.

from vest.

MentalGear avatar MentalGear commented on June 17, 2024

Great example, would be nice to have it in the docs re: handling enum/select ! @ealush

from vest.

ealush avatar ealush commented on June 17, 2024

Thanks @MentalGear, added to the docs will be automatically published with the next patch to vest.

Thank you for keeping an eye out for things like this.

from vest.

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.