Skip to content

Commit

Permalink
Revert "fix: don't map array args when specified as arrays closes #2716"
Browse files Browse the repository at this point in the history
This reverts commit 7cbb13d.
  • Loading branch information
logaretm committed Jul 1, 2020
1 parent deedbb8 commit dc01dae
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 27 deletions.
10 changes: 4 additions & 6 deletions src/utils/rules.ts
Expand Up @@ -27,20 +27,18 @@ export function normalizeRules(rules: any) {
if (isObject(rules)) {
return Object.keys(rules).reduce((prev, curr) => {
let params = [];
let preserveArrayParams = false;
if (rules[curr] === true) {
params = [];
} else if (Array.isArray(rules[curr])) {
params = rules[curr];
preserveArrayParams = true;
} else if (isObject(rules[curr])) {
params = rules[curr];
} else {
params = [rules[curr]];
}

if (rules[curr] !== false) {
prev[curr] = buildParams(curr, params, preserveArrayParams);
prev[curr] = buildParams(curr, params);
}

return prev;
Expand All @@ -65,7 +63,7 @@ export function normalizeRules(rules: any) {
}, acc);
}

function buildParams(ruleName: string, provided: any[] | Record<string, any>, preserveArrayParams = false) {
function buildParams(ruleName: string, provided: any[] | Record<string, any>) {
const ruleSchema = RuleContainer.getRuleDefinition(ruleName);
if (!ruleSchema) {
return provided;
Expand Down Expand Up @@ -104,14 +102,14 @@ function buildParams(ruleName: string, provided: any[] | Record<string, any>, pr
const options = definedParams[i];
let value = options.default;
// if the provided is an array, map element value.
if (Array.isArray(provided) && !preserveArrayParams) {
if (Array.isArray(provided)) {
if (i in provided) {
value = provided[i];
}
} else {
// If the param exists in the provided object.
if (options.name in provided) {
value = (provided as Record<string, any>)[options.name];
value = provided[options.name];
// if the provided is the first param value.
} else if (definedParams.length === 1) {
value = provided;
Expand Down
21 changes: 0 additions & 21 deletions tests/validate.spec.js
Expand Up @@ -30,24 +30,3 @@ test('allows empty rules for the string format', async () => {
result = await validate(100, '||||numeric');
expect(result.valid).toBe(true);
});

describe('array args collection', () => {
extend('array_args', {
validate(val, args) {
return args.arr.indexOf(val) === -1;
},
params: ['arr']
});

test('should collect args in an array when using strings', async () => {
const result = await validate(2, 'array_args:1');

expect(result.valid).toBe(true);
});

test('should preserve array args as is when using objects', async () => {
const result = await validate(2, { array_args: [1] });

expect(result.valid).toBe(true);
});
});

0 comments on commit dc01dae

Please sign in to comment.