-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added null type, removed getDefault, added CHANGELOG
- Loading branch information
1 parent
f85a8d0
commit 56cdb2a
Showing
12 changed files
with
179 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#CHANGELOG | ||
|
||
##2.0 | ||
|
||
- Added `null` type. Previously, `null` was treated the same as `undefined`. Now they behave differently. | ||
- Removed `getDefault` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
var _ = require('lodash') | ||
|
||
module.exports = { | ||
validate: function (obj, s) { | ||
validate: function (obj) { | ||
return _.isBoolean(obj) | ||
}, | ||
validateSchema: function (s) { | ||
validateSchema: function () { | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var _ = require('lodash') | ||
|
||
module.exports = { | ||
validate: function (obj) { | ||
return _.isNull(obj) | ||
}, | ||
validateSchema: function () { | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
var _ = require('lodash') | ||
var n = require('../utils').n | ||
|
||
function isInteger(value) { | ||
return _.isNumber(value) && isFinite(value) && Math.floor(value) === value | ||
} | ||
|
||
module.exports = { | ||
validate: function (obj, s) { | ||
var validator = s.type === 'integer' ? Number.isInteger : _.isFinite | ||
var validator = s.type === 'integer' ? isInteger : _.isFinite | ||
|
||
return ( | ||
validator(obj) && | ||
(n(s.enum) || s.enum.indexOf(obj) > -1) && | ||
(n(s.max) || obj <= s.max) && | ||
(n(s.min) || obj >= s.min) | ||
(_.isUndefined(s.max) || obj <= s.max) && | ||
(_.isUndefined(s.min) || obj >= s.min) | ||
) | ||
}, | ||
|
||
validateSchema: function (s) { | ||
var validator = s.type === 'integer' ? Number.isInteger : _.isFinite | ||
|
||
return ( | ||
(n(s.max) || validator(s.max)) && | ||
(n(s.min) || validator(s.min)) && | ||
(n(s.max) || n(s.min) || s.max >= s.min) | ||
(_.isUndefined(s.max) || validator(s.max)) && | ||
(_.isUndefined(s.min) || validator(s.min)) && | ||
(_.isUndefined(s.max) || _.isUndefined(s.min) || s.max >= s.min) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
var _ = require('lodash') | ||
var n = require('../utils').n | ||
|
||
module.exports = { | ||
validate: function (obj, schema, manager) { | ||
return ( | ||
_.isPlainObject(obj) && | ||
_.every(schema.properties, function (value, key) { | ||
return n(obj[key]) || manager.validate(obj[key], value) | ||
return _.isUndefined(obj[key]) || manager.validate(obj[key], value) | ||
}) | ||
) | ||
}, | ||
validateSchema: function (schema, manager) { | ||
return ( | ||
!n(schema.properties) && | ||
!_.isUndefined(schema.properties) && | ||
_.every(schema.properties, function (value) { | ||
return manager.validateSchema(value) | ||
}) | ||
) | ||
}, | ||
normalize: function (obj, schema, manager) { | ||
return _.chain(schema.properties).mapValues(function (subSchema, propName) { | ||
return manager.normalize(obj[propName], subSchema) | ||
}).omit(n).value() | ||
}, | ||
getDefault: function (schema, manager) { | ||
return _.chain(schema.properties).mapValues(function(subSchema) { | ||
return manager.getDefault(subSchema) | ||
}).omit(n).value() | ||
var realObj = obj || {} | ||
return _.chain(schema.properties) | ||
.mapValues(function (subSchema, propName) { | ||
return manager.normalize(realObj[propName], subSchema) | ||
}) | ||
.omit(_.isUndefined) | ||
.value() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
var _ = require('lodash') | ||
var n = require('../utils').n | ||
|
||
module.exports = { | ||
validate: function (obj, s) { | ||
return ( | ||
_.isString(obj) && | ||
(n(s.regex) || !!obj.match(s.regex)) | ||
(_.isUndefined(s.regex) || !!obj.match(s.regex)) | ||
) | ||
}, | ||
validateSchema: function (s) { | ||
// creating a RegExp with an invalid string throws | ||
if (!n(s.regex) && _.isString(s.regex)) { | ||
if (!_.isUndefined(s.regex) && _.isString(s.regex)) { | ||
try { | ||
/* eslint no-new: 0 */ | ||
new RegExp(s.regex) | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
return n(s.regex) || _.isString(s.regex) || _.isRegExp(s.regex) | ||
return _.isUndefined(s.regex) || _.isString(s.regex) || _.isRegExp(s.regex) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.