Skip to content

Commit

Permalink
stylistic fix : applied jsbeautifier with indent with 2 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
parhelium committed Aug 27, 2014
1 parent 9933540 commit 5ac398f
Show file tree
Hide file tree
Showing 16 changed files with 622 additions and 622 deletions.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ DEBUG="js-schema.debug.js"
MIN="js-schema.min.js"

browserify index.js | sed 's|require("/index.js")|window.schema = require("/index.js")|g' >$DEBUG
browserify index.js > ./example/$DEBUG

uglifyjs $DEBUG >$MIN
2 changes: 1 addition & 1 deletion js-schema.min.js

Large diffs are not rendered by default.

74 changes: 37 additions & 37 deletions lib/extensions/Array.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
var Schema = require('../BaseSchema')
, EqualitySchema = require('../patterns/equality')
, anything = require('../patterns/anything').instance
var Schema = require('../BaseSchema'),
EqualitySchema = require('../patterns/equality'),
anything = require('../patterns/anything').instance

var ArraySchema = module.exports = Schema.extensions.ArraySchema = Schema.extend({
initialize : function(itemSchema, max, min) {
initialize: function(itemSchema, max, min) {
this.itemSchema = itemSchema || anything
this.min = min || 0
this.max = max || Infinity;
},
errors : function(instance) {
var self = this;
// Instance must be an instance of Array
if (!(instance instanceof Array))
return self.err(instance + " is not an instance of Array",instance);

// Checking length
if (this.min === this.max) {
if (instance.length !== this.min) return self.err("Array length should be equal to "+this.min+" and is " + instance.length,instance)

} else {
if (this.min > 0 && instance.length < this.min) return self.err("Array length should be less than "+this.min+" and is " + instance.length, instance);
if (this.max < Infinity && instance.length > this.max) return self.err("Array length should be more than "+this.max+" and is " + instance.length,instance);
}

// Checking conformance to the given item schema
var results = {};
for (var i = 0; i < instance.length; i++) {
var errs = this.itemSchema.errors(instance[i])
if(errs){
results[i] = errs
}
}
var resultKeysArray = Object.keys(results)
if(resultKeysArray.length > 0){
return self.err(resultKeysArray.length +" of "+instance.length + " were invalid",instance,results);
}

return false
errors: function(instance) {
var self = this;
// Instance must be an instance of Array
if (!(instance instanceof Array))
return self.err(instance + " is not an instance of Array", instance);

// Checking length
if (this.min === this.max) {
if (instance.length !== this.min) return self.err("Array length should be equal to " + this.min + " and is " + instance.length, instance)

} else {
if (this.min > 0 && instance.length < this.min) return self.err("Array length should be less than " + this.min + " and is " + instance.length, instance);
if (this.max < Infinity && instance.length > this.max) return self.err("Array length should be more than " + this.max + " and is " + instance.length, instance);
}

// Checking conformance to the given item schema
var results = {};
for (var i = 0; i < instance.length; i++) {
var errs = this.itemSchema.errors(instance[i])
if (errs) {
results[i] = errs
}
}
var resultKeysArray = Object.keys(results)
if (resultKeysArray.length > 0) {
return self.err(resultKeysArray.length + " of " + instance.length + " were invalid", instance, results);
}

return false
},
validate : function(instance) {
validate: function(instance) {
// Instance must be an instance of Array
if (!(instance instanceof Array)) return false

Expand All @@ -47,7 +47,7 @@ var ArraySchema = module.exports = Schema.extensions.ArraySchema = Schema.extend
if (instance.length !== this.min) return false

} else {
if (this.min > 0 && instance.length < this.min) return false
if (this.min > 0 && instance.length < this.min) return false
if (this.max < Infinity && instance.length > this.max) return false
}

Expand All @@ -59,7 +59,7 @@ var ArraySchema = module.exports = Schema.extensions.ArraySchema = Schema.extend
return true
},

toJSON : Schema.session(function() {
toJSON: Schema.session(function() {
var json = Schema.prototype.toJSON.call(this, true)

if (json['$ref'] != null) return json
Expand Down Expand Up @@ -96,4 +96,4 @@ Array.like = function(other) {
return new EqualitySchema(other).wrap()
}

Array.schema = new ArraySchema().wrap()
Array.schema = new ArraySchema().wrap()
33 changes: 18 additions & 15 deletions lib/extensions/Boolean.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
var Schema = require('../BaseSchema')

var BooleanSchema = module.exports = Schema.extensions.BooleanSchema = new Schema.extend({
errors: function (instance) {
if (!this.validate(instance)) {
return this.err(instance + " is not Boolean", instance);
}
return false;
},
validate: function (instance) {
return Object(instance) instanceof Boolean
},
errors: function(instance) {
if (!this.validate(instance)) {
return this.err(instance + " is not Boolean", instance);
}
return false;
},

validate: function(instance) {
return Object(instance) instanceof Boolean
},

toJSON: function () {
return { type: 'boolean' }
toJSON: function() {
return {
type: 'boolean'
}
}
})

var booleanSchema = module.exports = new BooleanSchema().wrap()

Schema.fromJSON.def(function (sch) {
if (!sch || sch.type !== 'boolean') return
Schema.fromJSON.def(function(sch) {
if (!sch || sch.type !== 'boolean') return

return booleanSchema
return booleanSchema
})

Boolean.schema = booleanSchema
Boolean.schema = booleanSchema
106 changes: 43 additions & 63 deletions lib/extensions/Number.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,64 @@
var Schema = require('../BaseSchema')

var NumberSchema = module.exports = Schema.extensions.NumberSchema = Schema.extend({
initialize : function(minimum, exclusiveMinimum, maximum, exclusiveMaximum, divisibleBy) {
initialize: function(minimum, exclusiveMinimum, maximum, exclusiveMaximum, divisibleBy) {
this.minimum = minimum != null ? minimum : -Infinity
this.exclusiveMinimum = exclusiveMinimum
this.maximum = minimum != null ? maximum : Infinity
this.exclusiveMaximum = exclusiveMaximum
this.divisibleBy = divisibleBy || 0
},

min : function(minimum) {
return new NumberSchema( minimum, false
, this.maximum, this.exclusiveMaximum
, this.divisibleBy
).wrap()
min: function(minimum) {
return new NumberSchema(minimum, false, this.maximum, this.exclusiveMaximum, this.divisibleBy).wrap()
},

above : function(minimum) {
return new NumberSchema( minimum, true
, this.maximum, this.exclusiveMaximum
, this.divisibleBy
).wrap()
above: function(minimum) {
return new NumberSchema(minimum, true, this.maximum, this.exclusiveMaximum, this.divisibleBy).wrap()
},

max : function(maximum) {
return new NumberSchema( this.minimum, this.exclusiveMinimum
, maximum, false
, this.divisibleBy
).wrap()
max: function(maximum) {
return new NumberSchema(this.minimum, this.exclusiveMinimum, maximum, false, this.divisibleBy).wrap()
},

below : function(maximum) {
return new NumberSchema( this.minimum, this.exclusiveMinimum
, maximum, true
, this.divisibleBy
).wrap()
below: function(maximum) {
return new NumberSchema(this.minimum, this.exclusiveMinimum, maximum, true, this.divisibleBy).wrap()
},

step : function(divisibleBy) {
return new NumberSchema( this.minimum, this.exclusiveMinimum
, this.maximum, this.exclusiveMaximum
, divisibleBy
).wrap()
step: function(divisibleBy) {
return new NumberSchema(this.minimum, this.exclusiveMinimum, this.maximum, this.exclusiveMaximum, divisibleBy).wrap()
},

publicFunctions : [ 'min', 'above', 'max', 'below', 'step' ],

errors : function(instance) {
var message;
if(!(Object(instance) instanceof Number)) {
message = instance+" is not Number";
}else if(instance < this.minimum){
message = "number = "+instance+" is smaller than required minimum = "+this.minimum;
}else if(instance > this.maximum) {
message = "number = "+instance+" is bigger than required maximum = "+this.maximum;
}else if(this.divisibleBy !== 0 && instance % this.divisibleBy !== 0){
message = "number = "+instance+ " is not divisbleBy " + this.divisibleBy;
}

if(message != null){
return this.err(message,instance);
}
return false;
publicFunctions: ['min', 'above', 'max', 'below', 'step'],

errors: function(instance) {
var message;
if (!(Object(instance) instanceof Number)) {
message = instance + " is not Number";
} else if (instance < this.minimum) {
message = "number = " + instance + " is smaller than required minimum = " + this.minimum;
} else if (instance > this.maximum) {
message = "number = " + instance + " is bigger than required maximum = " + this.maximum;
} else if (this.divisibleBy !== 0 && instance % this.divisibleBy !== 0) {
message = "number = " + instance + " is not divisbleBy " + this.divisibleBy;
}

if (message != null) {
return this.err(message, instance);
}
return false;
},
validate : function(instance) {
validate: function(instance) {
return (Object(instance) instanceof Number) &&
(this.exclusiveMinimum ? instance > this.minimum
: instance >= this.minimum) &&
(this.exclusiveMaximum ? instance < this.maximum
: instance <= this.maximum) &&
(this.divisibleBy === 0 || instance % this.divisibleBy === 0)
(this.exclusiveMinimum ? instance > this.minimum : instance >= this.minimum) &&
(this.exclusiveMaximum ? instance < this.maximum : instance <= this.maximum) &&
(this.divisibleBy === 0 || instance % this.divisibleBy === 0)
},

toJSON : function() {
toJSON: function() {
var json = Schema.prototype.toJSON.call(this)

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

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

Expand All @@ -96,17 +79,14 @@ var NumberSchema = module.exports = Schema.extensions.NumberSchema = Schema.exte
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)
)
return new NumberSchema(sch.minimum, sch.exclusiveMinimum, sch.maximum, sch.exclusiveMaximum, sch.divisibleBy || (sch.type === 'integer' ? 1 : 0))
})

Number.schema = new NumberSchema().wrap()
Number.min = Number.schema.min
Number.above = Number.schema.above
Number.max = Number.schema.max
Number.below = Number.schema.below
Number.step = Number.schema.step
Number.schema = new NumberSchema().wrap()
Number.min = Number.schema.min
Number.above = Number.schema.above
Number.max = Number.schema.max
Number.below = Number.schema.below
Number.step = Number.schema.step

Number.Integer = Number.step(1)
Number.Integer = Number.step(1)
62 changes: 31 additions & 31 deletions lib/patterns/class.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
var Schema = require('../BaseSchema')

var ClassSchema = module.exports = Schema.patterns.ClassSchema = Schema.extend({
initialize: function (constructor) {
this.constructor = constructor
},
getName: function (obj) {
if(!obj) return obj;
if (obj instanceof Object) {
return obj.constructor.name
} else {
return typeof obj + " = " + obj;
}
},
errors: function (instance) {
var middleMessage = " is not instance of ";
if (instance == null) {
return this.err(this.getName(instance) + middleMessage + this.getName(this.constructor))
}
if (!(instance instanceof this.constructor)) {
return this.err(this.getName(instance) + middleMessage + this.getName(this.constructor))
}
return false;
},
validate: function (instance) {
return instance instanceof this.constructor
initialize: function(constructor) {
this.constructor = constructor
},
getName: function(obj) {
if (!obj) return obj;
if (obj instanceof Object) {
return obj.constructor.name
} else {
return typeof obj + " = " + obj;
}
},
errors: function(instance) {
var middleMessage = " is not instance of ";
if (instance == null) {
return this.err(this.getName(instance) + middleMessage + this.getName(this.constructor))
}
if (!(instance instanceof this.constructor)) {
return this.err(this.getName(instance) + middleMessage + this.getName(this.constructor))
}
return false;
},
validate: function(instance) {
return instance instanceof this.constructor
}
})


Schema.fromJS.def(function (constructor) {
if (!(constructor instanceof Function)) return
Schema.fromJS.def(function(constructor) {
if (!(constructor instanceof Function)) return

if (constructor.schema instanceof Function) {
return constructor.schema.unwrap()
} else {
return new ClassSchema(constructor)
}
})
if (constructor.schema instanceof Function) {
return constructor.schema.unwrap()
} else {
return new ClassSchema(constructor)
}
})
Loading

0 comments on commit 5ac398f

Please sign in to comment.