Skip to content

Commit

Permalink
Merge pull request #129 from imbrn/fix/every-schema-bug
Browse files Browse the repository at this point in the history
Fix every schema bug
  • Loading branch information
imbrn authored Oct 3, 2018
2 parents a84cd65 + 3eae80a commit e624aca
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Fixed

- Bug with `schema` rule when working with array-based modifiers ([#127](https://github.com/imbrn/v8n/issues/127))

## [1.2.2] - 2018-08-29

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions src/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Rule {
let fn = this.fn;

try {
fn(value);
testAux(this.modifiers.slice(), fn)(value);
} catch (ex) {
fn = () => false;
}
Expand All @@ -24,7 +24,7 @@ class Rule {

_check(value) {
try {
this.fn(value);
testAux(this.modifiers.slice(), this.fn)(value);
} catch (ex) {
if (testAux(this.modifiers.slice(), it => it)(false)) {
return;
Expand Down
23 changes: 20 additions & 3 deletions src/v8n.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,26 @@ const availableModifiers = {
},

some: {
simple: fn => value => split(value).some(fn),
async: fn => value =>
Promise.all(split(value).map(fn)).then(result => result.some(Boolean))
simple: fn => value => {
return split(value).some(item => {
try {
return fn(item);
} catch (ex) {
return false;
}
});
},
async: fn => value => {
return Promise.all(
split(value).map(item => {
try {
return fn(item);
} catch (ex) {
return false;
}
})
).then(result => result.some(Boolean));
}
},

every: {
Expand Down
70 changes: 69 additions & 1 deletion src/v8n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,48 @@ describe("modifiers", () => {
});

describe("the 'some' modifier", () => {
it("expect that rule passes on some array value", () => {
it("expect that rule passes on some array value", async () => {
const validation = v8n().some.positive();
expect(validation.test([-1, -2, -3])).toBeFalsy();
expect(validation.test(10)).toBeFalsy();
expect(validation.test([-1, -2, 1])).toBeTruthy();
expect(validation.test([1, 2, 3])).toBeTruthy();

expect(
v8n()
.some.schema({ str: v8n().string() })
.test([true, { str: "hello" }, 12])
).toBeTruthy();

expect(
v8n()
.some.schema({ str: v8n().string() })
.test(["hello", { str: true }, 12])
).toBeFalsy();

expect(() =>
v8n()
.some.schema({ str: v8n().string() })
.check([true, { str: "hello" }, 12])
).not.toThrow();

expect(() =>
v8n()
.some.schema({ str: v8n().string() })
.check(["hello", { str: true }, 12])
).toThrow();

await expect(
v8n()
.some.schema({ str: v8n().string() })
.testAsync([true, { str: "hello" }, 12])
).resolves.toEqual([true, { str: "hello" }, 12]);

await expect(
v8n()
.some.schema({ str: v8n().string() })
.testAsync([false, { str: true }, 12])
).rejects.toBeDefined();
});
});

Expand All @@ -340,6 +376,38 @@ describe("modifiers", () => {
expect(validation.test([1, 2, 3, -1])).toBeFalsy();
expect(validation.test(10)).toBeFalsy();
expect(validation.test([1, 2, 3])).toBeTruthy();

expect(validation.test([1, 2, 3])).toBeTruthy();

expect(
v8n()
.every.schema({ str: v8n().string() })
.test([{ str: "Hello" }])
).toBeTruthy();

expect(() =>
v8n()
.every.schema({ str: v8n().string() })
.check([{ str: "Hello" }])
).not.toThrow();

expect(() =>
v8n()
.every.schema({ str: v8n().string() })
.check([{ str: true }])
).toThrow();

expect(
v8n()
.every.schema({ str: v8n().string() })
.testAsync([{ str: "Hello" }])
).resolves.toEqual([{ str: "Hello" }]);

expect(
v8n()
.every.schema({ str: v8n().string() })
.testAsync([{ str: true }])
).rejects.toBeDefined();
});
});

Expand Down

0 comments on commit e624aca

Please sign in to comment.