Implement a new property type - enum (enumeration).
Example model definition:
@model()
class Person extends Entity {
@property({
type: 'string',
enum: ['male', 'female', 'other'],
})
gender: string;
}
This issue is a replacement for https://github.com/strongloop/loopback/issues/1321 that's tracking ENUM feature request in LoopBack 3.x
Areas to cover:
string and number as a backing datatypecreate and updateAttributesenum constraint to the discovered definitions where applicableenum and include the constraint in the schema produced@loopback/rest should validate ENUM constraints for input parameters, see https://github.com/strongloop/loopback-next/issues/1624I'm currently looking for a way to validate properties on my model of a string type to only be one of an allowed value. This would be a great help as I'm having trouble finding the "loopback way" to do it. My current solution was to iterate over an enum with a custom interceptor... But I don't really like that approach.
any update about this feature?
Works if you put the enum in jsonSchema object
@model()
class Person extends Entity {
@property({
type: 'string',
jsonSchema: {
enum: ['male', 'female', 'other'],
}
})
gender: string;
}
Works if you put the enum in jsonSchema object
@zzhenryquezz Take care in using this though as it may not be validating input. See comment.
Also please use the voting system and refrain from creating comments that add no value to conversation such as +1. FYI - This just creates noise in notification feeds.
Take care in using this though as it may not be validating input. See comment.
@dougal83 I check on my side with the enum defined at property level (decorator) and this works perfectly (validation at controller level at least and spec generated). Enum as to be specified in a specific way tho
enum QueryLanguage {
JSON = 'json',
SQL = 'sql',
}
// ...
@property({
type: 'string',
required: true,
jsonSchema: {
enum: Object.values(QueryLanguage),
},
})
queryLanguage: QueryLanguage;
@dougal83 I check on my side with the enum defined at property level (decorator) and this works perfectly (validation at controller level at least and spec generated). Enum as to be specified in a specific way tho
enum QueryLanguage { JSON = 'json', SQL = 'sql', } // ... @property({ type: 'string', required: true, jsonSchema: { enum: Object.values(QueryLanguage), }, }) queryLanguage: QueryLanguage;
Yep, this is how I use here too. If you pass a normal enum will not work, probably because the jsonSchema expects an array of string or number but the enum of Typescript is converted to an object.
But this way works well for me.
@agnes512 @nabdelgadir Let's document this.
@zzhenryquezz @raymondfeng
Works if you put the enum in jsonSchema object
@model() class Person extends Entity { @property({ type: 'string', jsonSchema: { enum: ['male', 'female', 'other'], } }) gender: string; }I have an issue with this approach. lb4 inlines this jsonSchema fragment into both
PersonandPersonWithRelationsschemas. Which in turn causes theopenapi-generatorto create 2 separate enums in typescript code therefore makingPersonandPersonWithRelationstypes on the client side not compatible.
Is there a way to extract this piece into a reusable ref or tell lb4 to makePersonWithRelationsextendPersonschema?
~how would we handle an array of enums using the above example/workaround?~ figured that out 馃檪
Most helpful comment
@agnes512 @nabdelgadir Let's document this.