From 5dd862943c0292d99b9db2781237a8427cbb9456 Mon Sep 17 00:00:00 2001 From: tkow Date: Sat, 3 Nov 2018 17:01:33 +0900 Subject: [PATCH] fix: enable to use even if initialstate is unloaded at store --- package.json | 1 + src/lib/index.spec.ts | 28 ++++++++++++++++++++++++++++ src/lib/index.ts | 2 +- yarn.lock | 36 +++++++++++++++++++++++++++++++++++- 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b3428bd..1ad410f 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "opn-cli": "^3.1.0", "prettier": "^1.14.3", "redux": "^4.0.1", + "redux-actions": "^2.6.4", "standard-version": "^4.4.0", "trash-cli": "^1.4.0", "tslint": "^5.11.0", diff --git a/src/lib/index.spec.ts b/src/lib/index.spec.ts index 9c3790d..df2bd96 100644 --- a/src/lib/index.spec.ts +++ b/src/lib/index.spec.ts @@ -1,5 +1,6 @@ import test from "ava"; import { combineReducers, createStore } from "redux"; +import { handleActions } from "redux-actions"; import { isNumber } from "util"; import { watchRootReducer, withValidateReducer } from "./index"; @@ -26,6 +27,13 @@ const postalReducer = ( return state; }; +const initialStateUndefinedReducer = handleActions( + { + SET_NUMBER: () => 123 + }, + 0 +); + const _validateReducer = withValidateReducer(postalReducer, [ { error: { @@ -40,6 +48,26 @@ const identityReducer = (state: string = "hoge", action: { value: string }) => { return action && action.value ? action.value : state; }; +test("initial state not filter if the condition is invalid", async t => { + const rootReducer = watchRootReducer( + combineReducers({ + postalCode: withValidateReducer(initialStateUndefinedReducer, [ + { + error: { + id: "postalCode", + message: "Invalid PostalCode" + }, + validate: _state => Number(_state) > 100 + } + ]) + }) + ); + const store = createStore(rootReducer); + const state = store.getState(); + t.truthy(Object.keys(state.errors).length === 0); + t.truthy(state.postalCode === 0); +}); + test("whether combineReducers can validate for object", async t => { const _validateNestReducer = withValidateReducer(postalReducer, [ { diff --git a/src/lib/index.ts b/src/lib/index.ts index 8dc064c..a32ce40 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -55,7 +55,7 @@ class ValidationWatcher { const next = reducer(prev, action); if ( validators.some(validator => { - const invalid = !validator.validate(next); + const invalid = !validator.validate(next) && prev !== undefined; if (invalid) { this.withError(validator.error); } diff --git a/yarn.lock b/yarn.lock index 93d30b6..0401865 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2395,7 +2395,7 @@ interpret@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614" -invariant@^2.2.2: +invariant@^2.2.2, invariant@^2.2.4: version "2.2.4" resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6" dependencies: @@ -2810,6 +2810,10 @@ jsprim@^1.2.2: json-schema "0.2.3" verror "1.10.0" +just-curry-it@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/just-curry-it/-/just-curry-it-3.1.0.tgz#ab59daed308a58b847ada166edd0a2d40766fbc5" + kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0: version "3.2.2" resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64" @@ -3916,6 +3920,20 @@ redent@^2.0.0: indent-string "^3.0.0" strip-indent "^2.0.0" +reduce-reducers@^0.4.3: + version "0.4.3" + resolved "https://registry.yarnpkg.com/reduce-reducers/-/reduce-reducers-0.4.3.tgz#8e052618801cd8fc2714b4915adaa8937eb6d66c" + +redux-actions@^2.6.4: + version "2.6.4" + resolved "https://registry.yarnpkg.com/redux-actions/-/redux-actions-2.6.4.tgz#e1d9d7d987d274071b0134b707365d3e25ba3b26" + dependencies: + invariant "^2.2.4" + just-curry-it "^3.1.0" + loose-envify "^1.4.0" + reduce-reducers "^0.4.3" + to-camel-case "^1.0.0" + redux@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.1.tgz#436cae6cc40fbe4727689d7c8fae44808f1bfef5" @@ -4550,6 +4568,12 @@ timed-out@^4.0.0: version "4.0.1" resolved "https://registry.yarnpkg.com/timed-out/-/timed-out-4.0.1.tgz#f32eacac5a175bea25d7fab565ab3ed8741ef56f" +to-camel-case@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/to-camel-case/-/to-camel-case-1.0.0.tgz#1a56054b2f9d696298ce66a60897322b6f423e46" + dependencies: + to-space-case "^1.0.0" + to-fast-properties@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47" @@ -4558,6 +4582,10 @@ to-fast-properties@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e" +to-no-case@^1.0.0: + version "1.0.2" + resolved "https://registry.yarnpkg.com/to-no-case/-/to-no-case-1.0.2.tgz#c722907164ef6b178132c8e69930212d1b4aa16a" + to-object-path@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/to-object-path/-/to-object-path-0.3.0.tgz#297588b7b0e7e0ac08e04e672f85c1f4999e17af" @@ -4580,6 +4608,12 @@ to-regex@^3.0.1, to-regex@^3.0.2: regex-not "^1.0.2" safe-regex "^1.1.0" +to-space-case@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/to-space-case/-/to-space-case-1.0.0.tgz#b052daafb1b2b29dc770cea0163e5ec0ebc9fc17" + dependencies: + to-no-case "^1.0.0" + tough-cookie@~2.4.3: version "2.4.3" resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.4.3.tgz#53f36da3f47783b0925afa06ff9f3b165280f781"