Skip to content

Commit

Permalink
From now on, toJSON must call its parent method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gábor Molnár committed Jul 5, 2012
1 parent e32b27a commit 34f6526
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 40 deletions.
12 changes: 6 additions & 6 deletions lib/extensions/Array.js
Expand Up @@ -66,15 +66,15 @@ ArraySchema.prototype = _.extend(Object.create(schema.prototype), {
},

toJSON : function() {
var schema = { type : 'array' }
var json = schema.prototype.toJSON.call(this)

if (this.min > 0 ) schema.minItems = this.min
if (this.max < Infinity) schema.maxItems = this.max
if (this.itemSchema !== anything) schema.items = this.itemSchema.toJSON()
json.tpye = 'array'

if (this.id) schema.id = this.id
if (this.min > 0) json.minItems = this.min
if (this.max < Infinity) json.maxItems = this.max
if (this.itemSchema !== anything) json.items = this.itemSchema.toJSON()

return schema
return json
}
})

Expand Down
18 changes: 9 additions & 9 deletions lib/extensions/Number.js
Expand Up @@ -111,23 +111,23 @@ NumberSchema.prototype = _.extend(Object.create(schema.prototype), {
},

toJSON : function() {
var step = this.divisibleBy
, integer = ( step !== 0 && step === Math.floor(step) )
, schema = { type : integer ? 'integer' : 'number' }
var json = json.prototype.toJSON.call(this)

if (step !== 0 && step !== 1) schema.divisibleBy = step
json.type = ( this.divisibleBy !== 0 && this.divisibleBy % 1 === 0 ) ? 'integer' : 'number'

if (this.divisibleBy !== 0 && this.divisibleBy !== 1) json.divisibleBy = this.divisibleBy

if (this.minimum !== -Infinity) {
schema.minimum = this.minimum
if (this.exclusiveMinimum === true) schema.exclusiveMinimum = true
json.minimum = this.minimum
if (this.exclusiveMinimum === true) json.exclusiveMinimum = true
}

if (this.maximum !== Infinity) {
schema.maximum = this.maximum
if (this.exclusiveMaximum === true) schema.exclusiveMaximum = true
json.maximum = this.maximum
if (this.exclusiveMaximum === true) json.exclusiveMaximum = true
}

return schema
return json
}
})

Expand Down
2 changes: 1 addition & 1 deletion lib/patterns/equality.js
Expand Up @@ -33,7 +33,7 @@ EqualitySchema.prototype = _.extend(Object.create(schema.prototype), {
},

toJSON : function() {
return { 'enum' : [this.object] }
return _.extend(schema.prototype.toJSON.call(this), { 'enum' : [this.object] })
}
})

Expand Down
6 changes: 3 additions & 3 deletions lib/patterns/object.js
Expand Up @@ -143,7 +143,9 @@ ObjectSchema.prototype = _.extend(Object.create(schema.prototype), {
},

toJSON : function() {
var i, property, regexp, json = { type : 'object' }
var i, property, regexp, json = schema.prototype.toJSON.call(this)

json.type = 'object'

for (i in this.stringProps) {
property = this.stringProps[i]
Expand All @@ -166,8 +168,6 @@ ObjectSchema.prototype = _.extend(Object.create(schema.prototype), {
json.additionalProperties = (this.other === nothing) ? false : this.other.toJSON()
}

if (this.id) json.id = this.id

return json
}
})
Expand Down
30 changes: 15 additions & 15 deletions lib/patterns/or.js
Expand Up @@ -36,23 +36,23 @@ OrSchema.prototype = _.extend(Object.create(schema.prototype), {
},

toJSON : function() {
var jsons = this.schemas.map(schema.toJSON)
var json = schema.prototype.toJSON.call(this)
, subjsons = this.schemas.map(schema.toJSON)
, onlyEquality = subjsons.every(function(json) {
return json['enum'] instanceof Array && json['enum'].length === 1
})

var onlyEquality = true
for (var i = 0; i < jsons.length; i++) {
if (!(jsons[i]['enum'] instanceof Array && jsons[i]['enum'].length === 1)) {
onlyEquality = false
break
}
}
if (onlyEquality) return { 'enum' : jsons.map(function(json) { return json['enum'][0] }) }

var json = { 'type' : jsons.map(function(json) {
var simpleType = typeof json.type === 'string' && Object.keys(json).length === 1
return simpleType ? json.type : json
})}
if (onlyEquality) {
json['enum'] = subjsons.map(function(json) {
return json['enum'][0]
})

if (this.id) schema.id = this.id
} else {
json['type'] = subjsons.map(function(json) {
var simpleType = typeof json.type === 'string' && Object.keys(json).length === 1
return simpleType ? json.type : json
})
}

return json
}
Expand Down
2 changes: 1 addition & 1 deletion lib/patterns/reference.js
Expand Up @@ -17,7 +17,7 @@ ReferenceSchema.prototype = _.extend(Object.create(schema.prototype), {
},

toJSON : function() {
return { 'enum' : [this.value] }
return _.extend(schema.prototype.toJSON.call(this), { 'enum' : [this.value] })
}
})

Expand Down
11 changes: 6 additions & 5 deletions lib/patterns/regexp.js
Expand Up @@ -24,15 +24,16 @@ RegexpSchema.prototype = _.extend(Object.create(schema.prototype), {
},

toJSON : function() {
var sch = { type : 'string' }
var json = schema.prototype.toJSON.call(this)

json.type = 'string'

if (this.regexp) {
console.log(this.regexp.toString())
sch.pattern = this.regexp.toString()
sch.pattern = sch.pattern.substr(1, sch.pattern.length - 2)
json.pattern = this.regexp.toString()s
json.pattern = json.pattern.substr(1, json.pattern.length - 2)
}

return sch
return json
}
})

Expand Down
8 changes: 8 additions & 0 deletions lib/schema.js
Expand Up @@ -73,6 +73,14 @@ schema.prototype = {
}

return this.validator
},

toJSON : function() {
var json = {}

if (this.id != null) json.id = this.id

return json
}
}

Expand Down

0 comments on commit 34f6526

Please sign in to comment.