Skip to content

Commit

Permalink
added check on members field in the examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
pcan committed Aug 26, 2016
1 parent 434b608 commit 7469736
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
12 changes: 7 additions & 5 deletions inspect-interface/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class MyClass {
field: Date;
}

for (let member of MyInterface.members) {
let memberDescription = `Member ${member.name} is ${member.type.kind}`;
if (member.type.kind === 'interface' || member.type.kind === 'class') {
memberDescription += ' - name: ' + (<NamedType>member.type).name;
if (MyInterface.members) { // members may be undefined if there are no members.
for (let member of MyInterface.members) {
let memberDescription = `Member ${member.name} is ${member.type.kind}`;
if (member.type.kind === 'interface' || member.type.kind === 'class') {
memberDescription += ' - name: ' + (<NamedType>member.type).name;
}
console.log(memberDescription);
}
console.log(memberDescription);
}
4 changes: 4 additions & 0 deletions json-unmarshalling/src/unmarshaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function convert(obj: any, targetClass: Class) {
// since recursion will take care of the deep copy.
Object.setPrototypeOf(obj, targetClass.getConstructor().prototype);

if (!targetClass.members) {
return; // current class has no members.
}

// get all fields of targetClass that are classes or reference to types that may be arrays.
let fields = targetClass.members.filter(m => m.type.kind === "class" || m.type.kind === "array");

Expand Down
21 changes: 11 additions & 10 deletions simple-validator/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
// check if an object matches an interface, recursively.
export function isValid(object: any, interfaceObj: Interface): boolean {

for (let member of interfaceObj.members) {
if (isPrimitive(member.type) && object[member.name] != null && member.type.kind != typeof object[member.name]) {
// found a primitive type field in the interface, but not in the object
console.log(`Field ${member.name} should be ${member.type.kind} but it is ${typeof object[member.name]}`)
return false;
} else if (member.type.kind === 'interface' && object[member.name] != null) {
// recursively check field type against the corresponding interface
return isValid(object[member.name], <Interface>member.type);
if (interfaceObj.members) { // members may be undefined if there are no members.
for (let member of interfaceObj.members) {
if (isPrimitive(member.type) && object[member.name] != null && member.type.kind != typeof object[member.name]) {
// found a primitive type field in the interface, but not in the object
console.log(`Field ${member.name} should be ${member.type.kind} but it is ${typeof object[member.name]}`)
return false;
} else if (member.type.kind === 'interface' && object[member.name] != null) {
// recursively check field type against the corresponding interface
return isValid(object[member.name], <Interface>member.type);
}
// TODO: check class types...
}
// TODO: check class types...
}

return true;
}

Expand Down

0 comments on commit 7469736

Please sign in to comment.