Permalink
Browse files

Exporting Schema classes to Schema.patterns and Schema.extensions.

  • Loading branch information...
1 parent a64e4ee commit b28b64cf9a1bdb4bb355ddcf13c73c798fa866f5 @molnarg committed Aug 18, 2012
View
3 lib/Schema.js
@@ -115,3 +115,6 @@ Schema.fromJS.def = Array.prototype.push.bind(fromJSdefs)
var fromJSONdefs = []
Schema.fromJSON = session(lastDefinedResult.bind(null, fromJSONdefs))
Schema.fromJSON.def = Array.prototype.push.bind(fromJSONdefs)
+
+Schema.patterns = {}
+Schema.extensions = {}
View
4 lib/extensions/Array.js
@@ -1,8 +1,8 @@
var Schema = require('../Schema')
, EqualitySchema = require('../patterns/equality')
- , anything = require('../patterns/anything')
+ , anything = require('../patterns/anything').instance
-var ArraySchema = module.exports = Schema.extend({
+var ArraySchema = module.exports = Schema.extensions.ArraySchema = Schema.extend({
initialize : function(itemSchema, max, min) {
this.itemSchema = itemSchema || anything
this.min = min || 0
View
2 lib/extensions/Boolean.js
@@ -1,6 +1,6 @@
var Schema = require('../Schema')
-var BooleanSchema = new Schema.extend({
+var BooleanSchema = module.exports = Schema.extensions.BooleanSchema = new Schema.extend({
validate : function(instance) {
return Object(instance) instanceof Boolean
},
View
2 lib/extensions/Number.js
@@ -1,6 +1,6 @@
var Schema = require('../Schema')
-var NumberSchema = module.exports = Schema.extend({
+var NumberSchema = module.exports = Schema.extensions.NumberSchema = Schema.extend({
initialize : function(minimum, exclusiveMinimum, maximum, exclusiveMaximum, divisibleBy) {
this.minimum = minimum != null ? minimum : -Infinity
this.exclusiveMinimum = exclusiveMinimum
View
2 lib/extensions/Schema.js
@@ -1,7 +1,7 @@
var Schema = require('../Schema')
, schema = require('../schema')
-var SchemaReference = Schema.extend({
+var SchemaReference = module.exports = Schema.extensions.SchemaReference = Schema.extend({
validate : function() {
throw new Error('Trying to validate unresolved schema reference.')
},
View
6 lib/patterns/anything.js
@@ -1,7 +1,7 @@
var Schema = require('../Schema')
- , nothing = require('./nothing')
+ , nothing = require('./nothing').instance
-var AnythingSchema = Schema.extend({
+var AnythingSchema = module.exports = Schema.patterns.AnythingSchema = Schema.extend({
validate : function(instance) {
return instance != null
},
@@ -16,7 +16,7 @@ var AnythingSchema = Schema.extend({
}
})
-var anything = module.exports = new AnythingSchema()
+var anything = AnythingSchema.instance = new AnythingSchema()
Schema.fromJS.def(function(sch) {
if (sch === undefined) return anything
View
2 lib/patterns/class.js
@@ -1,6 +1,6 @@
var Schema = require('../Schema')
-var ClassSchema = module.exports = Schema.extend({
+var ClassSchema = module.exports = Schema.patterns.ClassSchema = Schema.extend({
initialize : function(constructor) {
this.constructor = constructor
},
View
2 lib/patterns/equality.js
@@ -20,7 +20,7 @@ var equal = function(a, b) {
return true
}
-var EqualitySchema = module.exports = Schema.extend({
+var EqualitySchema = module.exports = Schema.patterns.EqualitySchema = Schema.extend({
initialize : function(object) {
this.object = object
},
View
6 lib/patterns/nothing.js
@@ -1,6 +1,6 @@
var Schema = require('../Schema')
-var NothingSchema = Schema.extend({
+var NothingSchema = module.exports = Schema.patterns.NothingSchema = Schema.extend({
validate : function(instance) {
return instance == null
},
@@ -14,9 +14,7 @@ var NothingSchema = Schema.extend({
}
})
-global.NothingSchema = NothingSchema
-
-var nothing = module.exports = new NothingSchema()
+var nothing = NothingSchema.instance = new NothingSchema()
Schema.fromJS.def(function(sch) {
if (sch === null) return nothing
View
6 lib/patterns/object.js
@@ -1,9 +1,9 @@
var Schema = require('../Schema')
, RandExp = require('randexp')
- , anything = require('./anything')
- , nothing = require('./nothing')
+ , anything = require('./anything').instance
+ , nothing = require('./nothing').instance
-var ObjectSchema = module.exports = Schema.extend({
+var ObjectSchema = module.exports = Schema.patterns.ObjectSchema = Schema.extend({
initialize : function(properties, other) {
var self = this
View
2 lib/patterns/or.js
@@ -1,7 +1,7 @@
var Schema = require('../Schema')
, EqualitySchema = require('../patterns/equality')
-var OrSchema = module.exports = Schema.extend({
+var OrSchema = module.exports = Schema.patterns.OrSchema = Schema.extend({
initialize : function(schemas) {
this.schemas = schemas
},
View
2 lib/patterns/reference.js
@@ -1,6 +1,6 @@
var Schema = require('../Schema')
-var ReferenceSchema = module.exports = Schema.extend({
+var ReferenceSchema = module.exports = Schema.patterns.ReferenceSchema = Schema.extend({
initialize : function(value) {
this.value = value
},
View
2 lib/patterns/regexp.js
@@ -3,7 +3,7 @@ var Schema = require('../Schema')
var defaultRandExp = new RandExp(/^[a-zA-Z0-9]{1,10}$/)
-var RegexpSchema = module.exports = Schema.extend({
+var RegexpSchema = module.exports = Schema.patterns.RegexpSchema = Schema.extend({
initialize : function(regexp) {
this.regexp = regexp
this.randexp = this.regexp ? new RandExp(this.regexp) : defaultRandExp
View
2 lib/schema.js
@@ -25,6 +25,8 @@ var schema = module.exports = function(schemaDescription) {
}
}
+schema.Schema = Schema
+
schema.toJSON = function(sch) {
return Schema.fromJS(sch).toJSON()
}

0 comments on commit b28b64c

Please sign in to comment.