diff --git a/lib/model/Device.js b/lib/model/Device.js index c8f6db84a..d55b120d4 100644 --- a/lib/model/Device.js +++ b/lib/model/Device.js @@ -23,6 +23,9 @@ const mongoose = require('mongoose'); const Schema = mongoose.Schema; +const Group = require('./Group'); + +mongoose.Schema.Types.ExplicitAttrsType = Group.ExplicitAttrsType; const Device = new Schema({ id: String, @@ -49,7 +52,7 @@ const Device = new Schema({ internalAttributes: Object, autoprovision: Boolean, expressionLanguage: String, - explicitAttrs: Boolean, + explicitAttrs: Group.ExplicitAttrsType, ngsiVersion: String, cache: Boolean }); diff --git a/lib/model/Group.js b/lib/model/Group.js index 10b48c0bd..f440fbc22 100644 --- a/lib/model/Group.js +++ b/lib/model/Group.js @@ -24,6 +24,23 @@ const mongoose = require('mongoose'); const Schema = mongoose.Schema; +class ExplicitAttrsType extends mongoose.SchemaType { + constructor(key, options) { + super(key, options, 'ExplicitAttrsType'); + } + // `cast()` takes a parameter that can be anything. You need to + // validate the provided `val` and throw a `CastError` if you + // can't convert it. + cast(val) { + if (!(typeof val === 'boolean' || typeof val === 'string')) { + throw new Error('ExplicitAttrsType: ' + val + ' is not Boolean or String'); + } + return val; + } +} + +mongoose.Schema.Types.ExplicitAttrsType = ExplicitAttrsType; + const Group = new Schema({ url: String, resource: String, @@ -43,9 +60,10 @@ const Group = new Schema({ internalAttributes: Array, autoprovision: Boolean, expressionLanguage: String, - explicitAttrs: Boolean, + explicitAttrs: ExplicitAttrsType, defaultEntityNameConjunction: String, ngsiVersion: String, + entityNameExp: String, cache: Boolean }); @@ -56,3 +74,4 @@ function load(db) { } module.exports.load = load; +module.exports.ExplicitAttrsType = ExplicitAttrsType;