bolt icon indicating copy to clipboard operation
bolt copied to clipboard

Blaze style enum support?

Open kabriel opened this issue 9 years ago • 10 comments

Trying to convert my Blaze rules into Bolt rules and I don't see any 1:1 support for enums. Is that planned? or do I need to do these as complex rules?

kabriel avatar Jan 26 '16 22:01 kabriel

Blaze enums allow you to specify string values that are limited to one of several pre-defined options.

We don't have direct support for enums in Bolt. Rather than being strictly declarative, Bolt uses a validate() expression to confirm that data conforms to a set of constraints.

So, where in Blaze you could say:

schema:
  type: string
  enum: [yes, no, maybe]

in Bolt you could define an extension of the String datatype with the same effect:

type Answer extends String {
  validate() { this == 'yes' || this == 'no' || this == 'maybe' }
}

or, more concisely - using the regular expression matcher:

type Answer extends String {
  validate() { this.test(/^yes|no|maybe$/) }
}

You would then use the type in another type (or path), like this:

type Survey {
  name: String;
  answer: Answer;
}

mckoss avatar Jan 26 '16 23:01 mckoss

If we did decide to support this, maybe:

enum Answer { 'yes', 'no', 'maybe' }

would evaluate to the same thing as above?

mckoss avatar Jan 26 '16 23:01 mckoss

Thanks for the response. I like your idea for how to support it. Will go with validate() logic for now.

// Kabe

On Jan 26, 2016, at 3:58 PM, Mike Koss [email protected] wrote:

If we did decide to support this, maybe:

enum Answer { 'yes', 'no', 'maybe' } would evaluate to the same thing as above?

— Reply to this email directly or view it on GitHub.

kabriel avatar Jan 27 '16 00:01 kabriel

+1 for built-in enum support. If statements are much harder to read.

mbleigh avatar Jan 28 '16 02:01 mbleigh

+1 also for built-in enum support.

gdestura avatar Aug 01 '16 23:08 gdestura

i'm still wrapping my brain around bolt, but is it possible to derive a set of enum values from a JS obj/array?

Right now for client side validation i have a simple array of permissible values. using answer above:

const answerTypes = ['YES', 'NO', 'MAYBE']

it'd be great if i could use that const and write a validation rule like this:

type Answer extends String {
  validate() { this.test(/^answerTypes.join('|')$/) }
}

It looks like this is not feasible in bolt. has anyone accomplished anything similar, though?

brandonmp avatar Sep 26 '16 21:09 brandonmp

Is this happening?

benjick avatar Apr 18 '17 11:04 benjick

Agreed, this would be awesome

Kluskey avatar May 11 '17 02:05 Kluskey

Would love to have built in enum support

RyanWarner avatar Sep 08 '17 04:09 RyanWarner

+1 also for built-in enum support. Thank you team!

ghost avatar May 19 '18 02:05 ghost