loopback icon indicating copy to clipboard operation
loopback copied to clipboard

New type: ENUM

Open bajtos opened this issue 11 years ago • 66 comments

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

bajtos avatar Apr 21 '15 10:04 bajtos

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

raymondfeng avatar Apr 21 '15 15:04 raymondfeng

Instead of introducing a new type, I would prefer to add a enum constraint to model properties

Sounds good :+1:

bajtos avatar Apr 21 '15 16:04 bajtos

+1

violet-day avatar Apr 22 '15 08:04 violet-day

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.

bajtos avatar Apr 22 '15 09:04 bajtos

+1

Would these additions then translate to loopback explorer? It'd be nice to show the allowed values.

ainesophaur avatar Jun 09 '15 02:06 ainesophaur

+1

billinghamj avatar Jul 13 '15 12:07 billinghamj

:+1:

gausie avatar Jul 14 '15 10:07 gausie

:+1:

Bramanga avatar Jul 23 '15 14:07 Bramanga

+1

ezraroi avatar Aug 30 '15 07:08 ezraroi

+1

MyNameIsURL avatar Sep 03 '15 18:09 MyNameIsURL

+1

terehov avatar Sep 18 '15 15:09 terehov

+1

parvbhullar avatar Sep 21 '15 23:09 parvbhullar

+1

sudomf avatar Sep 29 '15 02:09 sudomf

+1

geoand avatar Sep 29 '15 07:09 geoand

+1

riteshjagga avatar Oct 09 '15 15:10 riteshjagga

+1

ataraxus avatar Oct 12 '15 17:10 ataraxus

+1

g3k0 avatar Oct 21 '15 17:10 g3k0

+1

ghost avatar Oct 26 '15 19:10 ghost

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.

MarkAndrewJohnson avatar Oct 27 '15 02:10 MarkAndrewJohnson

+1

Enums are a common pattern; adding enum support - whether as a type or a constraint - would greatly simplify repeated validity checks on parameters.

andreaspitzer avatar Oct 28 '15 11:10 andreaspitzer