New type: ENUM
Implement a new property type - enum (enumeration).
Example model definition:
{
"name": "Person",
"properties": {
"sex": {
"type": "enum",
"values": ["male", "female", "other"]
}
}
}
Example remoting argument definition:
{
accepts: {
arg: 'sex', type: 'enum', values: ['male', 'female', 'other'],
http: { source: 'query' }
}
}
Implementation details:
- support both "string" and "number" as a backing datatype
- [ ] strong-remoting should validate and coerce enumeration types
- [ ] loopback-datasource-juggler should validate property values of enum type
- connectors should map enum type to appropriate database counterpart
- [ ] SQL connectors - see https://github.com/strongloop-internal/scrum-loopback/issues/603
- [ ] MongoDB - see https://github.com/strongloop-internal/scrum-loopback/issues/604
- [ ] loopback-swagger should emit correct json-schema descriptor for enum properties
- [ ] autodiscovery
Instead of introducing a new type, I would prefer to add a enum constraint to model properties, such as:
color: {
type: 'string',
enum: ['red', 'green', 'blue', ...]
}
It's similar as https://github.com/hapijs/joi#anyvalidvalue---aliases-only-equal
Instead of introducing a new type, I would prefer to add a enum constraint to model properties
Sounds good :+1:
+1
FWIW, we already have ENUM validation implemented via MyModel.validatesInclusionOf('gender', {in: ['male', 'female', 'other']});
I guess we only need to map enum property flag to validatesInclusionOf and implement support for enum in strong-remoting.
+1
Would these additions then translate to loopback explorer? It'd be nice to show the allowed values.
+1
:+1:
:+1:
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
The benefit of making this a type instead of a loopback option is that if you need to provision for a secondary access method which isn't via loopback then the enum won't validate the input. Obviously not every use case by far but worth keeping in mind.
+1
Enums are a common pattern; adding enum support - whether as a type or a constraint - would greatly simplify repeated validity checks on parameters.