Skip to content

Commit ffcaa26

Browse files
committed
improve validation of items in collaction
1 parent 9677a90 commit ffcaa26

File tree

7 files changed

+78
-78
lines changed

7 files changed

+78
-78
lines changed

bower.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"homepage": "https://designfirst.io/systemruntime/",
77
"author": {
88
"name": "erwan carriou",
9-
"email": "[email protected]",
10-
"twitter": "@ecarriou"
9+
"email": "[email protected]"
1110
},
1211
"repository": {
1312
"type": "git",

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
"main": "./src/runtime.js",
2424
"author": {
2525
"name": "erwan carriou",
26-
"email": "[email protected]",
27-
"twitter": "@ecarriou"
26+
"email": "[email protected]"
2827
},
2928
"license": "Apache-2.0",
3029
"repository": {
@@ -60,10 +59,10 @@
6059
"karma": "^2.0.2",
6160
"karma-chai": "^0.1.0",
6261
"karma-chrome-launcher": "^2.2.0",
63-
"karma-coverage": "^1.1.1",
62+
"karma-coverage": "^1.1.2",
6463
"karma-mocha": "^1.3.0",
6564
"karma-script-launcher": "^1.0.0",
66-
"load-grunt-tasks": "^3.5.2",
65+
"load-grunt-tasks": "^4.0.0",
6766
"mocha": "^5.1.1"
6867
}
6968
}

src/behavior.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,34 +129,34 @@ function createFunction(name, func, core, useCoreAPI) {
129129
// kludge for Babel
130130
funcBody = funcBody.replace(/_this/g, 'this');
131131

132-
if (params[0] === '') {
133-
params = [];
132+
if (paramsClean[0] === '') {
133+
paramsClean = [];
134134
}
135135

136136
if (useCoreAPI) {
137-
params.push('$component');
138-
params.push('$db');
139-
params.push('$metamodel');
140-
params.push('$workflow');
141-
params.push('$behavior');
142-
params.push('$state');
143-
params.push('$log');
144-
params.push('$helper');
137+
paramsClean.push('$component');
138+
paramsClean.push('$db');
139+
paramsClean.push('$metamodel');
140+
paramsClean.push('$workflow');
141+
paramsClean.push('$behavior');
142+
paramsClean.push('$state');
143+
paramsClean.push('$log');
144+
paramsClean.push('$helper');
145145
}
146146

147147
if ($helper.isOnNode()) {
148-
params.push('require');
148+
paramsClean.push('require');
149149
}
150150

151-
if (params[0] !== '') {
151+
if (paramsClean[0] !== '') {
152152
action = new Function(
153153
'__action',
154154
'return function ' +
155155
functionName +
156156
' (' +
157-
params.join(', ') +
157+
paramsClean.join(', ') +
158158
") { return new Function('" +
159-
params.join("', '") +
159+
paramsClean.join("', '") +
160160
"', __action).apply(this, arguments) };"
161161
)(funcBody);
162162
} else {

src/component.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,7 @@ function _Array(conf) {
173173
data: [val, 'add']
174174
});
175175
} else {
176-
if (typeof val.id !== 'undefined') {
177-
$log.invalidPropertyName(id, classId, propertyName, val.id(), type);
178-
} else {
179-
$log.invalidPropertyName(id, classId, propertyName, val, type);
180-
}
176+
$log.invalidPropertyName(id, classId, propertyName, val, type);
181177
}
182178
} else {
183179
if (val && $metamodel.isValidType(val, type)) {
@@ -1043,7 +1039,7 @@ function addProperties(model, Class, classId) {
10431039
this.constructor.name,
10441040
propertyName,
10451041
value,
1046-
propertyType
1042+
propertyType[0]
10471043
);
10481044
}
10491045
}
@@ -1445,15 +1441,15 @@ function addStructure(path, name, model, id) {
14451441
$workflow.process({
14461442
component: id,
14471443
state: fullPath.replace(/\[(\d)*\]/g, ''),
1448-
data: [arr, 'add']
1444+
data: [value, 'add']
14491445
});
14501446
}
14511447

14521448
// current element
14531449
$workflow.process({
14541450
component: id,
14551451
state: fullPath,
1456-
data: [arr, 'add']
1452+
data: [value, 'add']
14571453
});
14581454
}
14591455
} else {

src/log.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -664,21 +664,6 @@ exports.invalidType = function invalidType(name, value, typeName) {
664664
);
665665
};
666666

