Commit 750f2afe authored by Nicolas Biri's avatar Nicolas Biri

Fix assignemnt of undefined on non mandatory value / Remove _name from Enum enumarable properties

parent 4e3ad60a
Pipeline #829 passed with stage
in 40 seconds
......@@ -6,7 +6,7 @@ A part of the JSMF project dedicated to model definition.
## Install
Thanks to npm: `npm install git+ssh://git@git.list.lu:model/jsmf.git#0.9.0`
Thanks to npm: `npm install git+ssh://git@git.list.lu:model/jsmf.git#0.9.1`
## License information
......
{
"name": "jsmf-core",
"version": "0.9.0",
"version": "0.9.1",
"description": "Javascript Modelling Framework",
"main": "src/index.js",
"directories": {
......
......@@ -246,7 +246,7 @@ function createAttribute(o, name, desc) {
function createSetAttribute(o, name, desc) {
Object.defineProperty(o, setName(name),
{value: x => {
if (!desc.type(x) || (desc.mandatory && _.isUndefined(x))) {
if (!desc.type(x) && (desc.mandatory || !_.isUndefined(x))) {
desc.errorCallback(o, name, x)
}
o.__jsmf__.attributes[name] = x
......@@ -366,7 +366,7 @@ function removeName(n) {
}
function prefixedName(pre, n) {
return pre + n[0].toUpperCase() + n.slice(1)
return pre + _.capitalize(n)
}
const onError =
......
......@@ -29,9 +29,9 @@ const conformsTo = require('./Common').conformsTo
function Enum(name, values) {
function jsmfEnum(x) {return _.includes(jsmfEnum, x)}
jsmfEnum.__name = name
Object.defineProperties(jsmfEnum,
{ __jsmf__: {value: {uuid: uuid.v4(), conformsTo: Enum}}
, __name: {value: name}
, getName: {value: getName}
, conformsTo: {value: () => conformsTo(jsmfEnum)}
})
......
......@@ -95,6 +95,22 @@ describe('Class instance', function() {
done()
})
it('throws error when we set undefined value for mandatory attribute', function(done) {
const Foo = new Class('Foo', [], {x: {type: Number, mandatory: true}});
const s = new Foo({x: 12})
function test() {s.x = undefined}
test.should.throw()
done()
})
it('allows undefined value to optional attribute', function(done) {
const Foo = new Class('Foo', [], {x: {type: Number, mandatory: false}});
const s = new Foo({x: 12})
function test() {s.x = undefined}
test.should.not.throw()
done()
})
it('doesn\'t stop on false value', function(done) {
const C = new Class('C', undefined, {a: Boolean, b: Number})
const c = new C({a: false, b: 12})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment