bolt
bolt copied to clipboard
Blaze style enum support?
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?
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;
}
If we did decide to support this, maybe:
enum Answer { 'yes', 'no', 'maybe' }
would evaluate to the same thing as above?
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.
+1 for built-in enum support. If statements are much harder to read.
+1 also for built-in enum support.
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?
Is this happening?
Agreed, this would be awesome
Would love to have built in enum support
+1 also for built-in enum support. Thank you team!