667-
/**
668-
* @method invalidClassType
669-
* @param {Object} obj an object
670-
* @param {String} type expected class type
671-
* @description Invalid class type
672-
*/
673-
exports.invalidClassType = function invalidClassType(obj, type) {
674-
getLogger().warn(
675-
"invalid class name for the object '" +
676-
JSON.stringify(obj) +
677-
"': expected '" +
678-
type
679-
);
680-
};
681-
682667
/**
683668
* @method invalidConfiguration
684669
* @param {Object} obj an object

src/metamodel.js

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,36 +1838,6 @@ exports.isValidType = function isValidType(value, typeName) {
18381838
return result;
18391839
}
18401840

1841-
/**
1842-
* @method _checkClassName
1843-
* @param {String} value
1844-
* @param {String} typeName
1845-
* @returns {Boolean} the object is compliant with the type
1846-
* @private
1847-
* @description Check if an object is compliant with a class
1848-
*/
1849-
function _checkClassName(value, typeName) {
1850-
var isValid = false;
1851-
var typeRef = getRealClassName(typeName);
1852-
var component = value;
1853-
1854-
if (value !== '' && value !== null) {
1855-
if (hasType(value, 'string')) {
1856-
component = $component.get(value);
1857-
}
1858-
if (getClassName(component) === typeRef && component && component.id) {
1859-
isValid = true;
1860-
}
1861-
} else {
1862-
isValid = true;
1863-
}
1864-
1865-
if (!isValid) {
1866-
$log.invalidClassType(value, typeName);
1867-
}
1868-
return isValid;
1869-
}
1870-
18711841
/**
18721842
* @method _isValidType
18731843
* @param {String} value
@@ -1896,7 +1866,12 @@ exports.isValidType = function isValidType(value, typeName) {
18961866
isValid = checkCustomSchema(value[i], typeName[0]);
18971867
break;
18981868
case exports.isClassName(typeName[0]):
1899-
isValid = _checkClassName(value[i], typeName[0]);
1869+
if (value[i] !== '' && value[i] !== null) {
1870+
isValid = exports.inheritFrom(
1871+
getClassName(value[i]),
1872+
typeName[0]
1873+
);
1874+
}
19001875
break;
19011876
default:
19021877
isValid = hasType(value[i], typeName[0]);
@@ -1930,7 +1905,9 @@ exports.isValidType = function isValidType(value, typeName) {
19301905
}
19311906
break;
19321907
case exports.isClassName(typeName):
1933-
result = _checkClassName(value, typeName);
1908+
if (value !== '' && value !== null) {
1909+
result = exports.inheritFrom(getClassName(value), typeName);
1910+
}
19341911
break;
19351912
default:
19361913
result = _isValidType(value, typeName);
@@ -2305,7 +2282,6 @@ exports.isValidObject = function isValidObject(
23052282

23062283
if (!hasType(comp, 'undefined')) {
23072284
if (!exports.inheritFrom(comp.constructor.name, typeRef)) {
2308-
// if (getClassName(comp) !== typeRef) { uncomment this line for a strict mode
23092285
isValid = false;
23102286
$log.invalidType(fieldName, field, typeRef);
23112287
} else {

test/runtime/component-spec.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('a System Runtime component', function () {
2323
'fullName': 'method',
2424
'testMethod': 'method',
2525
'children': 'collection',
26+
'teacher': 'link',
2627
'father': 'link',
2728
'moving': 'event'
2829
});
@@ -77,6 +78,12 @@ describe('a System Runtime component', function () {
7778
'mandatory': false,
7879
'default': {}
7980
},
81+
'teacher': {
82+
'type': 'Person',
83+
'readOnly': false,
84+
'mandatory': false,
85+
'default': {}
86+
},
8087
'address': {
8188
'type': 'string',
8289
'readOnly': false,
@@ -487,7 +494,7 @@ describe('a System Runtime component', function () {
487494
expect(yoda.birthDate().toISOString()).equal(now.toISOString());
488495
});
489496

490-
it('can add a link to another components', function () {
497+
it('can add a link to another component', function () {
491498
const Person = runtime.require('Person');
492499

493500
const anakin = new Person({
@@ -505,7 +512,26 @@ describe('a System Runtime component', function () {
505512
expect(leia.father().firstName()).equal('Anakin');
506513
});
507514

508-
it('can remove a link to another components', function () {
515+
it('can add a link to another component that inherits from the valid class', function () {
516+
const Person = runtime.require('Person');
517+
const Teacher = runtime.require('Teacher');
518+
519+
const luke = new Person({
520+
'firstName': 'Luke',
521+
'lastName': 'Skywalker'
522+
});
523+
524+
const yoda = new Teacher({
525+
'firstName': 'Yoda',
526+
'lastName': 'Master'
527+
});
528+
529+
luke.teacher(yoda);
530+
531+
expect(luke.teacher()).to.not.be.undefined;
532+
});
533+
534+
it('can remove a link to another component', function () {
509535
const Person = runtime.require('Person');
510536

511537
const anakin = new Person({
@@ -656,6 +682,25 @@ describe('a System Runtime component', function () {
656682
anakin.children([]);
657683

658684
expect(anakin.children().length).equal(0);
685+
}); const Teacher = runtime.require('Teacher');
686+
687+
it('can add an item in a collection that inherits from the valid class', function () {
688+
const Person = runtime.require('Person');
689+
const Teacher = runtime.require('Teacher');
690+
691+
const vador = new Person({
692+
'firstName': 'Dark',
693+
'lastName': 'Vador'
694+
});
695+
696+
const luke = new Teacher({
697+
'firstName': 'Yoda',
698+
'lastName': 'Master'
699+
});
700+
701+
vador.children(0, luke);
702+
703+
expect(vador.children(0)).to.not.be.undefined;
659704
});
660705

661706
it('can destroy itself', function () {

0 commit comments

Comments
 (0)