From d4ab525ad22cce93fa455261d435b1757fa346e3 Mon Sep 17 00:00:00 2001 From: Yedid Herskovitz Date: Wed, 23 Nov 2016 12:02:47 -0500 Subject: [PATCH 1/2] supporting custom validation --- index.js | 1 + lib/extensions/Function.js | 9 +++++++-- lib/patterns/custom.js | 25 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 lib/patterns/custom.js diff --git a/index.js b/index.js index 5fd271e..e9f850a 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ module.exports = require('./lib/schema') // Patterns +require('./lib/patterns/custom') require('./lib/patterns/reference') require('./lib/patterns/nothing') require('./lib/patterns/anything') diff --git a/lib/extensions/Function.js b/lib/extensions/Function.js index 5954cbb..9d10bff 100644 --- a/lib/extensions/Function.js +++ b/lib/extensions/Function.js @@ -1,5 +1,10 @@ var ReferenceSchema = require('../patterns/reference') +var CustomSchema = require('../patterns/custom') -Function.reference = function(f) { - return new ReferenceSchema(f).wrap() +Function.reference = function (f) { + return new ReferenceSchema(f).wrap() +} + +Function.custom = function (f) { + return new CustomSchema(f).wrap() } diff --git a/lib/patterns/custom.js b/lib/patterns/custom.js new file mode 100644 index 0000000..061572e --- /dev/null +++ b/lib/patterns/custom.js @@ -0,0 +1,25 @@ +var Schema = require('../BaseSchema') + +var CustomSchema = module.exports = Schema.patterns.CustomSchema = Schema.extend({ + initialize: function (func) { + this.func = func + }, + errors: function (instance) { + var self = this + var result = self.func(instance); + + if (!result.valid) + return result.msg + return false + }, + validate: function (instance) { + return this.func(instance).valid + }, + toJSON: function () { + return { type: 'custom' } + } +}) + +Schema.fromJS.def(function (func) { + return new CustomSchema(func) +}) From 4bfb153f5c1ee6e9e3ddbfe223b990217ec283d8 Mon Sep 17 00:00:00 2001 From: Yedid Date: Wed, 23 Nov 2016 12:24:18 -0500 Subject: [PATCH 2/2] documenting custom functions --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7aedd0f..598b584 100644 --- a/README.md +++ b/README.md @@ -240,7 +240,14 @@ The `Array.of` method has three signatures: ### Functions ### -`Function.reference(func)` matches `x` if `x === func`. +The `Function.reference(func)` matches `x` if `x === func`. + +The `Function.custom(func)` takes a user-defined function and passes `x` to it. The user-defined function must return `{valid: myBooleanValue, msg: 'My fail message'}` where the `valid` field tells js-schema to pass or fail the test, and `msg` is the error message shown if `valid` is `false`. For example: +```javascript +var validate = schema({ +  'name': Function.custom(x => ( { valid: x !== 'Bob' && x.length < 4, msg: 'Name is Bob or is too long' } )) +}); +``` Future plans ============