From bae6a32b83209846b2a6a757c0c414d537c157b2 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 8 Apr 2015 23:04:56 -0400 Subject: adding enum validation --- app/node_modules/okschema/index.js | 12 ++++++++++-- examples/db.json | 4 +++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/node_modules/okschema/index.js b/app/node_modules/okschema/index.js index 51783a1..0829807 100644 --- a/app/node_modules/okschema/index.js +++ b/app/node_modules/okschema/index.js @@ -19,7 +19,15 @@ var types = { 'text': { parent: {type: 'string'}, // Let parent handle validation - assertValid: function(s) {} + assertValid: function(spec, value) {} + }, + 'enum': { + parent: {type: 'string'}, + assertValid: function(spec, value) { + if (~spec.options.indexOf(value)) { + throw [] + } + } } } @@ -59,7 +67,7 @@ OKSchema.prototype.assertValid = function(data) { Object.keys(data).forEach(function(prop) { var type = spec[prop].type; if (types[type]) { - types[type].assertValid(data[prop]); + types[type].assertValid(spec[prop], data[prop]); } }); var result = mschema.validate(data, this.toMschema()); diff --git a/examples/db.json b/examples/db.json index e0ea06b..b2cdf4a 100644 --- a/examples/db.json +++ b/examples/db.json @@ -4,7 +4,9 @@ { "type": "pretzel", "description": "really a very tasty bread! yup yes", - "id": "pretzel" + "color": "blue", + "id": "pretzel", + "title": "" }, { "type": "bagel", -- cgit v1.2.3-70-g09d2