Skip to content
This repository was archived by the owner on Aug 3, 2022. It is now read-only.

Commit 5dd8629

Browse files
committed
fix: enable to use even if initialstate is unloaded at store
1 parent feb13c6 commit 5dd8629

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"opn-cli": "^3.1.0",
7272
"prettier": "^1.14.3",
7373
"redux": "^4.0.1",
74+
"redux-actions": "^2.6.4",
7475
"standard-version": "^4.4.0",
7576
"trash-cli": "^1.4.0",
7677
"tslint": "^5.11.0",

src/lib/index.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import test from "ava";
22
import { combineReducers, createStore } from "redux";
3+
import { handleActions } from "redux-actions";
34
import { isNumber } from "util";
45
import { watchRootReducer, withValidateReducer } from "./index";
56

@@ -26,6 +27,13 @@ const postalReducer = (
2627
return state;
2728
};
2829

30+
const initialStateUndefinedReducer = handleActions(
31+
{
32+
SET_NUMBER: () => 123
33+
},
34+
0
35+
);
36+
2937
const _validateReducer = withValidateReducer(postalReducer, [
3038
{
3139
error: {
@@ -40,6 +48,26 @@ const identityReducer = (state: string = "hoge", action: { value: string }) => {
4048
return action && action.value ? action.value : state;
4149
};
4250

51+
test("initial state not filter if the condition is invalid", async t => {
52+
const rootReducer = watchRootReducer(
53+
combineReducers({
54+
postalCode: withValidateReducer(initialStateUndefinedReducer, [
55+
{
56+
error: {
57+
id: "postalCode",
58+
message: "Invalid PostalCode"
59+
},
60+
validate: _state => Number(_state) > 100
61+
}
62+
])
63+
})
64+
);
65+
const store = createStore(rootReducer);
66+
const state = store.getState();
67+
t.truthy(Object.keys(state.errors).length === 0);
68+
t.truthy(state.postalCode === 0);
69+
});
70+
4371
test("whether combineReducers can validate for object", async t => {
4472
const _validateNestReducer = withValidateReducer(postalReducer, [
4573
{

src/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ValidationWatcher {
5555
const next = reducer(prev, action);
5656
if (
5757
validators.some(validator => {
58-
const invalid = !validator.validate(next);
58+
const invalid = !validator.validate(next) && prev !== undefined;
5959
if (invalid) {
6060
this.withError(validator.error);
6161
}

yarn.lock

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2395,7 +2395,7 @@ interpret@^1.0.0:
23952395
version "1.1.0"
23962396
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614"
23972397

2398-
invariant@^2.2.2:
2398+
invariant@^2.2.2, invariant@^2.2.4:
23992399
version "2.2.4"
24002400
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6"
24012401
dependencies:
@@ -2810,6 +2810,10 @@ jsprim@^1.2.2:
28102810
json-schema "0.2.3"
28112811
verror "1.10.0"
28122812

2813+
just-curry-it@^3.1.0:
2814+
version "3.1.0"
2815+
resolved "https://registry.yarnpkg.com/just-curry-it/-/just-curry-it-3.1.0.tgz#ab59daed308a58b847ada166edd0a2d40766fbc5"
2816+
28132817
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
28142818
version "3.2.2"
28152819
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
@@ -3916,6 +3920,20 @@ redent@^2.0.0:
39163920
indent-string "^3.0.0"
39173921
strip-indent "^2.0.0"
39183922

3923+
reduce-reducers@^0.4.3:
3924+
version "0.4.3"
3925+
resolved "https://registry.yarnpkg.com/reduce-reducers/-/reduce-reducers-0.4.3.tgz#8e052618801cd8fc2714b4915adaa8937eb6d66c"
3926+
3927+
redux-actions@^2.6.4:
3928+
version "2.6.4"
3929+
resolved "https://registry.yarnpkg.com/redux-actions/-/redux-actions-2.6.4.tgz#e1d9d7d987d274071b0134b707365d3e25ba3b26"
3930+
dependencies:
3931+
invariant "^2.2.4"
3932+
just-curry-it "^3.1.0"
3933+
loose-envify "^1.4.0"
3934+
reduce-reducers "^0.4.3"
3935+
to-camel-case "^1.0.0"
3936+
39193937
redux@^4.0.1:
39203938
version "4.0.1"
39213939
resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.1.tgz#436cae6cc40fbe4727689d7c8fae44808f1bfef5"
@@ -4550,6 +4568,12 @@ timed-out@^4.0.0:
45504568
version "4.0.1"
45514569
resolved "https://registry.yarnpkg.com/timed-out/-/timed-out-4.0.1.tgz#f32eacac5a175bea25d7fab565ab3ed8741ef56f"
45524570

4571+
to-camel-case@^1.0.0:
4572+
version "1.0.0"
4573+
resolved "https://registry.yarnpkg.com/to-camel-case/-/to-camel-case-1.0.0.tgz#1a56054b2f9d696298ce66a60897322b6f423e46"
4574+
dependencies:
4575+
to-space-case "^1.0.0"
4576+
45534577
to-fast-properties@^1.0.3:
45544578
version "1.0.3"
45554579
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:
45584582
version "2.0.0"
45594583
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e"
45604584

4585+
to-no-case@^1.0.0:
4586+
version "1.0.2"
4587+
resolved "https://registry.yarnpkg.com/to-no-case/-/to-no-case-1.0.2.tgz#c722907164ef6b178132c8e69930212d1b4aa16a"
4588+
45614589
to-object-path@^0.3.0:
45624590
version "0.3.0"
45634591
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:
45804608
regex-not "^1.0.2"
45814609
safe-regex "^1.1.0"
45824610

4611+
to-space-case@^1.0.0:
4612+
version "1.0.0"
4613+
resolved "https://registry.yarnpkg.com/to-space-case/-/to-space-case-1.0.0.tgz#b052daafb1b2b29dc770cea0163e5ec0ebc9fc17"
4614+
dependencies:
4615+
to-no-case "^1.0.0"
4616+
45834617
tough-cookie@~2.4.3:
45844618
version "2.4.3"
45854619
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.4.3.tgz#53f36da3f47783b0925afa06ff9f3b165280f781"

0 commit comments

Comments
 (0)