Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Stripping whitespace everywhere.

  • Loading branch information...
commit 5f125cfce6b760013da8764b20d8cb0f4d20349d 1 parent 5fd4a7f
@molnarg authored
View
24 lib/Schema.js
@@ -7,28 +7,28 @@ Schema.prototype = {
wrap : function() {
if (this.wrapped) return this.validate
this.wrapped = true
-
+
var publicFunctions = [ 'toJSON', 'generate', 'unwrap' ]
publicFunctions = publicFunctions.concat(this.publicFunctions || [])
-
+
for (var i = 0; i < publicFunctions.length; i++) {
if (!this[publicFunctions[i]]) continue
this.validate[publicFunctions[i]] = this[publicFunctions[i]].bind(this)
}
-
+
return this.validate
},
-
+
unwrap : function() {
return this
},
-
+
toJSON : session(function(makeReference) {
var json, session = Schema.session
-
+
// Initializing session if it isnt
if (!session.serialized) session.serialized = { objects: [], jsons: [], ids: [] }
-
+
var index = session.serialized.objects.indexOf(this)
if (makeReference && index !== -1) {
// This was already serialized, returning a JSON schema reference ($ref)
@@ -95,16 +95,16 @@ Schema.extend = function(descriptor) {
if (!descriptor.validate) {
throw new Error('Schema objects must have a validate function.')
}
-
+
var constructor = function() {
if (this.initialize) this.initialize.apply(this, arguments)
-
+
this.validate = this.validate.bind(this)
-
+
this.validate.schema = this.validate
}
-
+
constructor.prototype = _.extend(Object.create(Schema.prototype), descriptor)
-
+
return constructor
}
View
28 lib/extensions/Array.js
@@ -9,59 +9,59 @@ var ArraySchema = module.exports = Schema.extend({
this.min = min || 0
this.max = max || Infinity
},
-
+
validate : function(instance) {
// Instance must be an instance of Array
if (!(instance instanceof Array)) return false
-
+
// Checking length
if (this.min === this.max) {
if (instance.length !== this.min) return false
-
+
} else {
if (this.min > 0 && instance.length < this.min) return false
if (this.max < Infinity && instance.length > this.max) return false
}
-
+
// Checking conformance to the given item schema
for (var i = 0; i < instance.length; i++) {
if (!this.itemSchema.validate(instance[i])) return false;
}
-
+
return true
},
-
+
generate : function() {
var array = []
-
+
while (array.length < this.min || (array.length < this.max && Math.random() < 0.666)) {
array.push(this.itemSchema.generate())
}
-
+
return array
},
-
+
toJSON : Schema.session(function() {
var json = Schema.prototype.toJSON.call(this, true)
if (json['$ref'] != null) return json
-
+
json.tpye = 'array'
-
+
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 json
})
})
Schema.fromJSON.def(function(sch) {
if (!sch || sch.type !== 'array') return
-
+
// Tuple typing is not yet supported
if (sch.items instanceof Array) return
-
+
return new ArraySchema(Schema.fromJSON(sch.items), sch.maxItems, sch.minItems)
})
View
4 lib/extensions/Boolean.js
@@ -4,7 +4,7 @@ var BooleanSchema = new Schema.extend({
validate : function(instance) {
return Object(instance) instanceof Boolean
},
-
+
generate : function() {
return Math.random() < 0.5
},
@@ -18,7 +18,7 @@ var booleanSchema = module.exports = new BooleanSchema().wrap()
Schema.fromJSON.def(function(sch) {
if (!sch || sch.type !== 'boolean') return
-
+
return booleanSchema
})
View
30 lib/extensions/Number.js
@@ -8,7 +8,7 @@ var NumberSchema = module.exports = Schema.extend({
this.exclusiveMaximum = exclusiveMaximum
this.divisibleBy = divisibleBy || 0
},
-
+
min : function(minimum) {
return new NumberSchema( minimum, false
, this.maximum, this.exclusiveMaximum
@@ -43,7 +43,7 @@ var NumberSchema = module.exports = Schema.extend({
, divisibleBy
).wrap()
},
-
+
publicFunctions : [ 'min', 'above', 'max', 'below', 'step' ],
validate : function(instance) {
@@ -60,7 +60,7 @@ var NumberSchema = module.exports = Schema.extend({
, min = this.minimum
, max = this.maximum
, step = this.divisibleBy
-
+
// If there's no declared maximum or minimum then assigning a reasonable value
if (min === -Infinity || max === Infinity) {
length = 10
@@ -75,55 +75,55 @@ var NumberSchema = module.exports = Schema.extend({
max = min + length
}
}
-
+
// Choosing random number from a continuous set
if (step === 0) {
length = max - min
do {
random = min + Math.random()*length
} while ((this.exclusiveMinimum && random === min) || (this.exclusiveMaximum && random === max))
-
+
// Choosing random number from a discrete set
} else {
min = Math.ceil(min / step) * step
max = Math.floor(max / step) * step
-
+
if (this.exclusiveMinimum && min === this.minimum) min += step
if (this.exclusiveMaximum && max === this.maximum) max -= step
-
+
if (min > max) return undefined
-
+
length = Math.round((max - min) / step) + 1
random = min + step * Math.floor(Math.random()*length)
}
-
+
return random
},
toJSON : function() {
var json = Schema.prototype.toJSON.call(this)
-
+
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) {
json.minimum = this.minimum
if (this.exclusiveMinimum === true) json.exclusiveMinimum = true
}
-
+
if (this.maximum !== Infinity) {
json.maximum = this.maximum
if (this.exclusiveMaximum === true) json.exclusiveMaximum = true
}
-
+
return json
}
})
Schema.fromJSON.def(function(sch) {
if (!sch || (sch.type !== 'number' && sch.type !== 'integer')) return
-
+
return new NumberSchema( sch.minimum, sch.exclusiveMinimum
, sch.maximum, sch.exclusiveMaximum
, sch.divisibleBy || (sch.type === 'integer' ? 1 : 0)
View
8 lib/extensions/Schema.js
@@ -5,10 +5,10 @@ var SchemaReference = Schema.extend({
validate : function() {
throw new Error('Trying to validate unresolved schema reference.')
},
-
+
resolve : function(schemaDescriptor) {
var schemaObject = Schema.fromJS(schemaDescriptor)
-
+
for (var key in schemaObject) {
if (schemaObject[key] instanceof Function) {
this[key] = schemaObject[key].bind(schemaObject)
@@ -16,10 +16,10 @@ var SchemaReference = Schema.extend({
this[key] = schemaObject[key]
}
}
-
+
delete this.resolve
},
-
+
publicFunctions : [ 'resolve' ]
})
View
2  lib/extensions/String.js
@@ -9,7 +9,7 @@ String.of = function() {
, max = args[1]
, min = (args.length > 2) ? args[2] : args[1]
, regexp = '^' + charset + '{' + (min || 0) + ',' + (max || '') + '}$'
-
+
return new RegexpSchema(RegExp(regexp)).wrap()
}
View
2  lib/patterns/anything.js
@@ -5,7 +5,7 @@ var AnythingSchema = Schema.extend({
validate : function(instance) {
return instance != null
},
-
+
generate : function() {
var type = [{schema:nothing}, Boolean, Number, String, Array, Object][Math.floor(Math.random()*6)]
return type.schema.generate()
View
8 lib/patterns/class.js
@@ -4,15 +4,15 @@ var ClassSchema = module.exports = Schema.extend({
initialize : function(constructor) {
this.constructor = constructor
},
-
+
validate : function(instance) {
return instance instanceof this.constructor
},
-
+
generate : function() {
var Constructor = function() {}
Constructor.prototype = this.constructor.prototype
-
+
return new Constructor()
}
})
@@ -20,7 +20,7 @@ var ClassSchema = module.exports = Schema.extend({
Schema.fromJS.def(function(constructor) {
if (!(constructor instanceof Function)) return
-
+
if (constructor.schema instanceof Function) {
return constructor.schema.unwrap()
} else {
View
16 lib/patterns/equality.js
@@ -4,18 +4,18 @@ var Schema = require('../Schema')
var equal = function(a, b) {
// if a or b is primitive, simple comparison
if (Object(a) !== a || Object(b) !== b) return a === b
-
+
// both a and b must be Array, or none of them
if ((a instanceof Array) !== (b instanceof Array)) return false
-
+
// they must have the same number of properties
if (Object.keys(a).length !== Object.keys(b).length) return false
-
+
// and every property should be equal
for (var key in a) {
if (!equal(a[key], b[key])) return false
}
-
+
// if every check succeeded, they are deep equal
return true
}
@@ -24,16 +24,16 @@ var EqualitySchema = module.exports = Schema.extend({
initialize : function(object) {
this.object = object
},
-
+
validate : function(instance) {
return equal(instance, this.object)
},
-
+
toJSON : function() {
var json = Schema.prototype.toJSON.call(this)
-
+
json['enum'] = [this.object]
-
+
return json
}
})
View
4 lib/patterns/nothing.js
@@ -4,11 +4,11 @@ var NothingSchema = Schema.extend({
validate : function(instance) {
return instance == null
},
-
+
generate : function() {
return Math.random() < 0.5 ? null : undefined
},
-
+
toJSON : function() {
return { type : 'null' }
}
View
70 lib/patterns/object.js
@@ -7,10 +7,10 @@ var Schema = require('../Schema')
var ObjectSchema = module.exports = Schema.extend({
initialize : function(properties, other) {
var self = this
-
+
this.other = other || anything
this.properties = properties || []
-
+
// Sorting properties into two groups
this.stringProps = {}, this.regexpProps = []
this.properties.forEach(function(property) {
@@ -21,26 +21,26 @@ var ObjectSchema = module.exports = Schema.extend({
}
})
},
-
+
validate : function(instance) {
var self = this
-
+
if (instance == null) return false
-
+
// Simple string properties
var stringPropsValid = Object.keys(this.stringProps).every(function(key) {
return (self.stringProps[key].min === 0 && !(key in instance)) ||
(self.stringProps[key].value.validate(instance[key]))
})
if (!stringPropsValid) return false
-
+
// If there are no RegExp and other validator, that's all
if (!this.regexpProps.length && this.other === anything) return true
-
+
// Regexp and other properties
var checked
for (var key in instance) {
-
+
// Checking the key against every key regexps
checked = false
var regexpPropsValid = Object.keys(this.regexpProps).every(function(key) {
@@ -49,20 +49,20 @@ var ObjectSchema = module.exports = Schema.extend({
)
})
if (!regexpPropsValid) return false
-
+
// If the key is not matched by regexps and by simple string checks
// then check it against this.other
if (!checked && !(key in this.stringProps) && !this.other.validate(instance[key])) return false
-
+
}
-
+
// If all checks passed, the instance conforms to the schema
return true
},
-
+
generate : function() {
var object = {}
-
+
for (var key in this.stringProps) {
if (this.stringProps[key].min === 1 || Math.random() < 0.5) {
object[key] = this.stringProps[key].value.generate()
@@ -73,12 +73,12 @@ var ObjectSchema = module.exports = Schema.extend({
for (var i = 0; i < this.regexpProps.length; i++) {
property = this.regexpProps[i]
n = Object.keys(object).filter(function(ikey){ return property.key.test(ikey) }).length
-
+
randexp = new RandExp(property.key)
while (n < property.min || (n < property.max && Math.random() < 0.5)) {
key = randexp.gen()
if (key in object) continue
-
+
object[key] = property.value.generate()
n += 1
}
@@ -93,14 +93,14 @@ var ObjectSchema = module.exports = Schema.extend({
return object
},
-
+
toJSON : Schema.session(function() {
var i, property, regexp, json = Schema.prototype.toJSON.call(this, true)
if (json['$ref'] != null) return json
-
+
json.type = 'object'
-
+
for (i in this.stringProps) {
property = this.stringProps[i]
json.properties = json.properties || {}
@@ -108,7 +108,7 @@ var ObjectSchema = module.exports = Schema.extend({
if (property.min === 1) json.properties[property.key].required = true
if (property.title) json.properties[property.key].title = property.title
}
-
+
for (i = 0; i < this.regexpProps.length; i++) {
property = this.regexpProps[i]
json.patternProperties = json.patternProperties || {}
@@ -117,11 +117,11 @@ var ObjectSchema = module.exports = Schema.extend({
json.patternProperties[regexp] = property.value.toJSON()
if (property.title) json.patternProperties[regexp].title = property.title
}
-
+
if (this.other !== anything) {
json.additionalProperties = (this.other === nothing) ? false : this.other.toJSON()
}
-
+
return json
})
})
@@ -137,10 +137,10 @@ var regexpString = (function() {
var shouldntBeEscaped = 'bBwWdDsS'.split('').map(function(element) {
return RegExp('(\\\\)*' + element, 'g')
})
-
+
return function(string) {
var i, j, match
-
+
for (i = 0; i < shouldBeEscaped.length; i++) {
match = string.match(shouldBeEscaped[i])
if (!match) continue
@@ -157,54 +157,54 @@ var regexpString = (function() {
if (match[j].length % 2 === 0) return RegExp('^' + string + '$')
}
}
-
+
// It is not a real regexp. Removing the escaping.
for (i = 0; i < shouldBeEscaped.length; i++) {
string = string.replace(shouldBeEscaped[i], function(match) {
return match.substr(1)
})
}
-
+
return string
}
})()
Schema.fromJS.def(function(object) {
if (!(object instanceof Object)) return
-
+
var other, property, properties = []
for (var key in object) {
property = { value : Schema.fromJS(object[key]) }
-
+
// '*' as property name means 'every other property should match this schema'
if (key === '*') {
other = property.value
continue
}
-
+
// Handling special chars at the beginning of the property name
property.min = (key[0] === '*' || key[0] === '?') ? 0 : 1
property.max = (key[0] === '*' || key[0] === '+') ? Infinity : 1
key = key.replace(/^[*?+]/, '')
-
+
// Handling property title that looks like: { 'a : an important property' : Number }
key = key.replace(/\s*:[^:]+$/, function(match) {
property.title = match.replace(/^\s*:\s*/, '')
return ''
})
-
+
// Testing if it is regexp-like or not. If it is, then converting to a regexp object
property.key = regexpString(key)
-
+
properties.push(property)
}
-
+
return new ObjectSchema(properties, other)
})
Schema.fromJSON.def(function(json) {
if (!json || json.type !== 'object') return
-
+
var key, properties = []
for (key in json.properties) {
properties.push({ min : json.properties[key].required ? 1 : 0
@@ -222,11 +222,11 @@ Schema.fromJSON.def(function(json) {
, title : json.patternProperties[key].title
})
}
-
+
var other
if (json.additionalProperties !== undefined) {
other = json.additionalProperties === false ? nothing : Schema.fromJSON(json.additionalProperties)
}
-
+
return new ObjectSchema(properties, other)
})
View
20 lib/patterns/or.js
@@ -6,38 +6,38 @@ var OrSchema = module.exports = Schema.extend({
initialize : function(schemas) {
this.schemas = schemas
},
-
+
validate : function(instance) {
return this.schemas.some(function(sch) {
return sch.validate(instance)
})
},
-
+
generate : function() {
return this.schemas[Math.floor(Math.random()*this.schemas.length)].generate()
},
-
+
toJSON : Schema.session(function() {
var json = Schema.prototype.toJSON.call(this, true)
, subjsons = this.schemas.map(function(sch) { return sch.toJSON() })
, onlyEquality = subjsons.every(function(json) {
return json['enum'] instanceof Array && json['enum'].length === 1
})
-
+
if (json['$ref'] != null) return json
-
+
if (onlyEquality) {
json['enum'] = subjsons.map(function(json) {
- return json['enum'][0]
+ return json['enum'][0]
})
-
+
} 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
})
})
@@ -51,13 +51,13 @@ Schema.fromJS.def(function(schemas) {
Schema.fromJSON.def(function(sch) {
if (!sch) return
-
+
if (sch['enum'] instanceof Array) {
return new OrSchema(sch['enum'].map(function(object) {
return new EqualitySchema(object)
}))
}
-
+
if (sch['type'] instanceof Array) {
return new OrSchema(sch['type'].map(function(type) {
return Schema.fromJSON(typeof type === 'string' ? { type : type } : type)
View
10 lib/patterns/reference.js
@@ -4,20 +4,20 @@ var ReferenceSchema = module.exports = Schema.extend({
initialize : function(value) {
this.value = value
},
-
+
validate : function(instance) {
return instance === this.value
},
-
+
generate : function() {
return this.value
},
-
+
toJSON : function() {
var json = Schema.prototype.toJSON.call(this)
-
+
json['enum'] = [this.value]
-
+
return json
}
})
View
14 lib/patterns/regexp.js
@@ -8,32 +8,32 @@ var RegexpSchema = module.exports = Schema.extend({
this.regexp = regexp
this.randexp = this.regexp ? new RandExp(this.regexp) : defaultRandExp
},
-
+
validate : function(instance) {
return Object(instance) instanceof String && (!this.regexp || this.regexp.test(instance))
},
-
+
generate : function() {
return this.randexp.gen()
},
-
+
toJSON : function() {
var json = Schema.prototype.toJSON.call(this)
-
+
json.type = 'string'
-
+
if (this.regexp) {
json.pattern = this.regexp.toString()
json.pattern = json.pattern.substr(1, json.pattern.length - 2)
}
-
+
return json
}
})
Schema.fromJSON.def(function(sch) {
if (!sch || sch.type !== 'string') return
-
+
if ('pattern' in sch) {
return new RegexpSchema(RegExp('^' + sch.pattern + '$'))
} else if ('minLength' in sch || 'maxLength' in sch) {
View
6 lib/schema.js
@@ -2,19 +2,19 @@ var Schema = require('./Schema')
var schema = module.exports = function(schemaDescription) {
var doc, schemaObject
-
+
if (arguments.length === 2) {
doc = schemaDescription
schemaDescription = arguments[1]
}
-
+
if (this instanceof schema) {
// When called with new, create a schema object and then return the schema function
var constructor = Schema.extend(schemaDescription)
schemaObject = new constructor()
if (doc) schemaObject.doc = doc
return schemaObject.wrap()
-
+
} else {
// When called as simple function, forward everything to fromJS
// and then resolve schema.self to the resulting schema object
Please sign in to comment.
Something went wrong with that request. Please try again.