Skip to content

Commit

Permalink
Fixed a bug with inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
moshegottlieb committed May 19, 2020
1 parent 71d350b commit 832f34d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-json-object",
"version": "0.2.7",
"version": "0.2.8",
"description": "JSON typescript runtime validation",
"main": "dist/src/index.js",
"scripts": {
Expand Down
18 changes: 13 additions & 5 deletions src/json_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class JSONObject extends Object{
}
let json_keys = JSONObject.jsonKeys(this)
if (!json_keys) return // no properties defined
for (let key of JSONObject.jsonKeys(this)){
for (let key of json_keys){
let json_value:any
let json_key:string
// Is there a mapping this value?
Expand Down Expand Up @@ -190,16 +190,24 @@ export class JSONObject extends Object{
}

private static jsonKeys(target:any){
return Reflect.getMetadata(__keys,target)
let ret = Array<string>()
// Get the keys, all the way to the base class
while (target = target.__proto__){
let i = Reflect.getMetadata(__keys,target,target.constructor.name)
if (i){
ret = ret.concat(i)
}
}
return ret
}

private static preprocess(target:any,key:string){
let keys:Array<string>
if (Reflect.hasMetadata(__keys,target)){
keys = Reflect.getMetadata(__keys,target)
if (Reflect.hasMetadata(__keys,target,target.constructor.name)){
keys = Reflect.getMetadata(__keys,target,target.constructor.name)
} else {
keys = Array<string>()
Reflect.defineMetadata(__keys,keys,target)
Reflect.defineMetadata(__keys,keys,target,target.constructor.name)
}
if (!keys.length || keys[keys.length - 1] != key){
keys.push(key)
Expand Down
7 changes: 7 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,16 @@ let test = new Test([
a:number
}
class Sub extends Base {
x:number
@required
b:number
}
class Sub2 extends Base {
@required
a2:number
@map('x')
b:string
}
let s = new Sub({a:1, b:2})
assert.ok(s.a == 1)
assert.ok(s.b == 2)
Expand Down

0 comments on commit 832f34d

Please sign in to comment.