Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ [feature] supports the enum/in can use object to transform value #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Anchor.prototype.to = function (ruleset, context) {

if (rule === 'type') {

if (typeof ruleset['in'] === 'object' && ruleset['in'].hasOwnProperty(this.data)) {
this.data = ruleset['in'][this.data]
}
// Use deep match to descend into the collection and verify each item and/or key
// Stop at default maxDepth (50) to prevent infinite loops in self-associations
errors = errors.concat(Anchor.match.type.call(context, this.data, ruleset['type']));
Expand Down
24 changes: 24 additions & 0 deletions lib/match/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@
var _ = require('lodash');
var validator = require('validator');

//the validator author reject this PR, so override here:
validator.isIn = function (str, options) {
var i ,result=false;
if (Object.prototype.toString.call(options) === '[object Array]') {
var array = [];
for (i in options) {
array[i] = validator.toString(options[i]);
}
result = array.indexOf(str) >= 0;
} else if (typeof options === 'object') {
if (options.hasOwnProperty(str)) {
result = true;
} else for (i in options) {
if (str == validator.toString(options[i])) {
result = true;
break;
}
}
} else if (options && typeof options.indexOf === 'function') {
result = options.indexOf(str) >= 0;
}
return result;
};



/**
Expand Down
8 changes: 8 additions & 0 deletions test/basicType.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ describe('basic rules', function() {
it ('"number" should identify integers ', function () {
return testType('number',28235,'foo');
});

it ('"number" should support the enum(in) ', function () {
return testType({type: 'number', 'in': {one:1, two:2}},'one','foo', true);
});

it ('"number" should support the enum(in) and pass value directly', function () {
return testType({type: 'number', 'in': {one:1, two:2}}, 1, 'foo', true);
});
});

});
12 changes: 5 additions & 7 deletions test/util/testType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ var async = require('async');

// Test a rule given a deliberate example and nonexample
// Test WITH and WITHOUT callback
module.exports = function testType (rule, example, nonexample) {
module.exports = function testType (rule, example, nonexample, isRule) {

// Throw an error if there's any trouble
// (not a good production usage pattern-- just here for testing)
var exampleOutcome, nonexampleOutcome;

if (isRule !== true)
rule = {type: rule};
// Should be falsy
exampleOutcome = anchor(example).to({
type: rule
});
exampleOutcome = anchor(example).to(rule);

// Should be an array
nonexampleOutcome = anchor(nonexample).to({
type: rule
});
nonexampleOutcome = anchor(nonexample).to(rule);

if (exampleOutcome) {
return gotErrors(exampleOutcome);
Expand Down