diff --git a/CHANGELOG.md b/CHANGELOG.md index c52fb15..c4ddec8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Rule.js b/src/Rule.js index b5dccbf..95cf0f7 100644 --- a/src/Rule.js +++ b/src/Rule.js @@ -10,7 +10,7 @@ class Rule { let fn = this.fn; try { - fn(value); + testAux(this.modifiers.slice(), fn)(value); } catch (ex) { fn = () => false; } @@ -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; diff --git a/src/v8n.js b/src/v8n.js index d174b28..2a4c52a 100644 --- a/src/v8n.js +++ b/src/v8n.js @@ -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: { diff --git a/src/v8n.test.js b/src/v8n.test.js index d78a021..31c67f3 100644 --- a/src/v8n.test.js +++ b/src/v8n.test.js @@ -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(); }); }); @@ -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(); }); });