Browse files

Generalizing schema.self to schema.reference .

  • Loading branch information...
1 parent d696237 commit 7539f0ff5cbdb482ae1c29f1a8f7eec099ed6a9f @molnarg committed Jul 29, 2012
Showing with 28 additions and 17 deletions.
  1. +1 −1 lib/Schema.js
  2. +27 −16 lib/extensions/Schema.js
View
2 lib/Schema.js
@@ -25,7 +25,7 @@ Schema.prototype = {
}
if (leafs.uncertain.indexOf(Schema.self) !== -1) {
// schema.self needs to be pointed to this schema, and then it must be reset
- Schema.self.set(this.validate)
+ Schema.self.resolve(this)
}
}
View
43 lib/extensions/Schema.js
@@ -1,25 +1,36 @@
var Schema = require('../Schema')
, schema = require('../schema')
-var SelfSchema = module.exports = Schema.extend({
- set : function(target) {
- if (!this.target) this.target = target
- Schema.self = schema.self = new SelfSchema()
+var SchemaReference = Schema.extend({
+ validate : function() {
+ throw new Error('Trying to validate unresolved schema reference.')
},
- publicFunctions : [ 'set' ],
-
- validate : function(instance) {
- return this.target(instance)
- },
-
- generate : function() {
- return this.target.generate()
+ resolve : function(schemaDescriptor) {
+ var schemaObject = Schema.fromJS(schemaDescriptor)
+
+ for (var key in schemaObject) {
+ if (!schemaObject.hasOwnProperty(key)) continue
+
+ this[key] = schemaObject[key]
+ }
+
+ delete this.resolve
},
- toJSON : function() {
- return { '$ref' : this.target.getId() }
- }
+ publicFunctions : [ 'resolve' ]
})
-Schema.self = schema.self = new SelfSchema()
+schema.reference = function(schemaDescriptor) {
+ return new SchemaReference()
+}
+
+function renewing(ref) {
+ ref.resolve = function() {
+ Schema.self = schema.self = renewing(new SchemaReference())
+ return SchemaReference.prototype.resolve.apply(this, arguments)
+ }
+ return ref
+}
+
+Schema.self = schema.self = renewing(new SchemaReference())

0 comments on commit 7539f0f

Please sign in to comment.