Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New type: ENUM #1321

Closed
6 tasks
bajtos opened this issue Apr 21, 2015 · 66 comments
Closed
6 tasks

New type: ENUM #1321

bajtos opened this issue Apr 21, 2015 · 66 comments

Comments

@bajtos
Copy link
Member

bajtos commented Apr 21, 2015

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:

@raymondfeng
Copy link
Member

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

@bajtos
Copy link
Member Author

bajtos commented Apr 21, 2015

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

Sounds good 👍

@violet-day
Copy link

+1

@bajtos
Copy link
Member Author

bajtos commented Apr 22, 2015

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.

@ainesophaur
Copy link

+1

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

@billinghamj
Copy link

+1

7 similar comments
@gausie
Copy link
Contributor

gausie commented Jul 14, 2015

👍

@Bramanga
Copy link

👍

@ezraroi
Copy link

ezraroi commented Aug 30, 2015

+1

@MyNameIsURL
Copy link

+1

@terehov
Copy link

terehov commented Sep 18, 2015

+1

@parvbhullar
Copy link

+1

@sudomf
Copy link

sudomf commented Sep 29, 2015

+1

@geoand
Copy link

geoand commented Sep 29, 2015

+1

4 similar comments
@riteshjagga
Copy link

+1

@ataraxus
Copy link

+1

@g3k0
Copy link

g3k0 commented Oct 21, 2015

+1

@ghost
Copy link

ghost commented Oct 26, 2015

+1

@MarkAndrewJohnson
Copy link

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.

@AndreasPizsa
Copy link

+1

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

@loay loay self-assigned this Nov 5, 2015
@loay loay added the #wip label Nov 13, 2015
@superkhau superkhau added #tob and removed #wip labels Nov 17, 2015
@adrien-candiotti
Copy link

+1

1 similar comment
@yadavji83
Copy link

+1

@chadwithuhc
Copy link

👍

1 similar comment
@joseluisjb1990
Copy link

+1

@superkhau superkhau assigned kjdelisle and unassigned rashmihunt Feb 16, 2017
@kjdelisle kjdelisle removed the p1 label Feb 21, 2017
@phil-hawkins
Copy link

+1

@hoangtrongphuc
Copy link

I have a solution and I'm using it in my project:
In model Game :

color: {
  type: 'string'
}

In game.js file:

module.exports = function(Game) {
  Game.validatesInclusionOf('color', {in: ["hello", "bye"]});
};

@hckhanh
Copy link

hckhanh commented Jul 24, 2017

+100000000

@scippio
Copy link

scippio commented Jul 26, 2017

+1

@kjdelisle
Copy link
Contributor

Feature development on loopback@3.x is winding down as a result of our push towards loopback-next. Given the nature of the new framework and its use of TypeScript, support for things like enums will be much easier to implement going forward rather than on this current version.

@terehov
Copy link

terehov commented Aug 16, 2017

Does it mean that loopback-next is around the corner and there is a clear migration path?

@kjdelisle
Copy link
Contributor

kjdelisle commented Aug 16, 2017

Loopback-next is going into beta soon and will consist of the most core components of the framework. The intention of the new platform is to focus more of our energy on providing a solid foundation, rather than attempting to build every connector and component for everyone.

Migration
There is a repository implementation called the legacy-juggler-bridge that will allow continued use of your JSON model definitions and the existing loopback-datasource-juggler/connectors.

However, there won't necessarily be a direct upgrade path for your application that transforms your existing logic into a loopback@4.x compatible application. I'd strongly encourage reading the wiki (https://github.com/strongloop/loopback-next/wiki); it's still a work-in-progress but combining it with the loopback-next-example repo should give you insight into loopback-next's many potential workflows and designs.

@kjdelisle
Copy link
Contributor

@terehov Sorry, forgot to add your name to the comment. :P

@randomdev-731
Copy link

@kjdelisle Where should I look for enum related model data validations in Loopback 4?

@bajtos
Copy link
Member Author

bajtos commented Jun 3, 2019

Let's move the discussion to loopbackio/loopback-next#3033.

@strongloop strongloop locked as resolved and limited conversation to collaborators Jun 3, 2019
@bajtos
Copy link
Member Author

bajtos commented Jun 3, 2019

For anybody who lands in this issue: if ENUMs are important to you, then please up-vote loopbackio/loopback-next#3033 by adding a 👍 reaction at the top. Please DO NOT post +1 comments, those are not helpful.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests