question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add enum sugar for prop validators

See original GitHub issue

What problem does this feature solve?

For enum props, it’s common to validate the given value against valid enum values like this:

props: {
  position: {
    type: String,
    validator (val) {
      return ['top', 'right', 'bottom', 'left'].includes(val)
    }
  }
}

What does the proposed API look like?

I think it’ll be handy to provide extra convenience by supporting this:

props: {
  position: {
    type: String,
    validator: ['top', 'right', 'bottom', 'left']
  }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:34
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

82reactions
sirlancelotcommented, Mar 16, 2018

if you want a shorthand, just use an arrow function:

props: {
  position: {
    type: String,
    validator: (val) => ['top', 'right', 'bottom', 'left'].includes(val)
  }
}

Less magic. It’s Just JavaScript.

51reactions
owenallenazcommented, Dec 12, 2018

If the goal is enum, maybe we should be explicit about it?

props: {
  position: {
    type: String,
    enum: ['top', 'right', 'bottom', 'left']
  }
}

It would only checks exact matches against the passed array. In example from graphQL https://graphql.org/learn/schema/#enumeration-types . From the docs it states two of the benefits.

  • Validate that any arguments of this type are one of the allowed values
  • Communicate through the type system that a field will always be one of a finite set of values

I think in this case, both apply to Vue, no?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validations for Enum Types - Baeldung
Learn how to build validations for enums using Java custom annotations.
Read more >
How to Validate Your Data with Custom Validators of Pydantic ...
In practice, this type of simple validation can be achieved with an Enum or a literal type. However, let's focus on the basic...
Read more >
How can I guarantee that my enums definition doesn't change ...
For each entry, create a new local variable whose name starts with ENUM_ or INDEX_ , then the name of the group, then...
Read more >
JavaScript Enums - Mastering JS
JavaScript Enums · Get all allowed enum values: Object.keys(Direction) returns an array ['Up', 'Down', 'Left', 'Right'] · You can modify the enum ......
Read more >
Enum: Not Just a Constant With a Pretty Face - kdgregory.com
The enum keyword was introduced with JDK 1.5 (Java 5), as syntactic sugar for creating a subclass of java.lang.Enum and adding static ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found